Bug 13537 – Unions may break immutability

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-09-27T01:44:00Z
Last change time
2016-10-01T11:48:21Z
Keywords
accepts-invalid, pull, safe
Assigned to
nobody
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2014-09-27T01:44:34Z
------ void fun() @safe { union U { immutable int x; int y; } U u; u.y = 1; assert(u.x == 1); u.y = 2; assert(u.x == 2); // look ma! I broke immutability! } ------
Comment #1 by hsteoh — 2014-10-02T03:41:30Z
Basically, immutable cannot be allowed to overlap with anything mutable, otherwise there will be a way to break the immutability guarantee.
Comment #2 by hsteoh — 2016-02-19T00:30:55Z
Unfortunately, fixing this may prove to be a challenge, because std.typecons.Rebindable uses a union of (possibly) immutable and mutable fields in order to achieve rebindability. Arguably, though, some kind of explicit cast ought to be required in that case. Either that, or we impose the non-overlapping restriction only in @safe code, so that @system code can continue using unions to do black magic with immutability.
Comment #3 by hsteoh — 2016-02-19T17:33:29Z
Comment #4 by bugzilla — 2016-07-17T11:50:38Z
Comment #5 by github-bugzilla — 2016-07-19T20:43:26Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/09ed87c973fbcb777806506f7e05fbed30a3f355 fix Issue 13537 - Unions may break immutability https://github.com/dlang/dmd/commit/72077372aec4aa77899885fc5cffbdbc5d289021 Merge pull request #5940 from WalterBright/fix13537 fix Issue 13537 - Unions may break immutability
Comment #6 by github-bugzilla — 2016-10-01T11:48:21Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/09ed87c973fbcb777806506f7e05fbed30a3f355 fix Issue 13537 - Unions may break immutability https://github.com/dlang/dmd/commit/72077372aec4aa77899885fc5cffbdbc5d289021 Merge pull request #5940 from WalterBright/fix13537