Bug 8902 – Unexpected "duplicate union initialization for X" error
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-28T00:12:00Z
Last change time
2013-03-05T08:32:12Z
Keywords
rejects-valid
Assigned to
nobody
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2012-10-28T00:12:15Z
Should assigning `.init` to a union be allowed or disallowed? At leas current behavior is inconsistent:
---
union U { int a, b; }
enum U u0 = U.init; // No errors
U u1; // No errors
U u2 = U.init; // Error: duplicate union initialization for b
void main()
{
U u3 = U.init; // No errors
immutable U u4 = U.init; // No errors
immutable static U u5 = U.init; // Error: duplicate union...
static U u6 = u4; // Error: duplicate union...
static U u7 = U.init; // Error: duplicate union...
}
---
Comment #1 by monarchdodra — 2012-10-28T01:06:44Z
(In reply to comment #0)
> Should assigning `.init` to a union be allowed or disallowed? At leas current
> behavior is inconsistent:
>
> ---
> union U { int a, b; }
>
> enum U u0 = U.init; // No errors
> U u1; // No errors
> U u2 = U.init; // Error: duplicate union initialization for b
>
> void main()
> {
> U u3 = U.init; // No errors
> immutable U u4 = U.init; // No errors
> immutable static U u5 = U.init; // Error: duplicate union...
> static U u6 = u4; // Error: duplicate union...
> static U u7 = U.init; // Error: duplicate union...
> }
> ---
It would further more appear that the compiler has trouble detecting this in conditional implementations, which makes it difficult to bypass this problem.
//----
import std.stdio;
union U { int a, b; }
void main()
{
static if (is(typeof((inout int _dummy=0){static U i = U.init;}))) //FINE
{
static U i = U.init; //L9: DERP
}
}
//----
main.d(9): Error: duplicate union initialization for b
//----
Raising priority due to the impossibility to easily bypass this problem...
Comment #2 by verylonglogin.reg — 2012-10-28T02:12:11Z
(In reply to comment #1)
> (In reply to comment #0)
> It would further more appear that the compiler has trouble detecting this in
> conditional implementations, which makes it difficult to bypass this problem.
Possible this is related to an error gagging problem. E.g. consider this:
---
union U { int a, b; }
template t(T)
{ static T t = T.init; } // Error: duplicate union initialization for b
static if (__traits(compiles, t!U)) { }
---
Comment #3 by github-bugzilla — 2012-12-12T07:41:20Z