Bug 12871 – inner function returning pointer to outer context local rejected
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2014-06-06T18:21:00Z
Last change time
2015-02-21T15:41:06Z
Keywords
rejects-valid
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2014-06-06T18:21:45Z
When writing an inner function, or lambda, that will not escape a local for that function's scope, but instead escapes a local for the enclosing scope, the compiler rejects it with the message "Error: escaping reference to local"
Example:
void foo()
{
int a;
int *dg(){return &a;}
}
Clearly, a will not escape its scope through dg. Such a function may be used to modularize parts of the function, and so should not be flagged as invalid. If the delegate is returned, it should create a closure anyway.
Such constructs are very important to get right because the expected way to apply certain attributes such as @trusted to an expression inside a @safe function are via a lambda expression.