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

fix(lowering): eliminate spurious E3002 alongside E3001 for moved struct members - #9994

Merged
orizi merged 1 commit into
mainfrom
orizi/05-28-fix_lowering_eliminate_spurious_e3002_alongside_e3001_for_moved_struct_members
May 28, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/05-28-fix_lowering_eliminate_spurious_e3002_alongside_e3001_for_moved_struct_members

Conversation

@orizi

@orizi orizi commented May 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When a non-copy variable is moved (used more than once), the borrow checker previously created a fresh dummy variable to represent the already-moved value. This dummy variable had no corresponding lowering statement, which caused spurious E3002 ("Variable not dropped") errors to appear alongside the real E3001 ("Variable was previously moved") error. Additionally, when assembling scattered (destructured) struct values that contained a moved member, the reconstructed struct variable was discarded and a new dummy variable was returned, causing the struct reconstruction statements to be emitted but their result to be orphaned.

The fix changes MovedVar to carry the original VariableId instead of just the TypeId. When a move error is detected, the original variable ID is reused rather than allocating a new dummy. When assembling a scattered struct that contains a moved member, the struct is still reconstructed (so the statements are valid), and the resulting variable ID is propagated through the MovedVar error. A new test case (Spurious E3002 alongside E3001 for orphaned inner struct construction) is added to confirm the fix.


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 non-copy variable was used after being moved, the compiler emitted a spurious E3002 error in addition to the correct E3001 error. This happened because a dummy variable was created to stand in for the moved value, but no statement ever defined that dummy variable, leaving it "undropped" from the borrow checker's perspective. Similarly, struct reconstruction statements were being emitted for orphaned variables, producing inconsistent lowering IR.


What was the behavior or documentation before?

  • A use-after-move of a non-copy variable produced both E3001 (variable was previously moved) and a spurious E3002 (variable not dropped) diagnostic.
  • The E3002 note incorrectly pointed to the second use site and cited a potential panic as the reason, rather than pointing to the divergence caused by the if branch.
  • Scattered struct assembly discarded the reconstructed variable when a member was moved, leaving orphaned struct_construct statements in the lowered IR.

What is the behavior or documentation after?

  • A use-after-move of a non-copy variable produces only the correct E3001 diagnostic.
  • The E3002 note now correctly identifies the divergence (e.g., an if block) rather than a panic site.
  • Scattered struct assembly always emits and uses the reconstructed variable, keeping the lowered IR consistent.

Related issue or discussion (if any)

Bug repro captured in the new test: Spurious E3002 alongside E3001 for orphaned inner struct construction.


Additional context

The PanicDestruct trait note was also removed from the E3002 diagnostic output, as it is no longer emitted in the updated test expectations.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented May 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

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

@orizi
orizi marked this pull request as ready for review May 28, 2026 08:29
@orizi
orizi added this pull request to the merge queue May 28, 2026
@cursor

cursor Bot commented May 28, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core lowering move semantics and borrow-check IR expectations; behavior change is localized to error paths but affects all non-copy use-after-move and scattered struct reassembly.

Overview
MovedVar now tracks the real lowered VariableId instead of only a type, so use-after-move no longer allocates an extra undefined variable that borrow checking treated as undropped.

When a scattered (destructured) struct is reassembled and a member was already moved, lowering still emits struct_construct and threads the reconstructed id through the move error instead of discarding it—fixing orphaned inner reconstructions and the bogus E3002 next to E3001 on nested paths like outer after consume(outer.b).

Borrow-check snapshots are updated accordingly, including a new regression case and refreshed expectations (returns/remaps use the original moved vars; one E3002 case now points at control-flow divergence rather than a panic note).

Reviewed by Cursor Bugbot for commit 439b76f. 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: 513bc9906b

ℹ️ 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-lowering/src/lower/refs.rs

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

Comment thread crates/cairo-lang-lowering/src/lower/refs.rs
@orizi
orizi removed this pull request from the merge queue due to a manual request May 28, 2026
@orizi
orizi force-pushed the orizi/05-28-fix_lowering_eliminate_spurious_e3002_alongside_e3001_for_moved_struct_members branch from 513bc99 to 439b76f Compare May 28, 2026 10:01

@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 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue May 28, 2026
Merged via the queue into main with commit 6a9d40a May 28, 2026
54 checks passed
@orizi
orizi deleted the orizi/05-28-fix_lowering_eliminate_spurious_e3002_alongside_e3001_for_moved_struct_members branch May 29, 2026 16:32
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