bugfix(semantic): Reject arrays of phantom elements.#10171
Conversation
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.
|
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.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
PR SummaryLow Risk Overview In New semantic tests cover Reviewed by Cursor Bugbot for commit 34a8d80. Bugbot is set up for automated code reviews on this repo. Configure here. |
eytan-starkware
left a comment
There was a problem hiding this comment.
@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
left a comment
There was a problem hiding this comment.
@eytan-starkware made 1 comment and resolved 1 discussion.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).
TomerStarkware
left a comment
There was a problem hiding this comment.
@TomerStarkware reviewed all commit messages and made 1 comment.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on orizi).

Summary
When an
Arraycontains a phantom type, the compiler now emitsInstancesOfPhantomTypes(E2019) instead ofArrayOfZeroSizedElements. 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:
Why is this change needed?
Array<Ph>wherePhis 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>wherePhis a non-zero-sized phantom type produced no diagnostic from the array check path.Array<Ph>wherePhis an empty (zero-sized) phantom struct producedArrayOfZeroSizedElementsinstead ofInstancesOfPhantomTypes.What is the behavior or documentation after?
Array<Ph>for any phantom type consistently produceserror[E2019]: Phantom types cannot be instantiated.ArrayOfZeroSizedElementsdiagnostic 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.