Bug 4421 – Union propagates copy constructors and destructors over all members
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-07-04T02:59:50Z
Last change time
2020-11-07T08:34:23Z
Keywords
accepts-invalid, spec
Assigned to
No Owner
Creator
Shin Fujishiro
Comments
Comment #0 by rsinfu — 2010-07-04T02:59:50Z
Copy constructor and destructor are invoked on every field of a union. It must be compile error to define a union containing any object which has a copy-constructor and/or a destructor -- since calling cpctor or dtor on 'non-active members' leads to undefined behavior.
This program compiles:
--------------------
import std.stdio;
void main()
{
U u, v;
v = u;
}
union U
{
R r;
S s;
}
struct R
{
this(this) { writeln("R : this(this)"); }
~this() { writeln("R : ~this()"); }
}
struct S
{
this(this) { writeln("S : this(this)"); }
~this() { writeln("S : ~this()"); }
}
--------------------
And prints this lines:
--------------------
R : this(this)
S : this(this)
S : ~this()
R : ~this()
S : ~this()
R : ~this()
S : ~this()
R : ~this()
--------------------
Comment #1 by github-bugzilla — 2012-01-23T21:24:42Z
Fixed spec to disallow them. It's now a compiler bug that they are accepted.
Comment #3 by maxim — 2013-05-18T13:30:15Z
*** Issue 8576 has been marked as a duplicate of this issue. ***
Comment #4 by maxim — 2013-05-18T13:39:25Z
In the first order unions and structs both belong to aggregate type category. Normally they should share few common characteristics, but currently in D they are very close to each other so that when you use a union, you use a struct except ctor ability and member storage. Following futures are supported by unions:
* postblits
* dtors
* member functions
* operator overloading
* shared static constructors
* invariants
* and other features...
Comment #5 by verylonglogin.reg — 2013-09-30T06:20:43Z
This is an old and easily fixable major accepts-invalid bug. Probably it's just forgotten. What about to fix it?