Bug 14140 – Bad codegen for CTFE union initialisers for immutable structs

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-02-07T12:25:00Z
Last change time
2015-09-13T08:35:50Z
Keywords
CTFE, pull, wrong-code
Assigned to
nobody
Creator
temtaime

Comments

Comment #0 by temtaime — 2015-02-07T12:25:28Z
Comment #1 by temtaime — 2015-02-07T12:25:43Z
This bug exists with GIT HEAD too.
Comment #2 by ketmar — 2015-02-08T08:26:20Z
actually, this is not a default argument passing bug, this is "CTFE union initializer bug": import std.stdio; import std.string; struct Vector3 { union { float[3][1] A; float[3] flat; } string toString() const { return format(`[%( %s %) ]`, flat); } this(in float[] args…) { flat[] = args[]; } } immutable AXIS_Y_1 = Vector3(0, 1, 0); void main() { auto n = AXIS_Y_1; writeln(n); } output: [ nan nan nan ] import std.stdio; import std.string; struct Vector3 { float[3] flat; string toString() const { return format(`[%( %s %) ]`, flat); } this(in float[] args…) { flat[] = args[]; } } immutable AXIS_Y_1 = Vector3(0, 1, 0); void main() { auto n = AXIS_Y_1; writeln(n); } output: [ 0 1 0 ]
Comment #3 by ketmar — 2015-02-08T08:28:32Z
and, hehehe: import std.stdio; import std.string; struct Vector3 { union { float[3] flat; float[3][1] A; } string toString() const { return format(`[%( %s %) ]`, flat); } this(in float[] args…) { flat[] = args[]; } } immutable AXIS_Y_1 = Vector3(0, 1, 0); void main() { auto n = AXIS_Y_1; writeln(n); } output: [ 0 1 0 ] so, compiler just unable to see that `A` and `flat` occupies the same space, and takes the default values for the first member of the union. for `A` they are all nans. and for `flat` they are set by CTFE constructor.
Comment #4 by k.hara.pg — 2015-09-07T07:54:25Z
Comment #5 by github-bugzilla — 2015-09-13T08:35:49Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/c8c56038c1876b24a499e6728f8ae9c2088a80da fix Issue 14140 - Bad codegen for CTFE union initialisers for immutable structs https://github.com/D-Programming-Language/dmd/commit/16969dcd711c82a49acf1f0afdc561fe83b5c859 Merge pull request #5050 from 9rnsr/fix14140 Issue 14140 - Bad codegen for CTFE union initialisers for immutable structs