dmd/compiler/test/compilable/test23812.d
Tim Schendekehl 0e8e67097d
Fix bugzilla 23812 - ImportC: allow adding function attributes to imported C functions (#16820)
This adds a new pragma for ImportC, which allows to set default storage
classes. Only `nothrow`, `@nogc` and `pure` are supported for now.
They can be disabled later using `#pragma attribute(pop)`.

Unknown storage classes are ignored.
2024-09-14 10:58:00 -07:00

75 lines
1.8 KiB
D

// EXTRA_FILES: imports/imp23812.c
import imports.imp23812;
void callDefault()
{
funcDefault();
funcDefault2();
}
static assert(!__traits(compiles, () nothrow { funcDefault(); } ));
static assert(!__traits(compiles, () @nogc { funcDefault(); } ));
static assert(!__traits(compiles, () pure { funcDefault(); } ));
static assert(!__traits(compiles, () nothrow { funcDefault2(); } ));
static assert(!__traits(compiles, () @nogc { funcDefault2(); } ));
static assert(!__traits(compiles, () pure { funcDefault2(); } ));
void callNothrow() nothrow
{
funcNothrow();
funcNothrow2();
}
static assert(!__traits(compiles, () @nogc { funcNothrow(); } ));
static assert(!__traits(compiles, () pure { funcNothrow(); } ));
static assert(!__traits(compiles, () @nogc { funcNothrow2(); } ));
static assert(!__traits(compiles, () pure { funcNothrow2(); } ));
void callNogc() @nogc
{
funcNogc();
}
static assert(!__traits(compiles, () nothrow { funcNogc(); } ));
static assert(!__traits(compiles, () pure { funcNogc(); } ));
void callPure() pure
{
funcPure();
}
static assert(!__traits(compiles, () nothrow { funcPure(); } ));
static assert(!__traits(compiles, () @nogc { funcPure(); } ));
void callNothrowNogc() nothrow @nogc
{
funcNothrowNogc();
funcNothrowNogc2();
}
static assert(!__traits(compiles, () pure { funcNothrowNogc(); } ));
static assert(!__traits(compiles, () pure { funcNothrowNogc2(); } ));
extern(C) void callbackDefault()
{
}
extern(C) void callbackNothrow() nothrow
{
}
void callFuncWithCallback() nothrow
{
funcWithCallback(&callbackNothrow);
Callbacks callbacks;
callbacks.f = &callbackNothrow;
}
static assert(!__traits(compiles, () { funcWithCallback(&callbackDefault); } ));
static assert(!__traits(compiles, () { Callbacks callbacks; callbacks.f = &callbackDefault; } ));