ImportC and nothrow/@nogc?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Aug 18 11:17:13 UTC 2024
On Saturday, August 17, 2024 10:43:34 PM MDT Manu via Digitalmars-d 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 >_<
As I understand it, the core problem is that the linkage of the function
does not necessarily match how the function is implemented. So, for
instance, you could have an extern(C) function written in D which happily
uses the GC and/or exceptions. Obviously, in most cases, this is not true -
particularly when you're using importC, since the only way that such C code
would use D features is if it calls D code somewhere, which is only going to
happen under pretty specialized circumstances - so in most cases, you
probably would prefer that extern(C) functions be treated as nothrow and
@nogc. This is far less of an issue when not using importC, because then you
can just slap @nogc and nothrow on the bindings that you write (druntime
routinely does this to entire files with the OS bindings), but for importC,
you're not writing the bindings.
I don't know if there's any plan on how to deal with this. However, importC
is fairly new and is a feature that has grown quite organically (and
weirdly, Walter considers it to be a compiler feature and not a language
feature), so it wouldn't be surprising if this behavior were changed upon
request or that some way were provided to force it (though since Walter
wants imports for importC to look identical to normal D imports, I don't
know how we'd force it without forcing it globally). On the whole, importC
has grown features in response to issues that have come up rather than
having be fully planned out from the get-go (and a number of the issues that
have come up seem to be ones that Walter didn't foresee).
So, I'd suggest that you open an enhancement request and tag it as being
importC, since Walter tends to give those kinds of issue priority (and
having brought up the issue here like you have may bring it to his
attention). I don't know what his response to the issue will be, but the
current state of things is obviously less than ideal.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list