Skip to content

Commit

Permalink
chore: implement Expr::try_as_const_integer
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Oct 28, 2024
1 parent c3590a4 commit 2d7fff7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/src/compiler/ir/ast2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,7 @@ fn of_expr_from_ast(
// If the quantifier expression is greater than the number of items,
// the `of` expression is always false.
if let Quantifier::Expr(expr) = &quantifier {
if let TypeValue::Integer(Value::Const(value)) =
ctx.ir.get(*expr).type_value()
{
if let Some(value) = ctx.ir.get(*expr).try_as_const_integer() {
if value > num_items.try_into().unwrap() {
ctx.warnings.add(|| warnings::InvariantBooleanExpression::build(
ctx.report_builder,
Expand Down
10 changes: 10 additions & 0 deletions lib/src/compiler/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,14 @@ impl Expr {
None
}
}

/// If the expression is a constant integer, returns its value, if not
/// returns [`None`]
pub fn try_as_const_integer(&self) -> Option<i64> {
if let TypeValue::Integer(Value::Const(v)) = self.type_value() {
Some(v)
} else {
None
}
}
}

0 comments on commit 2d7fff7

Please # to comment.