Thread overview
[Issue 18478] Spurious "escapes a reference to local variable" error in function that does not return by reference
Mar 03, 2018
Walter Bright
Mar 15, 2018
Carsten Blüggel
Mar 16, 2018
Carsten Blüggel
Mar 16, 2018
Carsten Blüggel
Mar 20, 2018
Walter Bright
Mar 20, 2018
Walter Bright
Mar 20, 2018
jpdutoit@gmail.com
Mar 20, 2018
Carsten Blüggel
Mar 22, 2018
Carsten Blüggel
March 03, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
                 CC|                            |bugzilla@digitalmars.com

--
March 15, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chilli@posteo.net

--
March 16, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |18444


Referenced Issues:

https://issues.dlang.org/show_bug.cgi?id=18444
[Issue 18444] [DIP25][DIP1000] Tracking issue for: "The implementation doesn't
match DIPs 25/1000"
--
March 16, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid, spec

--- Comment #1 from Carsten Blüggel <chilli@posteo.net> ---
Another error example from phobos that appeared in master within the last ~24h: https://github.com/dlang/phobos/blob/master/std/exception.d

make -f posix.mak std/exception.test (-dip1000 for that module; DMD64 master)

std/exception.d(1243): Error: returning & i escapes a reference to local
variable i

excerpt from std/exception.d:

/// Arrays (dynamic and static)
@system unittest
{
    int i;
    int[]  slice = [0, 1, 2, 3, 4];
    int[5] arr   = [0, 1, 2, 3, 4];
    int*[]  slicep = [&i];               <= line 1243
    int*[1] arrp   = [&i];
...
    assert( slicep[0].doesPointTo(i));   <= slicep usage
    assert(!slicep   .doesPointTo(i));   <= slicep usage
...

1. This is @system code, allowed to escape whatever
2. There is no escape (commenting out slicep usages doesn't change error
reported)

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to jpdutoit from comment #0)
> 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.

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.

> Some variations:
> 
> - Removing the return attribute in fn1_normal's parameter, removes "Error 1"

Right, because then the compiler thinks it isn't being returned.

> - Compiling with -dip1000 removes "Error 2".

Right, because it infers the 'return' attribute for templates.

> But then the return attribute in fn1_normal is required.

Right, because it checks the function body against the attributes.

The compiler is behaving as expected.

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to Carsten Blüggel from comment #1)
> Another error example from phobos that appeared in master within the last ~24h:

I don't really understand what you mean. In any case, it does not seem to be the same thing as in the first comment. Please clarify and file as a separate bugzilla issue.

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

--- Comment #4 from jpdutoit@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.

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

--- Comment #5 from Carsten Blüggel <chilli@posteo.net> ---
(In reply to Walter Bright from comment #3)
> (In reply to Carsten Blüggel from comment #1)
> > Another error example from phobos that appeared in master within the last ~24h:
> 
> I don't really understand what you mean. In any case, it does not seem to be the same thing as in the first comment. Please clarify and file as a separate bugzilla issue.

I hope, that I expressed my concern better in new separate https://issues.dlang.org/show_bug.cgi?id=18637

--
March 22, 2018
https://issues.dlang.org/show_bug.cgi?id=18478

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|18444                       |


Referenced Issues:

https://issues.dlang.org/show_bug.cgi?id=18444
[Issue 18444] [DIP25][DIP1000] Tracking issue for: "The implementation doesn't
match DIPs 25/1000"
--