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

bugfix(sierra-generator): represent user-defined phantom types as never - #10120

Merged
orizi merged 1 commit into
mainfrom
orizi/06-17-bugfix_sierra-generator_represent_user-defined_phantom_types_as_never_
Jun 17, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-17-bugfix_sierra-generator_represent_user-defined_phantom_types_as_never_

Conversation

@orizi

@orizi orizi commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

User-defined phantom structs and enums are now represented as the never type in Sierra, rather than using the dedicated Phantom long id. This allows containers such as Option<Ph> (where Ph is a #[phantom] enum) to specialize correctly without causing an ICE. Extern phantom types retain their Phantom long id, since their identity is required by the libfuncs that consume them (e.g. circuit gates).


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?

When a user-defined phantom type (struct or enum) appeared inside a generic container like Option<Ph>, the Sierra generator would ICE because the phantom type could not be represented in a way that allowed the container to specialize. Since user-defined phantom types are uninhabited, mapping them to never is semantically correct and allows the container to specialize without issues.


What was the behavior or documentation before?

User-defined phantom structs and enums were given a dedicated Phantom long id, the same as extern phantom types. Placing them inside a generic container (e.g. Option<Ph>) caused an ICE during Sierra generation.


What is the behavior or documentation after?

User-defined phantom structs and enums are mapped to the never type in Sierra. A container such as Option<Ph> now generates valid Sierra code, with the Some branch represented as enum_match<core::never> (an uninhabited match). Extern phantom types are unaffected and continue to use the Phantom long id.


Related issue or discussion (if any)

Fixes #10113


Additional context

A new test case (Test a user-defined phantom type in a container specializes (represented as never)) was added to generics to cover the Option<Ph> pattern and verify the generated Sierra output.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 17, 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 17, 2026 11:12
@cursor

cursor Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core type lowering in the Sierra generator; incorrect phantom classification could break circuit extern types or mis-specialize generics.

Overview
Fixes Sierra generation ICEs when user-defined #[phantom] structs/enums appear inside generic containers (e.g. Option<Ph>).

get_concrete_type_id no longer assigns the Phantom long id to all phantoms. Extern phantom types—and tuples/fixed-size arrays that contain them—still use Phantom, since circuit and other libfuncs depend on that identity. User phantom structs/enums are mapped to core::never so uninhabited types are representable and containers can specialize; tuples/arrays of user phantoms keep a normal struct shape with never fields.

New function-generator expectations cover Option<Ph> (enum_match on never in the Some branch) and Option<(Ph,)> (struct_deconstruct<Tuple<core::never>>).

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

@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: 769bb15e72

ℹ️ 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-sierra-generator/src/types.rs Outdated
…ver`

A `#[phantom]` struct/enum had no Sierra representation, so a type containing one
  (e.g. `Option<Ph>` built via `None`, including through a generic instantiation)
  failed to specialize and panicked the Sierra generator instead of compiling.

  Since a phantom type is uninhabited, represent user-defined phantom structs/enums
  as `never` (an empty, representable enum) in `get_concrete_type_id`, so containers
  of them specialize. Extern phantom types are left as-is (their `Phantom` long id):
  their identity is meaningful to the libfuncs that consume them, e.g. circuit gates.

  Fixes #10113.

@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 made 1 comment and resolved 1 discussion.
Reviewable status: 0 of 2 files reviewed, all discussions resolved (waiting on eytan-starkware and TomerStarkware).

Comment thread crates/cairo-lang-sierra-generator/src/types.rs Outdated
@orizi
orizi force-pushed the orizi/06-17-bugfix_sierra-generator_represent_user-defined_phantom_types_as_never_ branch from 769bb15 to 60c7d62 Compare June 17, 2026 13:01

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

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 60c7d62. Configure here.

Comment thread crates/cairo-lang-sierra-generator/src/types.rs

@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 made 1 comment.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).

Comment thread crates/cairo-lang-sierra-generator/src/types.rs

@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: 0 of 2 files reviewed, all discussions resolved (waiting on eytan-starkware and 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 eytan-starkware).

@orizi
orizi added this pull request to the merge queue Jun 17, 2026
Merged via the queue into main with commit aa4d403 Jun 17, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-17-bugfix_sierra-generator_represent_user-defined_phantom_types_as_never_ branch June 17, 2026 15:13
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: Generic instantiated with phantom argument crashes sierra-gen

3 participants