From 2d7fff7a998508e5827b9f8b8bc837e7ca69f500 Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Mon, 28 Oct 2024 13:40:41 +0100 Subject: [PATCH] chore: implement `Expr::try_as_const_integer` --- lib/src/compiler/ir/ast2ir.rs | 4 +--- lib/src/compiler/ir/mod.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/src/compiler/ir/ast2ir.rs b/lib/src/compiler/ir/ast2ir.rs index 422d6bd5..3ab2543e 100644 --- a/lib/src/compiler/ir/ast2ir.rs +++ b/lib/src/compiler/ir/ast2ir.rs @@ -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, diff --git a/lib/src/compiler/ir/mod.rs b/lib/src/compiler/ir/mod.rs index d77847bb..3fad57fb 100644 --- a/lib/src/compiler/ir/mod.rs +++ b/lib/src/compiler/ir/mod.rs @@ -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 { + if let TypeValue::Integer(Value::Const(v)) = self.type_value() { + Some(v) + } else { + None + } + } }