Skip to content

bugfix(semantic): Reject arrays of phantom elements.#10171

Open
orizi wants to merge 1 commit into
orizi/06-28-refactor_semantic_apply_full_type-based_diagnostics_to_all_signature_partsfrom
orizi/06-28-bugfix_semantic_reject_arrays_of_phantom_elements
Open

bugfix(semantic): Reject arrays of phantom elements.#10171
orizi wants to merge 1 commit into
orizi/06-28-refactor_semantic_apply_full_type-based_diagnostics_to_all_signature_partsfrom
orizi/06-28-bugfix_semantic_reject_arrays_of_phantom_elements

Conversation

@orizi

@orizi orizi commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

When an Array contains a phantom type, the compiler now emits InstancesOfPhantomTypes (E2019) instead of ArrayOfZeroSizedElements. Previously, phantom types that happened to be zero-sized (e.g., empty phantom structs) would incorrectly trigger the zero-sized array diagnostic rather than the phantom type diagnostic. The check now explicitly tests for phantom types first, falling back to the zero-sized check only for non-phantom types.


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?

Array<Ph> where Ph is a phantom type was either not diagnosed correctly or diagnosed with the wrong error. Phantom types should always produce E2019 (InstancesOfPhantomTypes) when used as array element types, regardless of whether they are also zero-sized. An empty phantom struct (#[phantom] struct Ph {}) would previously fall through to the zero-sized diagnostic, masking the real issue.


What was the behavior or documentation before?

  • Array<Ph> where Ph is a non-zero-sized phantom type produced no diagnostic from the array check path.
  • Array<Ph> where Ph is an empty (zero-sized) phantom struct produced ArrayOfZeroSizedElements instead of InstancesOfPhantomTypes.

What is the behavior or documentation after?

  • Array<Ph> for any phantom type consistently produces error[E2019]: Phantom types cannot be instantiated.
  • The ArrayOfZeroSizedElements diagnostic is only emitted for zero-sized types that are not phantom types.

Related issue or discussion (if any)

N/A


Additional context

Three new test cases were added covering: phantom enums as array element types, empty phantom structs as array element types, and phantom types used as array element types inside struct members.

An `Array<T>` whose element type is phantom has no runtime representation,
so report it as an instance of a phantom type. When the element is both
phantom and zero-sized (e.g. an empty phantom struct), prefer the phantom
diagnostic over the zero-sized-element one.

orizi commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@orizi orizi marked this pull request as ready for review June 28, 2026 14:31
@cursor

cursor Bot commented Jun 28, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Localized change to type diagnostic ordering in the semantic layer with regression tests; no runtime or codegen behavior change beyond clearer errors.

Overview
Array<T> type positions now treat a phantom element type as invalid before considering zero-sized elements, so users always get error[E2019]: Phantom types cannot be instantiated instead of the wrong ArrayOfZeroSizedElements when T is an empty phantom struct.

In add_type_based_diagnostics, the Array extern check calls arg_ty.is_phantom(db) first and reports InstancesOfPhantomTypes; ArrayOfZeroSizedElements is only emitted for non-phantom zero-sized element types.

New semantic tests cover Array<Ph> in function signatures (phantom enum and empty phantom struct), and Array<Ph> as a struct field.

Reviewed by Cursor Bugbot for commit 34a8d80. 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.

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


crates/cairo-lang-semantic/src/expr/test_data/attributes line 266 at r1 (raw file):

test_function_diagnostics(expect_diagnostics: true)

//! > function_code

How is this different then test 1?

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

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