Bug 6816 – [CTFE] nested function can't access this
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-10-16T00:20:00Z
Last change time
2011-11-02T13:07:32Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2011-10-16T00:20:10Z
struct S
{
size_t foo()
{
size_t nested()
{
return value + 1;
}
return nested();
}
size_t value;
}
enum s = S().foo();
----
It seems as if checking callers in ThisExp::interpret would work.
while (istate && !istate->localThis)
istate = istate->caller;
Errors for a wrong 'this' will/should be detected during semantic.
Comment #1 by clugdbug — 2011-10-16T14:03:28Z
Another test case, where it's a delegate literal instead of a nested function.
struct S {
size_t foo() {
return (){ return value+1; }();
}
size_t value;
}
enum s = S().foo();
Incidentally if you make 'foo' static, the error message is poor: the message "need this to access member 'value'" is generated in the glue layer (SymbolExp::toElem, in e2ir.c). It should be detected in the semantic pass instead.