D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6816 - [CTFE] nested function can't access this
Summary: [CTFE] nested function can't access this
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-16 00:20 UTC by Martin Nowak
Modified: 2011-11-02 13:07 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2011-10-16 00:20:10 UTC
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 Don 2011-10-16 14:03:28 UTC
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.