protect agains division by zero in constant folding

This commit is contained in:
2021-10-24 22:56:10 +02:00
parent d4a5d62255
commit b8b0a21ef9

View File

@@ -39,7 +39,10 @@ fn fold_expr(expr: &mut ast::Expression) {
use ast::BinOp::*; use ast::BinOp::*;
match expr.expr { match expr.expr {
ast::Expr::BinOp { ast::Expr::BinOp {
ref mut left, op, ref mut right, .. ref mut left,
op,
ref mut right,
..
} => { } => {
fold_expr(left); fold_expr(left);
fold_expr(right); fold_expr(right);
@@ -50,8 +53,20 @@ fn fold_expr(expr: &mut ast::Expression) {
Add => left.wrapping_add(right), Add => left.wrapping_add(right),
Sub => left.wrapping_sub(right), Sub => left.wrapping_sub(right),
Mul => left.wrapping_mul(right), Mul => left.wrapping_mul(right),
Div => left / right, // TODO: protect agains division by zero Div => {
Rem => left % right, // TODO: check correct behavior with negative operands if let Some(result) = left.checked_div(right) {
result
} else {
return;
}
}
Rem => {
if let Some(result) = left.checked_rem(right) {
result
} else {
return;
}
}
And => left & right, And => left & right,
Or => left | right, Or => left | right,
Xor => left ^ right, Xor => left ^ right,
@@ -64,7 +79,7 @@ fn fold_expr(expr: &mut ast::Expression) {
}; };
expr.expr = ast::Expr::I32Const(result); expr.expr = ast::Expr::I32Const(result);
} }
_ => () _ => (),
} }
} }
ast::Expr::I32Const(_) | ast::Expr::Variable { .. } => (), ast::Expr::I32Const(_) | ast::Expr::Variable { .. } => (),