Bug 19507 – auto ref infers lvalue for member of rvalue
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-12-22T16:21:50Z
Last change time
2019-10-28T09:03:33Z
Assigned to
No Owner
Creator
Atila Neves
Comments
Comment #0 by atila.neves — 2018-12-22T16:21:50Z
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!
Comment #1 by atila.neves — 2018-12-22T16:24:25Z
This also compiles and shouldn't:
--------------------
void main() {
Foo foo;
takesRef(foo.create.i); // oops
}
struct Foo {
int i;
Foo create() {
return Foo();
}
}
void takesRef(ref int x) { }
--------------------