Skip to content

Commit

Permalink
foreach range uses same logic as ?: for type deduction
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed Sep 8, 2013
1 parent a3e447c commit 0b7e3a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/statement.c
Expand Up @@ -2430,17 +2430,22 @@ Statement *ForeachRangeStatement::semantic(Scope *sc)
/* Just picking the first really isn't good enough.
*/
arg->type = lwr->type;
arg->type = arg->type->addStorageClass(arg->storageClass);
}
else if (lwr->type == upr->type)
{
/* Same logic as CondExp ?lwr:upr
*/
arg->type = lwr->type;
}
else
{
AddExp ea(loc, lwr, upr);
Expression *e = ea.typeCombine(sc);
arg->type = ea.type;
arg->type = arg->type->addStorageClass(arg->storageClass);
lwr = ea.e1;
upr = ea.e2;
}
arg->type = arg->type->addStorageClass(arg->storageClass);
}

/* Convert to a for loop:
Expand Down
8 changes: 8 additions & 0 deletions test/compilable/test1878a.d
@@ -0,0 +1,8 @@
void main()
{
ubyte from, to;
foreach(i; from..to)
{
static assert(is(typeof(i) == ubyte));
}
}

0 comments on commit 0b7e3a7

Please sign in to comment.