Skip to content

Commit

Permalink
adding comments for match arm examples
Browse files Browse the repository at this point in the history
  • Loading branch information
plotchy committed Jul 20, 2024
1 parent ad9c1f8 commit 765f27a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 251 deletions.
52 changes: 3 additions & 49 deletions crates/solc-expressions/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub trait Array: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
) -> Result<(), ExprErr> {
match ret {
ExprRet::Single(inner_ty) | ExprRet::SingleLiteral(inner_ty) => {
// ie: uint[]
// ie: uint[][]
if let Some(var_type) = VarType::try_from_idx(self, inner_ty) {
let dyn_b = Builtin::Array(var_type);
if let Some(idx) = self.builtins().get(&dyn_b) {
Expand All @@ -65,6 +67,7 @@ pub trait Array: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
}
}
ExprRet::Multi(inner) => {
// ie: unsure of syntax needed to get here. (not possible?)
inner
.into_iter()
.map(|i| self.match_ty(ctx, ty_expr, i))
Expand Down Expand Up @@ -180,7 +183,6 @@ pub trait Array: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
(RangeOp::Lte, RangeOp::Gte),
)?;
}

let name = format!(
"{}[{}]",
parent.name(self).into_expr_err(loc)?,
Expand Down Expand Up @@ -470,52 +472,4 @@ pub trait Array: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
Ok(())
}
}

fn update_array_min_if_length(
&mut self,
arena: &mut RangeArena<Elem<Concrete>>,
ctx: ContextNode,
loc: Loc,
maybe_length: ContextVarNode,
) -> Result<(), ExprErr> {
if let Some(backing_arr) = maybe_length.len_var_to_array(self).into_expr_err(loc)? {
let next_arr = self.advance_var_in_ctx(
backing_arr.latest_version_or_inherited_in_ctx(ctx, self),
loc,
ctx,
)?;
let new_len = Elem::from(backing_arr)
.get_length()
.max(maybe_length.into());
let min = Elem::from(backing_arr).set_length(new_len);
next_arr
.set_range_min(self, arena, min)
.into_expr_err(loc)?;
}
Ok(())
}

fn update_array_max_if_length(
&mut self,
arena: &mut RangeArena<Elem<Concrete>>,
ctx: ContextNode,
loc: Loc,
maybe_length: ContextVarNode,
) -> Result<(), ExprErr> {
if let Some(backing_arr) = maybe_length.len_var_to_array(self).into_expr_err(loc)? {
let next_arr = self.advance_var_in_ctx(
backing_arr.latest_version_or_inherited_in_ctx(ctx, self),
loc,
ctx,
)?;
let new_len = Elem::from(backing_arr)
.get_length()
.min(maybe_length.into());
let max = Elem::from(backing_arr).set_length(new_len);
next_arr
.set_range_max(self, arena, max)
.into_expr_err(loc)?;
}
Ok(())
}
}
17 changes: 14 additions & 3 deletions crates/solc-expressions/src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub trait Assign: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized
Ok(())
}
(ExprRet::Single(lhs), ExprRet::SingleLiteral(rhs)) => {
// ie: uint x = 5;
let lhs_cvar =
ContextVarNode::from(*lhs).latest_version_or_inherited_in_ctx(ctx, self);
let rhs_cvar =
Expand All @@ -86,6 +87,7 @@ pub trait Assign: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized
Ok(())
}
(ExprRet::Single(lhs), ExprRet::Single(rhs)) => {
// ie: uint x = y;
let lhs_cvar =
ContextVarNode::from(*lhs).latest_version_or_inherited_in_ctx(ctx, self);
let rhs_cvar =
Expand All @@ -94,23 +96,31 @@ pub trait Assign: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized
.into_expr_err(loc)?;
Ok(())
}
(l @ ExprRet::Single(_), ExprRet::Multi(rhs_sides)) => rhs_sides
.iter()
.try_for_each(|expr_ret| self.match_assign_sides(arena, ctx, loc, l, expr_ret)),
(l @ ExprRet::Single(_), ExprRet::Multi(rhs_sides)) => {
// ie: uint x = (a, b), not possible?
rhs_sides
.iter()
.try_for_each(|expr_ret| self.match_assign_sides(arena, ctx, loc, l, expr_ret))
}
(ExprRet::Multi(lhs_sides), r @ ExprRet::Single(_) | r @ ExprRet::SingleLiteral(_)) => {
// ie: (uint x, uint y) = a, not possible?
lhs_sides
.iter()
.try_for_each(|expr_ret| self.match_assign_sides(arena, ctx, loc, expr_ret, r))
}
(ExprRet::Multi(lhs_sides), ExprRet::Multi(rhs_sides)) => {
// try to zip sides if they are the same length
// (x, y) = (a, b)
// ie: (x, y) = (a, b, c), not possible?
if lhs_sides.len() == rhs_sides.len() {
// (x, y) = (a, b)
lhs_sides.iter().zip(rhs_sides.iter()).try_for_each(
|(lhs_expr_ret, rhs_expr_ret)| {
self.match_assign_sides(arena, ctx, loc, lhs_expr_ret, rhs_expr_ret)
},
)
} else {
// ie: (x, y) = (a, b, c), not possible?
rhs_sides.iter().try_for_each(|rhs_expr_ret| {
self.match_assign_sides(arena, ctx, loc, lhs_paths, rhs_expr_ret)
})
Expand Down Expand Up @@ -203,6 +213,7 @@ pub trait Assign: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized
}

if !lhs_cvar.ty_eq(&rhs_cvar, self).into_expr_err(loc)? {
// lhs type doesnt match rhs type (not possible? have never reached this)
let cast_to_min = match lhs_cvar.range_min(self).into_expr_err(loc)? {
Some(v) => v,
None => {
Expand Down
7 changes: 7 additions & 0 deletions crates/solc-expressions/src/bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
"No right hand side provided for binary operation".to_string(),
)),
(ExprRet::SingleLiteral(lhs), ExprRet::SingleLiteral(rhs)) => {
// ie: 5 + 5
let lhs_cvar =
ContextVarNode::from(*lhs).latest_version_or_inherited_in_ctx(ctx, self);
let rhs_cvar =
Expand All @@ -89,6 +90,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
Ok(())
}
(ExprRet::SingleLiteral(lhs), ExprRet::Single(rhs)) => {
// ie: 5 + x
ContextVarNode::from(*lhs)
.cast_from(&ContextVarNode::from(*rhs), self, arena)
.into_expr_err(loc)?;
Expand All @@ -104,6 +106,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
Ok(())
}
(ExprRet::Single(lhs), ExprRet::SingleLiteral(rhs)) => {
// ie: x + 5
ContextVarNode::from(*rhs)
.cast_from(&ContextVarNode::from(*lhs), self, arena)
.into_expr_err(loc)?;
Expand All @@ -119,6 +122,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
Ok(())
}
(ExprRet::Single(lhs), ExprRet::Single(rhs)) => {
// ie: x + y
let lhs_cvar =
ContextVarNode::from(*lhs).latest_version_or_inherited_in_ctx(ctx, self);
let rhs_cvar =
Expand All @@ -131,13 +135,15 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
Ok(())
}
(lhs @ ExprRet::Single(..), ExprRet::Multi(rhs_sides)) => {
// ie: x + (y, z), (not possible?)
rhs_sides
.iter()
.map(|expr_ret| self.op_match(arena, ctx, loc, lhs, expr_ret, op, assign))
.collect::<Result<Vec<()>, ExprErr>>()?;
Ok(())
}
(ExprRet::Multi(lhs_sides), rhs @ ExprRet::Single(..)) => {
// ie: (x, y) + z, (not possible?)
lhs_sides
.iter()
.map(|expr_ret| self.op_match(arena, ctx, loc, expr_ret, rhs, op, assign))
Expand All @@ -147,6 +153,7 @@ pub trait BinOp: AnalyzerBackend<Expr = Expression, ExprErr = ExprErr> + Sized {
(_, ExprRet::CtxKilled(kind)) => ctx.kill(self, loc, *kind).into_expr_err(loc),
(ExprRet::CtxKilled(kind), _) => ctx.kill(self, loc, *kind).into_expr_err(loc),
(ExprRet::Multi(lhs_sides), ExprRet::Multi(rhs_sides)) => Err(ExprErr::UnhandledCombo(
// ie: (x, y) + (a, b), (not possible?)
loc,
format!("Unhandled combination in binop: {lhs_sides:?} {rhs_sides:?}"),
)),
Expand Down
Loading

0 comments on commit 765f27a

Please sign in to comment.