Skip to content

Commit

Permalink
Fix std.typecons breaking
Browse files Browse the repository at this point in the history
Add test cases for accessing 'this' from constraint and DeclDefs scope
  • Loading branch information
9rnsr committed Feb 28, 2013
1 parent 3c17d5c commit 962dca4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/expression.c
Expand Up @@ -326,6 +326,33 @@ Expression *checkRightThis(Scope *sc, Expression *e)
p = p->parent;
FuncDeclaration *func = p ? p->isFuncDeclaration() : NULL;
FuncDeclaration *fdthis = hasThis(sc);
#if 1
/* Check special cases inside DeclDefs scope and template constraint
*/
if (func && !fdthis && sc->intypeof == 2)
{
//printf("[%s] func = %s\n", func->loc.toChars(), func->toChars());
for (Dsymbol *s = func->parent; 1; s = s->parent)
{
if (!s)
break;
//printf("\ts = %s %s\n", s->kind(), s->toChars());
if (s->isAggregateDeclaration() || s->isThis())
return e;
FuncDeclaration *f = s->isFuncDeclaration();
if (f)
{
if (f->isMember2())
break;
if (TemplateDeclaration *td = f->parent->isTemplateDeclaration())
{
if ((td->scope->stc & STCstatic) && td->isMember())
break; // no valid 'this'
}
}
}
}
#endif
if (sc->intypeof != 1 && (func && !fdthis || !func && !sc->getStructClassScope()) && ve->var->needThis())
{
//printf("checkRightThis sc->intypeof = %d, ad = %p, func = %p, fdthis = %p\n",
Expand Down
47 changes: 47 additions & 0 deletions test/runnable/testrightthis.d
Expand Up @@ -423,6 +423,52 @@ void test6()

/********************************************************/

struct Tuple7(T...)
{
T field;

enum check1 = is(typeof(field[0] = 1));
enum check2 = is(typeof({ field[0] = 1; }));

this(U, size_t n)(U[n] values)
if (is(typeof({ foreach (i, _; T) field[0] = values[0]; })))
{}
}

void test7()
{
alias Tuple7!(int, int) Tup7;
static assert(Tup7.check1);
static assert(Tup7.check2);
int[2] ints = [ 1, 2 ];
Tup7 t = ints;

struct S7
{
int value;

enum check1 = is(typeof(value = 1));
enum check2 = is(typeof({ value = 1; }));

void foo()(int v)
if (is(typeof({
value = v; // valid
}))) {}

static void bar()(int v)
if (is(typeof({
value = v; // always invalid
}))) {}
}
static assert(S7.check1);
static assert(S7.check2);
S7 s;
s.foo(1);
static assert(!__traits(compiles, S7.bar(1)));
}

/********************************************************/

int main()
{
test1();
Expand All @@ -431,6 +477,7 @@ int main()
test4();
test5();
test6();
test7();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 962dca4

Please sign in to comment.