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.
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.
https://github.com/D-Programming-Language/dmd/commit/3055cc6a8b36a2e00c270b0e48e1d1bd2c931f37 https://github.com/D-Programming-Language/dmd/commit/72e750209b370b66157f320d3517c5816a3951d7