Skip to content

Commit c7dfd78

Browse files
authored
Merge pull request #10636 from ethereum/smt_fix_constant
Fix constant evaluation build
2 parents b252ab0 + f5c96ea commit c7dfd78

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

libsolidity/formal/SMTEncoder.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,16 +1626,14 @@ void SMTEncoder::defineGlobalVariable(string const& _name, Expression const& _ex
16261626

16271627
bool SMTEncoder::shortcutRationalNumber(Expression const& _expr)
16281628
{
1629-
auto type = isConstant(_expr);
1630-
if (!type)
1629+
RationalNumberType const* rationalType = isConstant(_expr);
1630+
if (!rationalType)
16311631
return false;
16321632

1633-
solAssert(type->category() == Type::Category::RationalNumber, "");
1634-
auto const& rationalType = dynamic_cast<RationalNumberType const&>(*type);
1635-
if (rationalType.isNegative())
1636-
defineExpr(_expr, smtutil::Expression(u2s(rationalType.literalValue(nullptr))));
1633+
if (rationalType->isNegative())
1634+
defineExpr(_expr, smtutil::Expression(u2s(rationalType->literalValue(nullptr))));
16371635
else
1638-
defineExpr(_expr, smtutil::Expression(rationalType.literalValue(nullptr)));
1636+
defineExpr(_expr, smtutil::Expression(rationalType->literalValue(nullptr)));
16391637
return true;
16401638
}
16411639

@@ -2658,20 +2656,20 @@ map<ContractDefinition const*, vector<ASTPointer<frontend::Expression>>> SMTEnco
26582656
return baseArgs;
26592657
}
26602658

2661-
TypePointer SMTEncoder::isConstant(Expression const& _expr)
2659+
RationalNumberType const* SMTEncoder::isConstant(Expression const& _expr)
26622660
{
2663-
if (
2664-
auto type = _expr.annotation().type;
2665-
type->category() == Type::Category::RationalNumber
2666-
)
2661+
if (auto type = dynamic_cast<RationalNumberType const*>(_expr.annotation().type))
26672662
return type;
26682663

26692664
// _expr may not be constant evaluable.
26702665
// In that case we ignore any warnings emitted by the constant evaluator,
26712666
// as it will return nullptr in case of failure.
26722667
ErrorList l;
26732668
ErrorReporter e(l);
2674-
return ConstantEvaluator(e).evaluate(_expr);
2669+
if (auto t = ConstantEvaluator::evaluate(e, _expr))
2670+
return TypeProvider::rationalNumber(t->value);
2671+
2672+
return nullptr;
26752673
}
26762674

26772675
void SMTEncoder::createReturnedExpressions(FunctionCall const& _funCall)

libsolidity/formal/SMTEncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class SMTEncoder: public ASTConstVisitor
8585

8686
/// @returns a valid RationalNumberType pointer if _expr has type
8787
/// RationalNumberType or can be const evaluated, and nullptr otherwise.
88-
static TypePointer isConstant(Expression const& _expr);
88+
static RationalNumberType const* isConstant(Expression const& _expr);
8989

9090
protected:
9191
// TODO: Check that we do not have concurrent reads and writes to a variable,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pragma experimental SMTChecker;
2+
3+
contract C {
4+
uint constant x = 7;
5+
uint constant y = 3;
6+
uint constant z = x / y;
7+
8+
function f() public pure {
9+
assert(z == 2);
10+
assert(z == x / 3);
11+
assert(z == 7 / y);
12+
assert(z * 3 != 7);
13+
}
14+
}

0 commit comments

Comments
 (0)