fix: handle missing struct field in const evaluation instead of panicking#9812
Open
orizi wants to merge 1 commit intoorizi/03-31-fix_unsupported_extern_fn_in_constfrom
Open
Conversation
This was referenced Mar 31, 2026
Collaborator
Author
This was referenced Mar 31, 2026
eytan-starkware
approved these changes
Mar 31, 2026
Contributor
eytan-starkware
left a comment
There was a problem hiding this comment.
@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
eytan-starkware
requested changes
Mar 31, 2026
Contributor
eytan-starkware
left a comment
There was a problem hiding this comment.
@eytan-starkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).
crates/cairo-lang-semantic/src/items/constant.rs line 728 at r1 (raw file):
.find(|(_, member_id)| m.id == *member_id) .map(|(expr_id, _)| self.evaluate(*expr_id)) // A missing field should have been caught by semantic validation.
Comment is a misleading. It was almost certainly caught, but allowed to continue processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Fixed a panic in constant evaluation when struct literals have missing fields by returning a
ConstValue::Missinginstead of callingexpect(). Added a test case to verify proper error handling for statement-level const struct literals with missing fields.Type of change
Please check one:
Why is this change needed?
The constant evaluator was panicking when encountering struct literals with missing fields during constant evaluation. While semantic validation should catch these cases, the evaluator should handle them gracefully rather than crashing with an
expect()call.What was the behavior or documentation before?
The constant evaluator would panic with "Should have been caught by semantic validation" when processing struct literals with missing fields during constant evaluation.
What is the behavior or documentation after?
The constant evaluator now gracefully handles missing struct fields by returning
ConstValue::Missingand continues processing, allowing semantic validation to report the appropriate error messages to the user.Related issue or discussion (if any)
Fixes #9790
Additional context
The fix maintains the existing error reporting behavior while preventing crashes in the constant evaluator. The test case demonstrates that proper error messages are still generated for missing struct fields in const declarations.
Note
Low Risk
Low risk: small, localized change in constant evaluation that replaces a panic with a
ConstValue::Missingfallback, plus a targeted diagnostic test.Overview
Fixes a crash in const evaluation for struct literals with omitted fields by replacing an
expect()with a safe fallback toConstValue::Missing(skip_diagnostic()), allowing compilation to continue and diagnostics to surface normally.Adds a regression test covering statement-level
constdeclarations with a struct literal missing a field, asserting the missing-member error (and unused-const warning) is reported instead of panicking.Written by Cursor Bugbot for commit 0284f14. This will update automatically on new commits. Configure here.