www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17174] New: auto ref allows pointers to rvalues

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

          Issue ID: 17174
           Summary: auto ref allows pointers to rvalues
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: sascha.orlov gmail.com

As discussed here: 
https://forum.dlang.org/post/uxznyrmmtptmzucjcdzg forum.dlang.org

tested with 2.073.0 on a mac. 

// Code starts here
void main()
{
    initStruct iSb;
    iSb.var = 3;
    A b = A(iSb);
    assert(*b.myVar == 3); // this works
    iSb.var = 4;
    assert(*b.myVar == 4); // as expected

    b = A(initStruct(5)); // how does
    assert(*b.myVar == 5); // this work?
}

struct A
{
     disable this();
    size_t* myVar;
    this()(auto ref initStruct iS)  nogc
    {
        import core.stdc.stdio;
        __traits(isRef, iS) ? printf("ref case\n") : printf("value case");

        myVar = &iS.var;

        /* // This treatment is not needed?
        if(__traits(isRef, iS))
        {
            myVar = &iS.var;
        }
        else
        {
            myVar = new size_t(iS.var);
        }
        */    
    }
}

struct initStruct
{
    size_t var;
}
// Code ends here

In case of initializing an A-instance with an rvalue, it should be a
compile-time error to get a pointer to the parameter.

--
Feb 10