[Issue 3065] error: this for variable needs to be Type not Type!(arguments).Type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Feb 12 10:34:44 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=3065


siegelords_abode at yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |siegelords_abode at yahoo.com


--- Comment #3 from siegelords_abode at yahoo.com 2011-02-12 10:32:13 PST ---
I've analyzed this error and I think I know why it happens. Here's a simple
test case that reproduces the error:

class A(T)
{
    this()
    {
        var = 5;
        cause_error;
    }

    int var;
}

void F(T)()
{
    auto a = new A!(T);
}

void main()
{
    F!(int)();
}

bug.d(5): Error: this for var needs to be type A not type bug.A!(int).A
bug.d(6): Error: undefined identifier cause_error
bug.d(14): Error: template instance bug.A!(int) error instantiating
    instantiatied in bug.d(19): F!(int)

That cause_error is necessary, because the way this bug occurs is when the
template fails to instantiate the first time, and then is instantiated again at
a later point. Note that this bug occurs in my actual program without the
unrelated error, I just couldn't get a better test case. This bug is similar to
the bug 5046, although in my program you need to do what the above poster
suggested: explicit this does NOT work.

Anyway, the reason this bug occurs is that when a template is instantiated the
first time and it encounters an error, it aborts instantiation and the calling
code tries to instantiate it later (this test case invokes this mechanism in
expression.c:6293). Unfortunately, while instantiating the first time it
registers the types in the string table. When the template is instantiated
again, the template contents are syntax copied (in
TemplateInstance::semantic()) and when analyzed, the newly copied types have
different pointers than the ones in the string table, confusing the subsequent
this resolution code.

Firstly, let me say that this deferred instantiation of a template looks like a
giant hack, since from the code it looks like it's a memory leak.

Anyway, to fix it I tried the following things:

To determine whether this is of the same type or not I tried to compare it by
ClassDeclaration::type instead of ClassDeclaration itself. This didn't work for
some reason (although it did fix the test case, it broke my actual project).

I also tried to copy the string table at the beginning of
TemplateInstance::semantic and restore it if the semantic pass failed, but that
didn't work either (although it did fix the test case, it broke my actual
project in a slightly different way).

Anyway... any other ideas of how to fix this?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list