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

bugfix(syntax): fix numeric_value_and_suffix to handle compound suffixes; error on typed literals in consteval_int and test attrs. - #9927

Merged
orizi merged 1 commit into
mainfrom
orizi/05-14-bugfix_syntax_fix_numeric_value_and_suffix_to_handle_compound_suffixes_error_on_typed_literals_in_consteval_int_and_test_attrs
May 17, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/05-14-bugfix_syntax_fix_numeric_value_and_suffix_to_handle_compound_suffixes_error_on_typed_literals_in_consteval_int_and_test_attrs

Conversation

@orizi

@orizi orizi commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replaces numeric_value_and_suffix returning Option<(BigInt, Option<SmolStrId>)> with a direct (BigInt, Option<SmolStrId>) return type. The new implementation scans _-separated segments from right to left, finding the longest valid numeric prefix and treating the remainder as the suffix. This correctly handles compound suffixes like 1_u32_u8 (returning value 1 with suffix "u32_u8") and ambiguous radix cases like 0x1_f32 (parsed as hex 0x1F32 with no suffix). Malformed literals such as 0x_u32 return (BigInt::ZERO, Some(full_token_text)) as a sentinel.

Callers that previously used .unwrap_or_default() now receive the value directly and check suffix.is_some() to detect errors. The consteval_int macro and available_gas test attribute now emit explicit diagnostics when a typed (suffixed) literal is used where only plain integers are valid.


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?

The previous rsplit_once('_') approach only split at the last _, so compound suffixes like 1_u32_u8 were mishandled — u32_u8 was not recognized as a suffix, and the numeric parse would silently fail or return a wrong value via unwrap_or_default(). Additionally, callers that used unwrap_or_default() would silently swallow parse failures rather than surfacing them as diagnostics.


What was the behavior or documentation before?

numeric_value_and_suffix returned Option<(BigInt, Option<SmolStrId>)>, splitting only at the rightmost _. Compound suffixes like 1_u32_u8 were not correctly identified. Callers used .unwrap_or_default(), silently producing 0 on failure. The consteval_int macro and gas-value extraction did not emit diagnostics for typed literals.


What is the behavior or documentation after?

numeric_value_and_suffix returns (BigInt, Option<SmolStrId>) directly, scanning from right to left across all _ boundaries to find the longest valid numeric prefix. Compound suffixes are correctly extracted. The consteval_int macro emits "Literals with suffix are not supported in consteval_int macro" and available_gas emits "Literals with suffix are not supported as gas values" when a suffixed literal is encountered.


Related issue or discussion (if any)

Fixes #9926


Additional context

New test cases cover 1_u32_u8 and -1_u32_u8 for compound suffixes in literal parsing, and consteval_int!(1_u32) / consteval_int!(1_u32 + 2) for typed literals inside the consteval_int macro.

…xes; error on typed literals in consteval_int and test attrs.
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi marked this pull request as ready for review May 14, 2026 13:11
@cursor

cursor Bot commented May 14, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes low-level literal parsing used across the compiler, which could affect type inference and diagnostics for numeric literals in many code paths. Adds new hard errors for suffixed literals in consteval_int and available_gas, potentially breaking previously-accepted inputs.

Overview
Updates TerminalLiteralNumber::numeric_value_and_suffix to always return (value, suffix) and to scan _-separated segments from right to left, correctly extracting compound suffixes (e.g. 1_u32_u8) and avoiding radix ambiguities (e.g. 0x1_f32). Call sites in semantic expression lowering are updated to stop silently defaulting malformed parses.

Tightens validation in plugin/test utilities: consteval_int! now emits an explicit error when given a suffixed literal (including inside subexpressions), and the test plugin rejects suffixed literals for available_gas (and ignores them for should_panic(expected=...)). Adds regression tests covering compound suffix literals and the new consteval_int! diagnostics.

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

@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 6 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@orizi
orizi added this pull request to the merge queue May 17, 2026
Merged via the queue into main with commit 90871c0 May 17, 2026
54 checks passed
@orizi
orizi deleted the orizi/05-14-bugfix_syntax_fix_numeric_value_and_suffix_to_handle_compound_suffixes_error_on_typed_literals_in_consteval_int_and_test_attrs branch May 17, 2026 15:37
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: Multi-segment numeric literal suffix silently compiles to zero

3 participants