Bug 17718 – [scope] function literal arguments can be escaped
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-08-03T18:44:47Z
Last change time
2017-09-09T04:48:47Z
Keywords
safe
Assigned to
No Owner
Creator
Martin Nowak
Comments
Comment #0 by code — 2017-08-03T18:44:47Z
cat > bug.d << CODE
@safe:
struct S
{
int* p;
int* leak() return scope { return p; }
}
alias f = function int*(scope S s) { return s.leak; }; // broken
int* f(scope S s) { return s.leak; } // works
CODE
dmd -c -dip1000 bug
Comment #1 by petar.p.kirov — 2017-08-22T14:46:36Z
If I change the structure to:
struct S
{
int p;
int* leak() return scope { return &p; }
}
I get:
scope_bug17718.d(9): Error: returning s.leak() escapes a reference to parameter s, perhaps annotate with return
Where exactly is one supposed to add return?
Comment #2 by petar.p.kirov — 2017-08-22T14:49:12Z
(Where line 9 is the `alias f = ...` same as in the original example.)
Comment #3 by bugzilla — 2017-09-09T04:48:47Z
What's happening here is that the function literal f is getting its attributes inferred, so 'return scope' is inferred. The explicit function f is not getting its attributes inferred, the parameter is 'scope', not 'return scope', and so it errors.
If the latter is changed to:
auto f(scope S s) { return s.leak; }
then no error occurs, because attribute inference is done for auto functions.
The compiler is working correctly.