[Issue 17174] New: auto ref allows pointers to rvalues

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Feb 10 09:48:09 PST 2017


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 at puremagic.com
          Reporter: sascha.orlov at 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.

--


More information about the Digitalmars-d-bugs mailing list