Skip to content

Commit

Permalink
Issue 10018 - Add VRP support for const/immutable variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed Jun 28, 2014
1 parent db18e36 commit 45a26d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/cast.c
Expand Up @@ -3405,11 +3405,20 @@ IntRange getIntRange(Expression *e)
range = IntRange(ir1.imin >> ir2.imax, ir1.imax >> ir2.imin).cast(e->type);
}

void visit(AssignExp *e)
{
range = getIntRange(e->e2).cast(e->type);
}

void visit(VarExp *e)
{
Expression *ie;
VarDeclaration* vd = e->var->isVarDeclaration();
if (vd && vd->range)
range = *vd->range;
range = vd->range->cast(e->type);
else if (vd && vd->init && !vd->type->isMutable() &&
(ie = vd->getConstInitializer()))
ie->accept(this);
else
visit((Expression *)e);
}
Expand Down
13 changes: 13 additions & 0 deletions test/compilable/testVRP.d
Expand Up @@ -306,3 +306,16 @@ void test9617a()
}
}
}

void test10018(ubyte value)
{
const int c = value;
ubyte b = c;
static assert(!__traits(compiles, b = c - 1));
static assert(!__traits(compiles, b = c + 1));
immutable int i = value;
b = i;
static assert(!__traits(compiles, b = i - 1));
static assert(!__traits(compiles, b = i + 1));
}

0 comments on commit 45a26d5

Please sign in to comment.