Bug 23054 – importC: struct compound-literal assigned by pointer has wrong storage duration

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-04-24T23:06:47Z
Last change time
2022-05-14T09:07:07Z
Keywords
ImportC, pull, wrong-code
Assigned to
No Owner
Creator
duser

Comments

Comment #0 by duser — 2022-04-24T23:06:47Z
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)
Comment #1 by bugzilla — 2022-05-12T07:05:10Z
test1: I didn't realize that 6.5.2.5 Compound Literals could be non-static. This will require some significant work to implement.
Comment #2 by bugzilla — 2022-05-12T19:55:22Z
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.
Comment #3 by dlang-bot — 2022-05-12T20:01:37Z
@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
Comment #4 by dlang-bot — 2022-05-14T09:07:07Z
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