ImportC and nothrow/@nogc?

Gregor Mückl gregormueckl at gmx.de
Tue Aug 20 00:24:25 UTC 2024


On Sunday, 18 August 2024 at 09:42:16 UTC, Nicholas Wilson wrote:
> 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 >_<
>
> Well, C may call C++ which may throw. It is very unlikely to 
> call the GC however.
>
> I don't think it would be hard to add a switch that 
> "`extern(C)` functions being called from import C are `@nogc`".

Here's what I'm thinking:

The C function interface has no notion of C++ exceptions (AFAIK 
it's following C specs, not C++), so C++ exceptions should not 
even be a consideration at that point. I do not think that 
throwing a C++ exception up a stack that contains extern(C) 
functions is even properly defined behavior on the C++ side. In 
other words, all ImportC declarations should be implicitly 
@nothrow.

Any usage of the D GC from a C interface could only come in two 
flavors: D code exported through a C interface or a C interface 
implemented in another language that uses the D GC through a 
library interface. I believe that both cases should be treated as 
if the use of the D GC is an implementation detail and an 
implicit @nogc is warranted.


More information about the Digitalmars-d mailing list