feat(constant): Added support for const calculation for inner variants and lets.#9899
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
1557da3 to
06b20e7
Compare
PR SummaryMedium Risk Overview
Adds semantic test cases and a new bug sample ( Reviewed by Cursor Bugbot for commit 831a2b7. Bugbot is set up for automated code reviews on this repo. Configure here. |
06b20e7 to
71016fb
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 06b20e7588
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 71016fb. Configure here.
TomerStarkware
left a comment
There was a problem hiding this comment.
@TomerStarkware reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on orizi).
71016fb to
bb08980
Compare
bb08980 to
831a2b7
Compare
TomerStarkware
left a comment
There was a problem hiding this comment.
@TomerStarkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).
orizi
left a comment
There was a problem hiding this comment.
@orizi resolved 1 discussion.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on orizi).


Summary
Extends constant evaluation to support a broader set of pattern matching constructs. The
destructure_patternfunction now returnsOption<()>to indicate whether a pattern matched, enabling proper handling of non-matching arms. Match expressions now iterate arms usingdestructure_patternfor all pattern types (including wildcards, literals, tuples, fixed-size arrays, and structs) rather than only enum variants.let-elsestatements in constant blocks are now evaluated: if the pattern fails to match, the else clause is executed.if letchains andif letconditions similarly use the generalizeddestructure_patternpath. Boolean condition evaluation inifexpressions now compares against cached true/false const values directly instead of extracting the enum variant.Type of change
Please check one:
Why is this change needed?
Constant evaluation previously only handled enum variant patterns in
matchexpressions andif letconditions. Wildcard patterns, literal patterns, tuple patterns, array patterns, and struct patterns inmatcharms were not correctly evaluated, causing incorrect or missing constant folding. Additionally,let-elsestatements inside constant blocks were not evaluated at all — the else clause was never considered, so refutable patterns with alet-elsewould silently produce wrong results instead of executing the panic branch.What was the behavior or documentation before?
matchon non-enum types (e.g.,u8,felt252, tuples, structs) with wildcard or literal patterns would fail to evaluate correctly.let-elsein a constant block would ignore the else clause even when the pattern did not match.if letchains were restricted to enum variant patterns only.What is the behavior or documentation after?
matchexpressions in constants correctly evaluate wildcard, literal, tuple, fixed-size array, struct, and enum variant patterns.let-elsein constant blocks executes the else clause (typically a panic) when the pattern does not match, producing the appropriateFailed to calculate constantdiagnostic.if letandif letchains work with the full set of supported patterns.Related issue or discussion (if any)
Fixes #9887.
Additional context
A regression test (
tests/bug_samples/issue9887.cairo) covers wildcard, literal, tuple, struct, and unit patterns in constantmatchexpressions. New constant test cases in the semantic test data covermatchwith wildcards and literals,let-else,if let, andif letchains with multiple conditions.