Issue 13537 - Unions may break immutability
Summary: Unions may break immutability
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, pull, safe
Depends on:
Blocks:
 
Reported: 2014-09-27 01:44 UTC by hsteoh
Modified: 2016-10-01 11:48 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2014-09-27 01:44:34 UTC
------
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 hsteoh 2014-10-02 03:41:30 UTC
Basically, immutable cannot be allowed to overlap with anything mutable, otherwise there will be a way to break the immutability guarantee.
Comment 2 hsteoh 2016-02-19 00:30:55 UTC
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 4 Walter Bright 2016-07-17 11:50:38 UTC
Rebooted as:

https://github.com/dlang/dmd/pull/5940
Comment 5 github-bugzilla 2016-07-19 20:43:26 UTC
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 github-bugzilla 2016-10-01 11:48:21 UTC
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