www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18478] New: Spurious "escapes a reference to local variable"

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

          Issue ID: 18478
           Summary: Spurious "escapes a reference to local variable" error
                    in function that does not return by reference
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jpdutoit gmail.com

DMD: 2.08.3

When returning the result of a function that returns by reference from a
function that does not return by reference, some "escapes a reference to local
variable" errors are reported. 

In the code below, since fn2 is not returning by reference, it should not
matter that fn1_normal and fn1_template is returning by reference.

Some variations:

- Removing the return attribute in fn1_normal's parameter, removes "Error 1"

- Compiling with -dip1000 removes "Error 2". But then the return attribute in
fn1_normal is required.


https://run.dlang.io/gist/270890860f98760ab042126791f29bd8?compiler=dmd

----​
class A { }
​
A fn1_normal(return ref A a) {

    return a;
}
​
ref A fn1_template()(ref A a) {
    return a;
}
​
A fn2() {
    A a = new A();
    if (true) {
        // Error 1: returning fn1_normal(a) escapes a reference to local
variable a
        return fn1_normal(a);
    } else {
        // Error 2: returning fn1_template(a) escapes a reference to local
variable 
        return fn1_template(a);
    }
}
​
void main() {
    fn2();
}

--
Feb 20 2018