Thread overview
[Issue 21561] Unsafe aliasing of immutable union member allowed in @safe code
Jan 20, 2021
Paul Backus
Jan 20, 2021
Chloé
Jan 20, 2021
Bolpat
Jan 20, 2021
Paul Backus
Feb 27, 2021
anonymous4
Feb 27, 2021
anonymous4
Dec 17, 2022
Iain Buclaw
January 20, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe

--
January 20, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

Chloé <chloekek@use.startmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chloekek@use.startmail.com

--- Comment #1 from Chloé <chloekek@use.startmail.com> ---
Note that assigning to the a.x is still prohibited:

---
Error: field `U.x` cannot modify fields in `@safe` code that overlap fields with other storage classes
---

With that in place, is it necessary to also prohibit accessing the field?

--
January 20, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

Bolpat <qs.il.paperinik@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qs.il.paperinik@gmail.com
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--
January 20, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

--- Comment #2 from Paul Backus <snarwin+bugzilla@gmail.com> ---
Yes, because the data can still be modified by assigning a new value to the whole union.

---
void oops()
{
    U a = { y: 0 };
    U b = { x: 1 };
    assert(a.y == 0);
    a = b;
    assert(a.y == 1);
}
---

Strictly speaking, the problem is that we are allowed to access a.y after its lifetime has ended. However, this only leads to undefined behavior when unsafe aliasing is involved, so the unsafe aliasing is what needs to be fixed.

--
February 27, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

anonymous4 <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=19916

--
February 27, 2021
https://issues.dlang.org/show_bug.cgi?id=21561

anonymous4 <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |19916
           See Also|https://issues.dlang.org/sh |
                   |ow_bug.cgi?id=19916         |


Referenced Issues:

https://issues.dlang.org/show_bug.cgi?id=19916
[Issue 19916] union member access should be un-@safe
--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=21561

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--