Skip to content

Commit

Permalink
Merge pull request #35 from 9rnsr/fix3688
Browse files Browse the repository at this point in the history
Issue 3688 - Can't have declaration with assignment to const/immutable inside if condition
  • Loading branch information
WalterBright committed May 29, 2011
2 parents 37e5192 + c56e59d commit 269a344
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/statement.c
Expand Up @@ -2315,9 +2315,6 @@ Statement *IfStatement::syntaxCopy()

Statement *IfStatement::semantic(Scope *sc)
{
condition = condition->semantic(sc);
condition = resolveProperties(sc, condition);

// Evaluate at runtime
unsigned cs0 = sc->callSuper;
unsigned cs1;
Expand All @@ -2331,24 +2328,21 @@ Statement *IfStatement::semantic(Scope *sc)
sym->parent = sc->scopesym;
scd = sc->push(sym);

Type *t = arg->type ? arg->type : condition->type;
match = new VarDeclaration(loc, t, arg->ident, NULL);
match = new VarDeclaration(loc, arg->type, arg->ident, new ExpInitializer(loc, condition));
match->noscope = 1;
match->semantic(scd);
if (!scd->insert(match))
assert(0);
match->parent = sc->func;

/* Generate:
* ((arg = condition), arg)
*/
VarExp *v = new VarExp(0, match);
condition = new AssignExp(loc, v, condition);
condition = new CommaExp(loc, condition, v);
DeclarationExp *de = new DeclarationExp(loc, match);
VarExp *ve = new VarExp(0, match);
condition = new CommaExp(loc, de, ve);
condition = condition->semantic(scd);
}
else
{
condition = condition->semantic(sc);
condition = resolveProperties(sc, condition);
scd = sc->push();
}

// Convert to boolean after declaring arg so this works:
// if (S arg = S()) {}
Expand Down

0 comments on commit 269a344

Please sign in to comment.