www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19908] New: [DIP1000] union with single member should not

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 puremagic.com
          Reporter: iamthewilsonator 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

--
May 28 2019