Skip to content

Commit

Permalink
more require branch matches
Browse files Browse the repository at this point in the history
  • Loading branch information
plotchy committed Jul 20, 2024
1 parent 765f27a commit d200403
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
47 changes: 42 additions & 5 deletions crates/pyrometer/tests/test_data/require_with_killed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,57 @@ contract RequireWithKilled {
function requireLt(uint x) public {
// set bounds for storeRange
require(5 < storeRange && storeRange < 100);
"pyro::variable::storeRange::range::[6, 99]";
// set tighter bounds for x
require(6 < x && x < 99);
"pyro::variable::x::range::[7, 98]";
// make x less than storeRange
require(x < storeRange);
}

function requireLtLocal(uint x) public {
uint y = 50;
function requireLte(uint x) public {
// set bounds for storeRange
require(5 < y && y < 100);
require(5 < storeRange && storeRange < 100);
// set tighter bounds for x
require(6 < x && x < 99);
// make x less than storeRange
require(x < y);
// make x less than or equal to storeRange
require(x <= storeRange);
}

function requireGt(uint x) public {
// set bounds for storeRange
require(5 < storeRange && storeRange < 100);
// set tighter bounds for x
require(6 < x && x < 99);
// make x greater than storeRange
require(x > storeRange);
}

function requireGte(uint x) public {
// set bounds for storeRange
require(5 < storeRange && storeRange < 100);
// set tighter bounds for x
require(6 < x && x < 99);
// make x greater than or equal to storeRange
require(x >= storeRange);
}

function requireEq(uint x) public {
// set bounds for storeRange
require(5 < storeRange && storeRange < 100);
// set tighter bounds for x
require(6 < x && x < 99);
// make x equal to storeRange
require(x == storeRange);
}

function requireNeq(uint x) public {
// set bounds for storeRange
require(5 < storeRange && storeRange < 100);
// set tighter bounds for x
require(6 < x && x < 99);
// make x not equal to storeRange
require(x != storeRange);
}

function setCount() public {
Expand Down
9 changes: 9 additions & 0 deletions crates/solc-expressions/src/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
(_, ExprRet::Null) | (ExprRet::Null, _) => Ok(()),
(_, ExprRet::CtxKilled(..)) | (ExprRet::CtxKilled(..), _) => Ok(()),
(ExprRet::SingleLiteral(lhs), ExprRet::Single(rhs)) => {
// ie: require(5 == a);
ContextVarNode::from(*lhs)
.cast_from(&ContextVarNode::from(*rhs), self, arena)
.into_expr_err(loc)?;
Expand All @@ -639,6 +640,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
)
}
(ExprRet::Single(lhs), ExprRet::SingleLiteral(rhs)) => {
// ie: require(a == 5);
ContextVarNode::from(*rhs)
.cast_from(&ContextVarNode::from(*lhs), self, arena)
.into_expr_err(loc)?;
Expand All @@ -654,6 +656,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
)
}
(ExprRet::Single(lhs), ExprRet::Single(rhs)) => {
// ie: require(a == b);
let lhs_cvar =
ContextVarNode::from(*lhs).latest_version_or_inherited_in_ctx(ctx, self);
let new_lhs = self.advance_var_in_ctx(lhs_cvar, loc, ctx)?;
Expand All @@ -665,6 +668,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
Ok(())
}
(l @ ExprRet::Single(_) | l @ ExprRet::SingleLiteral(_), ExprRet::Multi(rhs_sides)) => {
// ie: require(a == (b, c)); (not possible)
rhs_sides.iter().try_for_each(|expr_ret| {
self.handle_require_inner(
arena,
Expand All @@ -679,6 +683,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
})
}
(ExprRet::Multi(lhs_sides), r @ ExprRet::Single(_) | r @ ExprRet::SingleLiteral(_)) => {
// ie: require((a, b) == c); (not possible)
lhs_sides.iter().try_for_each(|expr_ret| {
self.handle_require_inner(
arena,
Expand All @@ -693,8 +698,11 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
})
}
(ExprRet::Multi(lhs_sides), ExprRet::Multi(rhs_sides)) => {
// ie: require((a, b) == (c, d)); (not possible)
// ie: require((a, b) == (c, d, e)); (not possible)
// try to zip sides if they are the same length
if lhs_sides.len() == rhs_sides.len() {
// ie: require((a, b) == (c, d)); (not possible)
lhs_sides.iter().zip(rhs_sides.iter()).try_for_each(
|(lhs_expr_ret, rhs_expr_ret)| {
self.handle_require_inner(
Expand All @@ -710,6 +718,7 @@ pub trait Require: AnalyzerBackend + Variable + BinOp + Sized {
},
)
} else {
// ie: require((a, b) == (c, d, e)); (not possible)
rhs_sides.iter().try_for_each(|rhs_expr_ret| {
self.handle_require_inner(
arena,
Expand Down

0 comments on commit d200403

Please sign in to comment.