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

chore(corelib): drop internal #[panic_with] usages - #10095

Merged
orizi merged 1 commit into
mainfrom
orizi/06-14-chore_corelib_drop_internal_panic_with_usages
Jun 16, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-14-chore_corelib_drop_internal_panic_with_usages

Conversation

@orizi

@orizi orizi commented Jun 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Removes all #[panic_with(...)] attribute usages from array.cairo and integer.cairo, replacing the generated panic wrapper functions with explicit expect(...) calls or direct delegation at the call sites.

  • array_at (generated from #[panic_with('Index out of bounds', array_at)] on array_get) is replaced with array_get(...).expect('Index out of bounds').unbox() in ArrayImpl::at, and call sites in ArrayIndex, SpanImpl::at, and SpanIndex are updated to delegate through ArrayTrait::at or self.snapshot.at(index).
  • Integer panic wrappers (u128_from_felt252, u128_sub, u128_as_non_zero, u8_from_felt252, u8_as_non_zero, u16_from_felt252, u16_as_non_zero, u32_from_felt252, u32_as_non_zero, u64_from_felt252, u64_as_non_zero, u256_sub, u256_as_non_zero) are removed by dropping their #[panic_with(...)] attributes from the corresponding _try_* functions.

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 #[panic_with(...)] attribute generates a secondary function that panics on None, but this pattern bypasses the standard Option/Result handling idioms and creates implicit, hard-to-trace panic paths. Removing these attributes and replacing usages with explicit expect(...) calls makes the panic behavior visible at the call site and consistent with idiomatic Cairo error handling.


What was the behavior or documentation before?

Functions like array_get, u128_try_from_felt252, u128_checked_sub, etc. had #[panic_with(...)] attributes that auto-generated wrapper functions (e.g., array_at, u128_from_felt252, u128_sub) which would panic with a fixed message if the underlying Option returned None.


What is the behavior or documentation after?

The generated wrapper functions are removed. Call sites that previously used them now use the _try_* / _checked_* variants directly with explicit .expect(...) calls, or delegate through a single canonical implementation, making panic behavior explicit and traceable.


Related issue or discussion (if any)

N/A


Additional context

This is a breaking change for any downstream code that directly calls the now-removed generated functions (e.g., array_at, u128_from_felt252, u128_sub, u256_sub, u*_as_non_zero). Callers should migrate to the corresponding _try_* or _checked_* variants with explicit error handling.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

@cursor

cursor Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core array indexing and integer conversion/zero checks with a breaking API surface for code that called the removed #[panic_with] wrappers; runtime panic messages should match but behavior is now explicit at call sites.

Overview
Removes #[panic_with(...)] from array.cairo and integer.cairo, so compiler-generated panic wrappers are no longer produced for those Option-returning helpers.

For arrays, ArrayImpl::at now calls array_get(...).expect('Index out of bounds') instead of array_at. ArrayIndex, SpanImpl::at, and SpanIndex route through ArrayTrait::at / self.snapshot.at so bounds panics stay in one place.

For integers, attributes are dropped from u*_try_from_felt252, u*_try_as_non_zero, and checked helpers such as u128_checked_sub / u256_checked_sub. Public Add/Sub/Mul and TryInto<NonZero<...>> paths already use the _try_* / _checked_* APIs with explicit .expect(...); callers that imported the old generated names (e.g. u128_from_felt252, u128_sub, u*_as_non_zero) must switch to those variants.

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

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

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

@orizi
orizi force-pushed the orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features branch from 30760d2 to a25d6f4 Compare June 15, 2026 16:18
@orizi
orizi force-pushed the orizi/06-14-chore_corelib_drop_internal_panic_with_usages branch from ccf3121 to 063248d Compare June 15, 2026 16:18
@orizi
orizi force-pushed the orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features branch from a25d6f4 to 61f97b4 Compare June 16, 2026 07:00
@orizi
orizi force-pushed the orizi/06-14-chore_corelib_drop_internal_panic_with_usages branch from 063248d to 6cbbfb3 Compare June 16, 2026 07:00
`#[panic_with]` is deprecated. Remove its uses in corelib, which also drops the
  panicking wrappers it generated (`array_at`, `u128_from_felt252`, `u128_sub`,
  `u*_as_non_zero`, `u256_sub`, ...).

  `Array`/`Span` indexing now calls `array_get(...).expect('Index out of bounds')`
  and the index/at impls delegate to each other instead of the generated wrapper.
  The integer helpers were internal and unused elsewhere, so they are simply
  removed. No behavior change — panic messages are preserved.
@orizi
orizi changed the base branch from orizi/06-14-feat_defs_populate_macropluginmetadata_allowed_features to graphite-base/10095 June 16, 2026 10:46
@orizi
orizi force-pushed the graphite-base/10095 branch from 61f97b4 to 4409061 Compare June 16, 2026 10:46
@orizi
orizi force-pushed the orizi/06-14-chore_corelib_drop_internal_panic_with_usages branch from 6cbbfb3 to 0335928 Compare June 16, 2026 10:46
@orizi
orizi changed the base branch from graphite-base/10095 to main June 16, 2026 10:46
@orizi
orizi enabled auto-merge June 16, 2026 10:46
@orizi
orizi added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 6e4bec5 Jun 16, 2026
106 checks passed
@orizi
orizi deleted the orizi/06-14-chore_corelib_drop_internal_panic_with_usages branch June 16, 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.

4 participants