Skip to content

Commit

Permalink
Merge pull request #475 from 9rnsr/fix_typeof_super
Browse files Browse the repository at this point in the history
Issue 6847 & 6848 - Fix special cases of typeof(super)
  • Loading branch information
WalterBright committed Nov 2, 2011
2 parents 35ee32c + ab1f51b commit 85549f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/expression.c
Expand Up @@ -2915,8 +2915,7 @@ SuperExp::SuperExp(Loc loc)
}

Expression *SuperExp::semantic(Scope *sc)
{ FuncDeclaration *fd;
FuncDeclaration *fdthis;
{
ClassDeclaration *cd;
Dsymbol *s;

Expand All @@ -2926,22 +2925,22 @@ Expression *SuperExp::semantic(Scope *sc)
if (type)
return this;

FuncDeclaration *fd = hasThis(sc);

/* Special case for typeof(this) and typeof(super) since both
* should work even if they are not inside a non-static member function
*/
if (sc->intypeof)
if (!fd && sc->intypeof)
{
// Find enclosing class
for (Dsymbol *s = sc->parent; 1; s = s->parent)
for (Dsymbol *s = sc->getStructClassScope(); 1; s = s->parent)
{
ClassDeclaration *cd;

if (!s)
{
error("%s is not in a class scope", toChars());
goto Lerr;
}
cd = s->isClassDeclaration();
ClassDeclaration *cd = s->isClassDeclaration();
if (cd)
{
cd = cd->baseClass;
Expand All @@ -2954,11 +2953,9 @@ Expression *SuperExp::semantic(Scope *sc)
}
}
}

fdthis = sc->parent->isFuncDeclaration();
fd = hasThis(sc);
if (!fd)
goto Lerr;

assert(fd->vthis);
var = fd->vthis;
assert(var->parent);
Expand All @@ -2979,6 +2976,7 @@ Expression *SuperExp::semantic(Scope *sc)
else
{
type = cd->baseClass->type;
type = type->castMod(var->type->mod);
}

var->isVarDeclaration()->checkNestedReference(sc, loc);
Expand Down
35 changes: 35 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -3806,7 +3806,42 @@ struct Bar6087
}

/***************************************************/
// 6848

class Foo6848 {}

class Bar6848 : Foo6848
{
void func() immutable
{
static assert(is(typeof(this) == immutable(Bar6848))); // immutable(Bar6848)
auto t = this;
static assert(is(typeof(t) == immutable(Bar6848))); // immutable(Bar6848)

static assert(is(typeof(super) == immutable(Foo6848))); // Foo6848 instead of immutable(Foo6848)
auto s = super;
static assert(is(typeof(s) == immutable(Foo6848))); // Foo6848 instead of immutable(Foo6848)
}
}

/***************************************************/
// 6847

template True6847(T)
{
immutable True6847 = true;
}
class Foo6847
{}

class Bar6847 : Foo6847
{
static assert( True6847!(typeof(super)) );
static assert( is(typeof(super) == Foo6847) );
}

/***************************************************/
// 6289

void test6289()
{
Expand Down

0 comments on commit 85549f1

Please sign in to comment.