Skip to content

feat(constant): Added support for const calculation for inner variants and lets.#9899

Merged
orizi merged 1 commit into
mainfrom
orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets
May 10, 2026
Merged

feat(constant): Added support for const calculation for inner variants and lets.#9899
orizi merged 1 commit into
mainfrom
orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets

Conversation

@orizi
Copy link
Copy Markdown
Collaborator

@orizi orizi commented May 6, 2026

Summary

Extends constant evaluation to support a broader set of pattern matching constructs. The destructure_pattern function now returns Option<()> to indicate whether a pattern matched, enabling proper handling of non-matching arms. Match expressions now iterate arms using destructure_pattern for all pattern types (including wildcards, literals, tuples, fixed-size arrays, and structs) rather than only enum variants. let-else statements in constant blocks are now evaluated: if the pattern fails to match, the else clause is executed. if let chains and if let conditions similarly use the generalized destructure_pattern path. Boolean condition evaluation in if expressions now compares against cached true/false const values directly instead of extracting the enum variant.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)

Why is this change needed?

Constant evaluation previously only handled enum variant patterns in match expressions and if let conditions. Wildcard patterns, literal patterns, tuple patterns, array patterns, and struct patterns in match arms were not correctly evaluated, causing incorrect or missing constant folding. Additionally, let-else statements inside constant blocks were not evaluated at all — the else clause was never considered, so refutable patterns with a let-else would silently produce wrong results instead of executing the panic branch.


What was the behavior or documentation before?

  • match on non-enum types (e.g., u8, felt252, tuples, structs) with wildcard or literal patterns would fail to evaluate correctly.
  • let-else in a constant block would ignore the else clause even when the pattern did not match.
  • if let chains were restricted to enum variant patterns only.

What is the behavior or documentation after?

  • match expressions in constants correctly evaluate wildcard, literal, tuple, fixed-size array, struct, and enum variant patterns.
  • let-else in constant blocks executes the else clause (typically a panic) when the pattern does not match, producing the appropriate Failed to calculate constant diagnostic.
  • if let and if let chains 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 constant match expressions. New constant test cases in the semantic test data cover match with wildcards and literals, let-else, if let, and if let chains with multiple conditions.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Collaborator Author

orizi commented May 6, 2026

@orizi orizi force-pushed the orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets branch from 1557da3 to 06b20e7 Compare May 6, 2026 12:57
@orizi orizi requested a review from TomerStarkware May 6, 2026 13:03
@orizi orizi marked this pull request as ready for review May 6, 2026 13:03
@cursor
Copy link
Copy Markdown

cursor Bot commented May 6, 2026

PR Summary

Medium Risk
Extends compile-time constant evaluation semantics for pattern matching and let-else, which can change which constants successfully fold or now error; risk is moderated by added regression tests but impacts core compiler behavior.

Overview
Constant folding now understands more pattern constructs. Const evaluation no longer assumes match/if let patterns are enum-only; it uses a generalized destructure_pattern that returns match success, enabling wildcard/literal/tuple/array/struct/unit patterns and correct arm selection.

let bindings in const blocks now handle else clauses. During evaluation and validation, let-else is evaluated so non-matching refutable patterns execute the else branch (typically producing a Failed to calculate constant diagnostic).

Adds semantic test cases and a new bug sample (issue9887) to lock in behavior for const match, destructuring, and if let chains.

Reviewed by Cursor Bugbot for commit 831a2b7. Bugbot is set up for automated code reviews on this repo. Configure here.

@orizi orizi force-pushed the orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets branch from 06b20e7 to 71016fb Compare May 6, 2026 13:05
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/cairo-lang-semantic/src/items/constant.rs Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ 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.

Comment thread crates/cairo-lang-semantic/src/items/constant.rs Outdated
Comment thread crates/cairo-lang-semantic/src/items/constant.rs Outdated
Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on orizi).

@orizi orizi force-pushed the orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets branch from 71016fb to bb08980 Compare May 10, 2026 10:30
@orizi orizi force-pushed the orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets branch from bb08980 to 831a2b7 Compare May 10, 2026 10:31
Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).

Copy link
Copy Markdown
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi orizi added this pull request to the merge queue May 10, 2026
Merged via the queue into main with commit 0038bf2 May 10, 2026
54 checks passed
@orizi orizi deleted the orizi/05-06-feat_constant_added_support_for_const_calculation_for_inner_variants_and_lets branch May 10, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: compiler ICE bug on match in const initializer (sierra-generator/types.rs:166)

3 participants