D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11147 - Nested structs in a union are not correctly initialized
Summary: Nested structs in a union are not correctly initialized
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: industry, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2013-09-30 01:43 UTC by badlink
Modified: 2013-09-30 11:33 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description badlink 2013-09-30 01:43:43 UTC
DMD version: 2.063.2

The value of all variables in V should be zero, but the output shows they aren't.

---------------
struct V {
    union {
        struct {
            float x = 1;
            float y = 1;
	    float z = 1;
        }
        struct {
            float r;
            float g;
	    float b;
        }
    }
}
import std.stdio;
void main() {
    writeln("V(", V.init.x, ", ", V.init.y, ", ", V.init.z, ", ", V.init.r, ", ", V.init.g, ", ", V.init.b, ")");
    writeln(V.init);
    writeln(V(V.init.x, V.init.y, V.init.z));
}
---------------

V(0, 0, 0, 0, 0, 0)
V(0, nan, nan, 0, nan, nan)
V(0, 0, 0, 0, 0, 0)
Comment 1 badlink 2013-09-30 01:47:05 UTC
> struct V {
>     union {
>         struct {
>             float x = 1;
>             float y = 1;
>         float z = 1;
>         }
>         struct {
>             float r;
>             float g;
>         float b;
>         }
>     }
> }

I posted the wrong code, the correct one is:
struct V {
    union {
        struct {
            float x = 0;
            float y = 0;
	    	float z = 0;
        }
        struct {
            float r;
            float g;
	    	float b;
        }
    }
}
Comment 2 monarchdodra 2013-09-30 03:36:34 UTC
I wanted to add that this:

void main() {
  assert(V.init is V());
}

It would *appear* that the compiler is confused as to *what* the initial state of V is.
Comment 4 github-bugzilla 2013-09-30 11:33:03 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/a8d1d0911a10e2b941aba2a4de56c5a8b3f8dcea
fix Issue 11147 - Nested structs in a union are not correctly initialized

https://github.com/D-Programming-Language/dmd/commit/6e8d1f888c6d8e159e5145b5649252708976fb22
Merge pull request #2603 from 9rnsr/fix11147

Issue 11147 - Nested structs in a union are not correctly initialized