Skip to content

refactor(semantic): Apply full type-based diagnostics to all signature parts.#10170

Merged
orizi merged 1 commit into
mainfrom
orizi/06-28-refactor_semantic_apply_full_type-based_diagnostics_to_all_signature_parts
Jun 29, 2026
Merged

refactor(semantic): Apply full type-based diagnostics to all signature parts.#10170
orizi merged 1 commit into
mainfrom
orizi/06-28-refactor_semantic_apply_full_type-based_diagnostics_to_all_signature_parts

Conversation

@orizi

@orizi orizi commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

Introduces add_value_type_based_diagnostics, a new function that wraps add_type_based_diagnostics and additionally rejects phantom types when they appear in value positions (parameters, return types, implicit parameters, closure params, and expressions). The existing add_type_based_diagnostics is now reserved for type definition contexts (struct members, enum variants) where phantom types are permitted.

Previously, phantom type checks were scattered across multiple call sites — compute.rs, functions.rs, and compute_expr_closure_semantic — each manually calling is_phantom and reporting InstancesOfPhantomTypes before or after calling add_type_based_diagnostics. This also caused duplicate diagnostics: for example, a phantom type used as a function parameter would be reported both at the signature site and again when the parameter expression was analyzed.


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?

Phantom type diagnostics were being emitted multiple times for the same usage. For instance, using Option<Ph> as a parameter type would produce an E2019 error at the signature and a second one at the expression referencing the parameter. The fix consolidates phantom type checking into add_value_type_based_diagnostics, which is called at the canonical location for each value position, eliminating the duplicate reports.


What was the behavior or documentation before?

Using a phantom type in a function parameter, return type, or expression could produce multiple E2019: Phantom types cannot be instantiated diagnostics pointing to different locations for the same underlying type usage.


What is the behavior or documentation after?

Each phantom type usage in a value position produces exactly one E2019 diagnostic, reported at the most specific and relevant source location (the parameter, return type annotation, or expression site). The analyzed_types set in apply_inference_rewriter is pre-seeded with types from the function signature so that expression-level checks do not re-report errors already covered by the signature.


Related issue or discussion (if any)

N/A


Additional context

The add_type_based_diagnostics function is retained for use in type definition contexts where phantom types are valid. The new add_value_type_based_diagnostics function short-circuits to InstancesOfPhantomTypes if the type is phantom, and otherwise delegates to add_type_based_diagnostics for structural checks (infinite-size types, arrays of zero-sized or phantom elements).

…e parts.

Introduce `add_value_type_based_diagnostics`, which reports a phantom type
used as a value and otherwise delegates to `add_type_based_diagnostics`.
Route function parameters, return types, implicits, expressions and closure
signatures through it, so signatures now also surface the non-phantom type
errors (e.g. infinite size) that were previously only caught in the body.

Seed the signature's parameter, implicit and return types as already
analyzed so the body no longer re-diagnoses them, and drop the now-redundant
inline phantom checks scattered across the constructor and signature paths.

orizi commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator Author

@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
Diagnostic-only refactor in the semantic layer; behavior change is fewer duplicate errors, not altered type checking or codegen.

Overview
Centralizes phantom-type and structural type checks for value positions in a new add_value_type_based_diagnostics, which reports InstancesOfPhantomTypes when needed and otherwise delegates to add_type_based_diagnostics. add_type_based_diagnostics stays for type-definition sites (struct members, enum variants) where phantoms are allowed.

Call sites in function signatures (params, return, implicits), closures, and post-inference expression rewriting now use the helper instead of ad-hoc is_phantom plus separate structural checks. apply_inference_rewriter pre-seeds its per-type dedup set with signature param, implicit, and return types so phantom errors already reported on the signature are not emitted again when analyzing uses of those types in the body (e.g. referencing a phantom-typed parameter).

Several redundant phantom checks are removed (enum variant ctor, struct ctor, duplicate inline phantom reporting in expression paths). Snapshot tests drop extra E2019 on parameter use sites, keeping one diagnostic per value position.

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

@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 7151918. Configure here.

Comment thread crates/cairo-lang-semantic/src/expr/compute.rs

@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 5 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and orizi).

@orizi orizi added this pull request to the merge queue Jun 29, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jun 29, 2026
@orizi orizi added this pull request to the merge queue Jun 29, 2026
Merged via the queue into main with commit 808c713 Jun 29, 2026
55 checks passed
@orizi orizi deleted the orizi/06-28-refactor_semantic_apply_full_type-based_diagnostics_to_all_signature_parts branch June 29, 2026 13:25
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.

3 participants