[Issue 18478] Spurious "escapes a reference to local variable" error in function that does not return by reference

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 20 07:46:49 UTC 2018


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

--- Comment #4 from jpdutoit at gmail.com ---

(In reply to Walter Bright from comment #2)
> The error is on the function returning the reference, not the function using
> (or not using) the return value. Because of separate compilation, it has to
> be this way.

I assume you are speaking about fn2.

Storing the result to a temporary before returning removes the error:

auto tmp = fn1_normal(a);
return tmp;

I would expect the compiler to implicitly decay the returned reference to a
normal value when it sees that fn2 does not return by reference, the same way
it is done when first saving it into a temporary.

In any case, the error "returning fn1_normal(a) escapes a reference to local
variable a" does not describe what is actually going on, because no reference
would actually escape even if this error was disabled.

--


More information about the Digitalmars-d-bugs mailing list