Sitelet https://github.com/starkware-libs/cairo/pull/10040
Skip to content

fix(semantic): validate inferred numeric literal values against their type - #10040

Merged
orizi merged 1 commit into
mainfrom
orizi/06-05-fix_semantic_validate_inferred_numeric_literal_values_against_their_type
Jun 7, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-05-fix_semantic_validate_inferred_numeric_literal_values_against_their_type

Conversation

@orizi

@orizi orizi commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Moves out-of-range literal validation from the lowering phase (E3009) to the semantic phase (E2008). This is done by performing literal range checks inside apply_inference_rewriter after inference has resolved the literal's type, and removing the redundant validation that was previously done in ConstantEvaluateContext. The lowering test data is updated to reflect that these errors now appear as semantic diagnostics, and a new semantic test case is added for an inferred-type numeric literal whose value is out of range.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

Out-of-range literal errors were being reported during lowering (E3009) rather than during semantic analysis (E2008). Since the type of a numeric literal is resolved during inference (which happens in the semantic phase), the range check should also occur there. Reporting it at the wrong phase meant the error code was incorrect and the diagnostic was emitted later than necessary.


What was the behavior or documentation before?

Out-of-range numeric literal errors were reported as E3009 (a lowering-phase error) after inference had already resolved the literal type. The LiteralError variant existed in LoweringDiagnosticKind and validation was scattered across lower_expr_literal_to_var_usage, create_node_for_value, and handle_u256_literal.


What is the behavior or documentation after?

Out-of-range numeric literal errors are reported as E2008 (a semantic-phase error) during apply_inference_rewriter, immediately after inference resolves the literal's type. For suffix-less literals whose type was still an unresolved NumericLiteral var, only OutOfRange errors are reported to avoid cascading from already-reported type errors. The LiteralError variant is removed from LoweringDiagnosticKind entirely, along with the report helper on FlowControlGraphBuilder that was only used for that purpose.


Related issue or discussion (if any)


Additional context

The ConstantEvaluateContext::validate method previously had a dedicated Expr::Literal arm that re-ran literal validation. This is now removed since apply_inference_rewriter already handles it. The handle_u256_literal function no longer needs to validate or return a Maybe result, since out-of-range u256 patterns are now caught semantically and the lowering-level arm-dropping logic is removed.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 5, 2026 08:45
@cursor

cursor Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes when and where literal errors surface and how match graphs treat invalid arms; behavior is intentional but affects compiler diagnostics and lowering test expectations across semantic and lowering phases.

Overview
Out-of-range numeric literal diagnostics move from lowering (E3009) to semantic analysis (E2008), emitted when inference rewrites literals and patterns in apply_inference_rewriter via handle_literal_rewrite (only OutOfRange for suffix-less literals that were still NumericLiteral before rewrite).

Lowering no longer runs validate_literal or reports LoweringDiagnosticKind::LiteralError; match flow-control keeps out-of-range literal arms in the graph instead of dropping them and lifting filters. Constant evaluation drops duplicate literal checks in favor of the shared rewriter path.

Test fixtures now expect E2008 under semantic diagnostics and empty lowering literal errors where applicable.

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

… type

  A suffix-less numeric literal (`let _x: u8 = 256;`, `match x { 256 => .. }`)
  got a deferred `NumericLiteral` type var and was never value-validated — only
  suffixed (`256_u8`) and const literals were. Out-of-range inferred literals
  were silently accepted at semantic analysis and only caught later at lowering
  (E3009).

  Validate in `apply_inference_rewriter`: once a literal's type var is resolved,
  report `E2008` for an out-of-range value (`LiteralError::OutOfRange` only — an
  invalid literal type is already reported at conform time). The same
  `handle_literal_rewrite` path covers both expression and pattern literals, so
  these now error at semantic analysis instead of lowering.

  This is now the single validation point for source literals in all contexts,
  so the redundant per-literal check in const evaluation is dropped (const eval
  bails on a prior error; computed const values are still validated where they
  are evaluated). Add a regression test and refresh the lowering `literal`
  golden (E3009 → E2008).
@orizi
orizi force-pushed the orizi/06-05-fix_semantic_validate_inferred_numeric_literal_values_against_their_type branch from 4bde605 to 1850335 Compare June 5, 2026 08:47

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

Copy link
Copy Markdown

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: 4bde605df5

ℹ️ 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/expr/compute.rs Outdated

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

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 eytan-starkware).

@orizi
orizi added this pull request to the merge queue Jun 7, 2026
Merged via the queue into main with commit 5051041 Jun 7, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-05-fix_semantic_validate_inferred_numeric_literal_values_against_their_type branch June 7, 2026 11:25
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.

3 participants