From d4a3b712f8365458e765e1ac4779904dbcdeb77d Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Tue, 31 Mar 2026 18:20:26 +0300 Subject: [PATCH] fix: handle missing struct field in const evaluation instead of panicking Fixes starkware-libs/cairo#9790 --- .../src/expr/test_data/constant | 32 +++++++++++++++++++ .../cairo-lang-semantic/src/items/constant.rs | 4 ++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/cairo-lang-semantic/src/expr/test_data/constant b/crates/cairo-lang-semantic/src/expr/test_data/constant index 5eaed25d321..3ea833085ba 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/constant +++ b/crates/cairo-lang-semantic/src/expr/test_data/constant @@ -729,3 +729,35 @@ error[E2127]: This expression is not supported as constant. --> lib.cairo:2:20 const X: felt252 = f(); ^^^ + +//! > ========================================================================== + +//! > Statement-level const struct literal with missing field. + +//! > test_runner_name +test_function_diagnostics(expect_diagnostics: true) + +//! > function_code +fn foo() { + const X: S = S { a: 1 }; +} + +//! > function_name +foo + +//! > module_code +struct S { + a: felt252, + b: felt252, +} + +//! > expected_diagnostics +error[E0003]: Missing member "b". + --> lib.cairo:6:18 + const X: S = S { a: 1 }; + ^^^^^^^^^^ + +warning[E2070]: Unused constant. Consider ignoring by prefixing with `_`. + --> lib.cairo:6:11 + const X: S = S { a: 1 }; + ^ diff --git a/crates/cairo-lang-semantic/src/items/constant.rs b/crates/cairo-lang-semantic/src/items/constant.rs index d90cc26baf4..288a8694f5d 100644 --- a/crates/cairo-lang-semantic/src/items/constant.rs +++ b/crates/cairo-lang-semantic/src/items/constant.rs @@ -725,7 +725,9 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> { .iter() .find(|(_, member_id)| m.id == *member_id) .map(|(expr_id, _)| self.evaluate(*expr_id)) - .expect("Should have been caught by semantic validation") + // Semantic validation already reported an error, suppress cascading + // errors from const evaluation. + .unwrap_or_else(|| ConstValue::Missing(skip_diagnostic()).intern(db)) }) .collect(), *ty,