ImportC and nothrow/@nogc?

Steven Schveighoffer schveiguy at gmail.com
Mon Aug 26 17:26:33 UTC 2024


On Wednesday, 21 August 2024 at 19:41:00 UTC, Walter Bright wrote:
> C code can call C++ code which can throw.

Does this actually interact with D code properly? Like, does this 
work?

```d
void fn() nothrow {
    try {
       cFunctionThatCallsCplusplusThatThrows();
    } catch(Exception e) {
    }
}
```

>
> C code can also call D code with can throw and/or gc.
>
> D attributes are all subtractive, meaning they remove 
> capability from the called function. But C code can do anything.

I don't think you understand the problem here.

`printf` doesn't use the GC. It doesn't throw. This is not 
conjecture or philosophy, it objectively, explicitly does not do 
this.

Yet, if I use importC, I can't call `printf` from a `nothrow 
@nogc` function. Consider that I don't want to have to mark a 
function as using the GC and throwing because *`printf` made me 
do it*.

If I import `core.stdc.stdio`, then I can use it, but that 
basically means importC has failed if you have to do that.

In addition, I have already run into situations where I import C 
code that includes stdio.h, so even if I want to use 
`core.stdc.stdio`, I can't.

This must be fixed. There is no other option. If this is not 
fixed, you cannot use importC to actually import C.

https://issues.dlang.org/show_bug.cgi?id=23812

-Steve


More information about the Digitalmars-d mailing list