[Issue 20068] New: Union initialization in constructors should be @safe

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 21 15:44:23 UTC 2019


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 at puremagic.com
          Reporter: snarwin+bugzilla at 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.

--


More information about the Digitalmars-d-bugs mailing list