[Issue 19908] New: [DIP1000] union with single member should not generate cannot access pointers in `@safe` code that overlap other fields error

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 28 14:15:28 UTC 2019


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

          Issue ID: 19908
           Summary: [DIP1000] union with single member should not generate
                    cannot access pointers in `@safe` code that overlap
                    other fields error
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: iamthewilsonator at hotmail.com

Unions with a single element (such as where they are used purely to suppress
calling constructors and destructors) should not generate errors related to
accessing overlapped pointers in @safe code since no overlapped access can
occur.

struct Nullable(T)
{
    private bool _isNull = true;
    private union DontCallDestructorT
    {
         T payload;
    }
    private DontCallDestructorT _value = DontCallDestructorT.init;

    @property ref inout(T) get() inout @safe pure nothrow
    {                {
        enum message = "Called `get' on null Nullable!" ~ T.stringof ~ ".";     
        assert(!isNull, message);
        return _value.payload; // <<<
    }
}

std/typecons.d(2972): Error: field `DontCallDestructorT.payload` cannot access
pointers in `@safe` code that overlap other fields

uncovered by https://github.com/dlang/dmd/pull/9909

--


More information about the Digitalmars-d-bugs mailing list