www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20068] New: Union initialization in constructors should be

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

          Issue ID: 20068
           Summary: Union initialization in constructors should be  safe
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

Currently, initializing a union is  safe, even if that union contains a
pointer:

---
union A
{
    int i;
    int* p;
}

 safe void example(int* p)
{
    A a = { p: p }; // compiles
}
---

However, if a constructor is used to initialize the union, it is considered
 system:

---
union B
{
    int i;
    int* p;
     safe this(int* p)
    {
        // Error: cannot access pointers in  safe code that overlap other
fields
        this.p = p;
    }
}
---

Since the first example is  safe, the second example should be  safe as well.

--
Jul 21 2019