Bug 15938 – ctor doesn't initialize fields when struct is static immutable

Status
NEW
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-04-18T14:42:20Z
Last change time
2024-12-13T18:47:32Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Temtaime
Moved to GitHub: dmd#17753 →

Comments

Comment #0 by temtaime — 2016-04-18T14:42:20Z
struct S { static foo() { int[1][1] res = 1; return res; } union { int[1][1] A = foo; int[1] flat; } this(A...)(auto ref in A args) { uint k; foreach(v; args) flat[k++] = v; } } static immutable s = S(2); void main() { assert(s.flat[0] == 2); // fails assert(S(2).flat[0] == 2); }
Comment #1 by mathias.lang — 2016-04-18T15:10:26Z
Comment #2 by john.loughran.colvin — 2016-09-21T23:04:04Z
Simplified example: struct S { union { // works if order swapped int b; int[1] a; } this(int v) { a[0] = v; } } auto s = S(2); void main() { assert(s.a[0] == 2); // fails assert(S(2).a[0] == 2); // OK }
Comment #3 by razvan.nitu1305 — 2022-03-24T15:23:58Z
(In reply to John Colvin from comment #2) > Simplified example: > > struct S { > union { > // works if order swapped > int b; > int[1] a; > } > > this(int v) { > a[0] = v; > } > } > auto s = S(2); > > void main() { > assert(s.a[0] == 2); // fails > assert(S(2).a[0] == 2); // OK > } It seems that ctfe rewrites `auto s = S(2)` to `auto s = S(0, [2)`. So it does not seem to understand that a and b are inside a union and therefore only the first initializer (0) will be taken into account.
Comment #4 by robert.schadek — 2024-12-13T18:47:32Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17753 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB