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
Comment #2 by bugzilla — 2012-01-23T21:25:53Z
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?
Comment #6 by bugzilla — 2014-11-15T21:48:48Z
Comment #7 by bugzilla — 2014-11-15T23:15:20Z
Comment #8 by github-bugzilla — 2014-11-16T23:37:58Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/47abaf60e7960fffe56612c0f0c8ce91c08b3939 fix Issue 4421 - Union propagates copy constructors and destructors over all members https://github.com/D-Programming-Language/dmd/commit/ec674671879999a983c4e71b351acae90f249b20 Merge pull request #4144 from WalterBright/fix4421 fix Issue 4421 - Union propagates copy constructors and destructors over all members
Comment #9 by github-bugzilla — 2015-02-18T03:39:33Z
Commits pushed to 2.067 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/47abaf60e7960fffe56612c0f0c8ce91c08b3939 fix Issue 4421 - Union propagates copy constructors and destructors over all members https://github.com/D-Programming-Language/dmd/commit/ec674671879999a983c4e71b351acae90f249b20 Merge pull request #4144 from WalterBright/fix4421
Comment #10 by ilyayaroshenko — 2020-11-07T08:34:23Z