Skip to content

Commit

Permalink
Fix checkToBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jan 21, 2013
1 parent c16d6bd commit 000cb37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/expression.c
Expand Up @@ -1949,8 +1949,12 @@ Expression *Expression::checkToBoolean(Scope *sc)
assert(type);
#endif

// Structs can be converted to bool using opCast(bool)()
Expression *e = this;
Type *t = type;
Type *tb = type->toBasetype();
Type *att = NULL;
Lagain:
// Structs can be converted to bool using opCast(bool)()
if (tb->ty == Tstruct)
{ AggregateDeclaration *ad = ((TypeStruct *)tb)->sym;
/* Don't really need to check for opCast first, but by doing so we
Expand All @@ -1959,26 +1963,29 @@ Expression *Expression::checkToBoolean(Scope *sc)
Dsymbol *fd = search_function(ad, Id::cast);
if (fd)
{
Expression *e = new CastExp(loc, this, Type::tbool);
e = new CastExp(loc, e, Type::tbool);
e = e->semantic(sc);
return e;
}

// Forward to aliasthis.
if (ad->aliasthis)
if (ad->aliasthis && tb != att)
{
Expression *e = resolveAliasThis(sc, this);
e = e->checkToBoolean(sc);
return e;
if (!att && tb->checkAliasThisRec())
att = tb;
e = resolveAliasThis(sc, e);
t = e->type;
tb = e->type->toBasetype();
goto Lagain;
}
}

if (!type->checkBoolean())
{ if (type->toBasetype() != Type::terror)
error("expression %s of type %s does not have a boolean value", toChars(), type->toChars());
if (!t->checkBoolean())
{ if (tb != Type::terror)
error("expression %s of type %s does not have a boolean value", toChars(), t->toChars());
return new ErrorExp();
}
return this;
return e;
}

/****************************
Expand Down
4 changes: 4 additions & 0 deletions test/runnable/aliasthis.d
Expand Up @@ -336,6 +336,10 @@ void test7()
static assert(!__traits(compiles, { foreach (e; s3){} }));
static assert(!__traits(compiles, { foreach (e; c1){} }));
static assert(!__traits(compiles, { foreach (e; c3){} }));

// Expression::checkToBoolean
static assert(!__traits(compiles, { if (s1){} }));
static assert(!__traits(compiles, { if (s3){} }));
}

/***************************************************/
Expand Down

0 comments on commit 000cb37

Please sign in to comment.