Skip to content

Only suggest reborrowing a moved value where the reborrow is valid#157196

Open
onehr wants to merge 1 commit into
rust-lang:mainfrom
onehr:fix-borrowck-reborrow-suggestion-fn-closure-150175
Open

Only suggest reborrowing a moved value where the reborrow is valid#157196
onehr wants to merge 1 commit into
rust-lang:mainfrom
onehr:fix-borrowck-reborrow-suggestion-fn-closure-150175

Conversation

@onehr
Copy link
Copy Markdown
Contributor

@onehr onehr commented May 31, 2026

Fixes #150175.

Moving a &mut-typed value out of a closure (e.g. for foo in foos, whose implicit into_iter consumes foos) reports E0507. borrowck then suggests reborrowing the move site as &mut *foos, marked Applicability::MachineApplicable — but an Fn closure only holds its captures through &self, so the reborrow does not compile:

error[E0596]: cannot borrow `*foos` as mutable, as it is a captured variable in a `Fn` closure

The previous code only checked that the moved value was a &mut, not whether it could actually be reborrowed mutably in the current context. This gates the suggestion on borrowck's own is_mutable check for the reborrowed place (*moved_place) — the same predicate that decides whether &mut *moved_place would emit E0596 — so the suggestion is offered exactly when it compiles:

  • Fn-closure capture (reached through &self) → suppressed;
  • FnMut-closure capture (reached through &mut self) → still offered (the reborrow is valid there);
  • ordinary &mut local/param → still offered (unchanged).

The regression test exercises both the Fn case (no suggestion) and the FnMut case (suggestion kept), pinning down the precise behavior.

When a `&mut`-typed value is moved out of a closure via a `for` loop's
implicit `into_iter`, borrowck suggests "consider creating a fresh
reborrow" (`&mut *place`) with `Applicability::MachineApplicable`. For a
value captured by an `Fn` closure -- which holds its captures through
`&self` -- that place cannot be borrowed mutably (E0596), so applying the
machine-applicable suggestion produces code that does not compile.

Gate the suggestion on borrowck's own `is_mutable` check for the
reborrowed place, so it is only offered where `&mut *place` is valid:
suppressed for `Fn`-closure captures, still offered for `FnMut` closures
and ordinary `&mut` locals.
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2026
@onehr onehr marked this pull request as ready for review May 31, 2026 15:23
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 31, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 31, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 31, 2026

r? @mejrs

rustbot has assigned @mejrs.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E0507 possibly invalid suggestion to create fresh reborrow for Fn vs FnOnce issue

3 participants