test program: struct S { int x; }; int test1(int x) { // compile error because it's static (but currently this segfaults when compiling) // using a local variable assigned to x, it gives "Error: non-constant expression `y = x`" struct S *b = &(struct S){x}; return 0; } int test2(int x) { struct S *s = &(struct S){0}; s->x = x; if (x != 0) { test2(0); // detect if it's static instead of automatic // the recursive call shouldn't have affected this copy if (s->x != x) return 2; } return 0; } int test3(void) { // detect if it's GC instead of static or automatic // this is true in CTFE void *prev; for (int i = 0; i < 2; i++) { void *curr = &(struct S){0}; // should have the same address on both loop iterations if (i == 0) prev = curr; else if (curr != prev) return 3; } return 0; } _Static_assert(test1(1) == 0, "1"); _Static_assert(test2(1) == 0, "2"); _Static_assert(test3() == 0, "3"); // fails int main() { int rv; if (rv = test1(1)) return rv; // function fails to compile if (rv = test2(1)) return rv; // fails if (rv = test3()) return rv; return 0; } "&(struct S){0}" here should work like taking the address of a local variable outside CTFE, it works like a static variable, in CTFE it's allocated using GC (which is also wrong)
test1: I didn't realize that 6.5.2.5 Compound Literals could be non-static. This will require some significant work to implement.
test3 is checking the location of the temporary. C11 does not specify the location. Whether the address matches or not in the scope in the loop does not matter, as the lifetime of the temporary does not survive the scope. In any case, the temporary is allocated on the stack, not statically and not on the GC heap.
@WalterBright created dlang/dmd pull request #14118 "fix Issue 23054 - importC: struct compound-literal assigned by pointe…" fixing this issue: - fix Issue 23054 - importC: struct compound-literal assigned by pointer has wrong storage duration https://github.com/dlang/dmd/pull/14118
dlang/dmd pull request #14118 "fix Issue 23054 - importC: struct compound-literal assigned by pointe…" was merged into master: - 2b9ee9918bd4b8d160622df662413111bdb841ed by Walter Bright: fix Issue 23054 - importC: struct compound-literal assigned by pointer has wrong storage duration https://github.com/dlang/dmd/pull/14118