Skip to content

Commit

Permalink
Added validations for const fns.
Browse files Browse the repository at this point in the history
commit-id:4d4d4602
  • Loading branch information
orizi committed Jan 5, 2025
1 parent eabc3e0 commit 44d46e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use crate::diagnostic::{
ElementKind, MultiArmExprKind, NotFoundItemType, SemanticDiagnostics,
SemanticDiagnosticsBuilder, TraitInferenceErrors, UnsupportedOutsideOfFunctionFeatureName,
};
use crate::items::constant::{ConstValue, resolve_const_expr_and_evaluate};
use crate::items::constant::{ConstValue, resolve_const_expr_and_evaluate, validate_const_expr};
use crate::items::enm::SemanticEnumEx;
use crate::items::feature_kind::extract_item_feature_config;
use crate::items::functions::function_signature_params;
Expand Down Expand Up @@ -1085,7 +1085,9 @@ pub fn compute_root_expr(
inference.finalize(ctx.diagnostics, syntax.into());

ctx.apply_inference_rewriter();

if ctx.signature.map(|s| s.is_const) == Some(true) {
validate_const_expr(ctx, res);
}
Ok(res)
}

Expand Down
7 changes: 7 additions & 0 deletions crates/cairo-lang-semantic/src/items/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ pub fn constant_semantic_data_cycle_helper(
})
}

/// Checks if the given expression only involved constant calculations.
pub fn validate_const_expr(ctx: &mut ComputationContext<'_>, expr_id: ExprId) {
let mut eval_ctx =
ConstantEvaluateContext { db: ctx.db, arenas: &ctx.arenas, diagnostics: ctx.diagnostics };
eval_ctx.validate(expr_id);
}

/// Resolves the given const expression and evaluates its value.
pub fn resolve_const_expr_and_evaluate(
db: &dyn SemanticGroup,
Expand Down

0 comments on commit 44d46e3

Please sign in to comment.