ImportC and nothrow/@nogc?

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 28 02:21:02 UTC 2024


On Tuesday, 27 August 2024 at 22:22:12 UTC, Walter Bright wrote:
> On 8/26/2024 10:26 AM, Steven Schveighoffer wrote:
>> 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.
>
> I know everything there is to know about printf. I've 
> implemented a Standard compliant one :-)

If you are arguing it can possibly use the GC or throw an 
exception, then I'd like to hear more about how the standard 
allows that!

> I've also proposed an exemption to make printf usable from 
> @safe code.

`printf` is already usable from `@safe` code through 
interpolation overloads.

>> This must be fixed. There is no other option. If this is not 
>> fixed, you cannot use importC to actually import C.
>
> I remember when you were adamantly against C code defaulting to 
> @trusted.

I still am.

> Assuming nothrow and @nogc is in the same category.

It's not. C doesn't have a GC nor does it have an ability to 
throw D exceptions, whereas it can possibly have a safe interface.

But this isn't even close to the same problem anyway -- what we 
were talking about with "make C prototypes safe by default" were 
*`extern(C)` prototypes written in D*. That is, these weren't in 
the C language, which has no concept of marking for any D 
attributes. It is a bad default, and probably just kills D as a 
memory safe language, but it could at least be worked around if 
you cared enough.

The problem here isn't the default, the problem is the inability 
to mark because the language doesn't support it.

While making the default `@nogc nothrow` is at least consistent 
with C (as is making C functions `@system`), a problem with that 
approach is that unlike `@system`, there is no attribute to 
cancel `@nogc` or `nothrow`. So if we made the default attributes 
for importC code `nothrow @nogc`, then there would be no way to 
cancel that default.

> I agree this is a problem.

Yes, it is causing problems with people who want to write correct 
attributes in their D code. As I linked to in my project, I have 
to leave my D code that does not throw or use the GC unmarked, 
because of `printf` from importC. This makes importC unusable 
unless you just don't care about any attributes.

-Steve


More information about the Digitalmars-d mailing list