ImportC and nothrow/@nogc?

Atila Neves atila.neves at gmail.com
Thu Aug 29 16:01:40 UTC 2024


On Sunday, 18 August 2024 at 04:43:34 UTC, Manu wrote:
> I just tried using ImportC for the first time ever, but I was 
> surprised
> when I immediately received a sea of errors calling the C 
> symbols from
> `nothrow @nogc` functions.
> My entire program is `nothrow @nogc`... I assumed ImportC would 
> be
> supremely suitable in this context, but apparently not...
>
> Is there something I've missed? Is there a plan for this?
> I found just one single very short forum thread...
>
> This is classic D experience; so much work has been done on 
> this, and then the moment I try and use something I encounter a 
> showstopping oversight >_<

I missed most of this somehow, but after reading the posts in 
this thread, here's what I think:

* Automatically making declarations obtained via ImportC default 
to `@trusted` is a terrible idea.
* Making declarations obtained via ImportC uncallable from D code 
annotated with `@trusted nothrow` with no escape hatch is also a 
terrible idea.

This should work:

@trusted {
     import mycfile;
}

Or, at least, *something* like this should work. I don't think 
it's acceptable to ask people to have to translate (by hand or 
otherwise) C code to be able to add attributes to it. Whatever 
attributes my D code has or not, I should be able to call C code 
via ImportC if I'm convinced the C code is compatible. 
*Especially* for `@nogc nothrow`. Could a "C" function throw? 
Yes. Is that going to be common? No. The same goes for allocating 
with the GC or not.


More information about the Digitalmars-d mailing list