D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4256 - Inner template mathods can't access this pointer
Summary: Inner template mathods can't access this pointer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-05-31 05:52 UTC by Simen Kjaeraas
Modified: 2015-06-09 05:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2010-05-31 05:52:19 UTC
struct bar {
    int n;
    void baz( ) {
        void qux(T)() {
            n = 3;
        }
        qux!int();
    }
}

The above code barfs on 'n = 3;': "Error: need 'this' to access member n".
Comment 1 Simen Kjaeraas 2011-09-16 08:48:05 UTC
This bug has grown worse. A lot worse. Good news: this now compiles:

struct foo {
    int n;
    void baz( ) {
        void qux(T)() {
            n = 3;
            assert(n == 3); // Passes
        }
        qux!int();
    }
}

void main( ) {
    foo b;
    assert(b.n == 0); // Passes
    b.baz();
    assert(b.n == 3); // Fails
}

As we can see, the change in n is not properly propagated. What's even worse, is this:

struct foo {
    int n;
    void baz( ) {
        void qux(T)(void* a) { // Added parameter
            n = 3;
            assert(n == 3); // Passes
            assert(a == &this); // Added.
        }
        qux!int(&this);
    }
}

void main( ) {
    foo b;
    assert(b.n == 0); // Passes
    b.baz();
    assert(b.n == 3); // No longer fails.
}

You can then remove the assert on line 7 (a == &this) to get the old behavior back, the additional parameter changes nothing on its own.