<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 20 Aug 2024 at 10:26, Gregor Mückl via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sunday, 18 August 2024 at 09:42:16 UTC, Nicholas Wilson wrote:<br>
> On Sunday, 18 August 2024 at 04:43:34 UTC, Manu wrote:<br>
>> I just tried using ImportC for the first time ever, but I was <br>
>> surprised<br>
>> when I immediately received a sea of errors calling the C <br>
>> symbols from<br>
>> `nothrow @nogc` functions.<br>
>> My entire program is `nothrow @nogc`... I assumed ImportC <br>
>> would be<br>
>> supremely suitable in this context, but apparently not...<br>
>><br>
>> Is there something I've missed? Is there a plan for this?<br>
>> I found just one single very short forum thread...<br>
>><br>
>> This is classic D experience; so much work has been done on <br>
>> this, and then the moment I try and use something I encounter <br>
>> a showstopping oversight >_<<br>
><br>
> Well, C may call C++ which may throw. It is very unlikely to <br>
> call the GC however.<br>
><br>
> I don't think it would be hard to add a switch that <br>
> "`extern(C)` functions being called from import C are `@nogc`".<br>
<br>
Here's what I'm thinking:<br>
<br>
The C function interface has no notion of C++ exceptions (AFAIK <br>
it's following C specs, not C++), so C++ exceptions should not <br>
even be a consideration at that point. I do not think that <br>
throwing a C++ exception up a stack that contains extern(C) <br>
functions is even properly defined behavior on the C++ side. In <br>
other words, all ImportC declarations should be implicitly <br>
@nothrow.<br>
<br>
Any usage of the D GC from a C interface could only come in two <br>
flavors: D code exported through a C interface or a C interface <br>
implemented in another language that uses the D GC through a <br>
library interface. I believe that both cases should be treated as <br>
if the use of the D GC is an implementation detail and an <br>
implicit @nogc is warranted.<br></blockquote><div><br></div><div>Exactly... these points are my precise assumption going in here.</div><div>Also, considering the possibility that D code is called from C code via a function pointer; you have to wonder how the C code received a function pointer in the first place? If the C code is nothrow <a class="gmail_plusreply" id="plusReplyChip-5">@nogc</a>, then it would be impossible to supply a function pointer that was not also nothrow @nogc to the C code in the first place.</div><div> </div></div></div>