D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6439 - [CTFE] union fields are initialized independently
Summary: [CTFE] union fields are initialized independently
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords: CTFE, wrong-code
Depends on:
Blocks:
 
Reported: 2011-08-05 05:18 UTC by Dmitry Olshansky
Modified: 2013-11-16 02:05 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 Dmitry Olshansky 2011-08-05 05:18:59 UTC
struct A
{  
    this(uint a, uint b)
    {
	begin = a;
	end = b;
    }
    union
    {
        struct
        {
            uint begin, end;
        }
	uint[2] arr;
    }    
}

void main(){
	enum y = A(10, 20);
	A y2 = A(10, 20); 
	assert(y2.begin == y.begin && y2.end == y.end);  //passes
	assert(y.arr == [0,0]);  //passes, wtf ?!
	assert(y.arr == y2.arr); //fails
}

I dunno what was the last stance on unions in CTFE, but this bug also affects constfolded globals (which is tricky and nasty). So at minimum it's better to just reject it.
Comment 1 github-bugzilla 2013-11-16 00:59:27 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/adb94ddfaf388505a0112539c7df3edada2d5435
fix Issue 6439 - [CTFE] union fields are initialized independently
Comment 2 Kenji Hara 2013-11-16 02:05:09 UTC
Worksforme from 2.064.