Skip to content

Commit

Permalink
4298 Constant array translated to unnecessary array literal creation
Browse files Browse the repository at this point in the history
Uses rsinfu's patch, which works once the patch for bug 3779 is included.
I also need to make a little change to CTFE, which can no longer assume that
const variables have been expanded. This triggered one failure in Phobos --
but it was a bug:
immutable int[1] x =[7];
void main(){ int [] y = x;} // passed, now correctly generates an error
  • Loading branch information
Don Clugston committed Apr 14, 2011
1 parent 47d46bf commit 7192ddf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/interpret.c
Expand Up @@ -1287,14 +1287,17 @@ Expression *getVarExp(Loc loc, InterState *istate, Declaration *d, CtfeGoal goal
*/
if (v->ident == Id::ctfe)
return new IntegerExp(loc, 1, Type::tbool);

if ((v->isConst() || v->isImmutable() || v->storage_class & STCmanifest) && v->init && !v->getValue())
#else
if (v->isConst() && v->init)
#endif
{ e = v->init->toExpression();
if (e && !e->type)
e->type = v->type;
if (e)
e = e->interpret(istate, ctfeNeedAnyValue);
if (e && e != EXP_CANT_INTERPRET)
v->setValueWithoutChecking(e);
}
else if ((v->isCTFE() || (!v->isDataseg() && istate)) && !v->getValue())
{
Expand All @@ -1317,7 +1320,7 @@ Expression *getVarExp(Loc loc, InterState *istate, Declaration *d, CtfeGoal goal
}
else
{ e = v->getValue();
if (!v->isCTFE() && v->isDataseg())
if (!e && !v->isCTFE() && v->isDataseg())
{ error(loc, "static variable %s cannot be read at compile time", v->toChars());
e = EXP_CANT_INTERPRET;
}
Expand Down
2 changes: 1 addition & 1 deletion src/optimize.c
Expand Up @@ -60,7 +60,7 @@ Expression *expandVar(int result, VarDeclaration *v)
Type *tb = v->type->toBasetype();
if (result & WANTinterpret ||
v->storage_class & STCmanifest ||
(tb->ty != Tsarray && tb->ty != Tstruct)
v->type->toBasetype()->isscalar()
)
{
if (v->init)
Expand Down

0 comments on commit 7192ddf

Please sign in to comment.