www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19507] New: auto ref infers lvalue for member of rvalue

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

          Issue ID: 19507
           Summary: auto ref infers lvalue for member of rvalue
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: atila.neves gmail.com

The code below manages to bind an rvalue to a ref parameter due to wrong `auto
ref` inference:

----------------
void main() {
    Foo foo;
    fun(foo.create);
    fun(foo.create.i);
}

struct Foo {
    int i;
    Foo create() {
        return Foo();
    }
}

void fun(T)(auto ref T value) {
    import std.stdio;
    writeln("T: ", T.stringof, "   ref? ", __traits(isRef, value));
}
----------------

This prints:
T: Foo   ref? false
T: int   ref? true


In the first case, it correctly sees that `create` returns a new value and
infers it's an rvalue, so `auto ref` is not `ref.

But in the second case it reverts to `ref`, despite it being a subpart of an
rvalue!

--
Dec 22 2018