www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21561] New: Unsafe aliasing of immutable union member allowed

https://issues.dlang.org/show_bug.cgi?id=21561

          Issue ID: 21561
           Summary: Unsafe aliasing of immutable union member allowed in
                     safe code
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

Example program:

---
union U
{ 
    int x;
    immutable int y;
}

void example()  safe
{
    U a = { x: 0 };
    int n = a.y;
}
---

Access to `a.y` should be forbidden in  safe code according the the language
spec's definition of safe aliasing [1], which permits aliasing between types
with different mutability qualifiers only when

 * both types are const or immutable; or
 * one of the types is mutable while the other is a const-qualified basic data
   type
[1] https://dlang.org/spec/function.html#safe-aliasing --
Jan 20 2021