Skip to content

Commit e37c2cf

Browse files
committed
fix: handle missing struct field in const evaluation instead of panicking
Fixes #9790
1 parent 5248475 commit e37c2cf

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

crates/cairo-lang-semantic/src/expr/test_data/constant

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,35 @@ error[E2127]: This expression is not supported as constant.
729729
--> lib.cairo:2:20
730730
const X: felt252 = f();
731731
^^^
732+
733+
//! > ==========================================================================
734+
735+
//! > Statement-level const struct literal with missing field.
736+
737+
//! > test_runner_name
738+
test_function_diagnostics(expect_diagnostics: true)
739+
740+
//! > function_code
741+
fn foo() {
742+
const X: S = S { a: 1 };
743+
}
744+
745+
//! > function_name
746+
foo
747+
748+
//! > module_code
749+
struct S {
750+
a: felt252,
751+
b: felt252,
752+
}
753+
754+
//! > expected_diagnostics
755+
error[E0003]: Missing member "b".
756+
--> lib.cairo:6:18
757+
const X: S = S { a: 1 };
758+
^^^^^^^^^^
759+
760+
warning[E2070]: Unused constant. Consider ignoring by prefixing with `_`.
761+
--> lib.cairo:6:11
762+
const X: S = S { a: 1 };
763+
^

crates/cairo-lang-semantic/src/items/constant.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,9 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> {
725725
.iter()
726726
.find(|(_, member_id)| m.id == *member_id)
727727
.map(|(expr_id, _)| self.evaluate(*expr_id))
728-
.expect("Should have been caught by semantic validation")
728+
// Semantic validation already reported an error, suppress cascading
729+
// errors from const evaluation.
730+
.unwrap_or_else(|| ConstValue::Missing(skip_diagnostic()).intern(db))
729731
})
730732
.collect(),
731733
*ty,

0 commit comments

Comments
 (0)