Only suggest reborrowing a moved value where the reborrow is valid#157196
Open
onehr wants to merge 1 commit into
Open
Only suggest reborrowing a moved value where the reborrow is valid#157196onehr wants to merge 1 commit into
onehr wants to merge 1 commit into
Conversation
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.
Collaborator
|
r? @mejrs rustbot has assigned @mejrs. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #150175.
Moving a
&mut-typed value out of a closure (e.g.for foo in foos, whose implicitinto_iterconsumesfoos) reportsE0507. borrowck then suggests reborrowing the move site as&mut *foos, markedApplicability::MachineApplicable— but anFnclosure only holds its captures through&self, so the reborrow does not compile: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 ownis_mutablecheck for the reborrowed place (*moved_place) — the same predicate that decides whether&mut *moved_placewould emitE0596— 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);&mutlocal/param → still offered (unchanged).The regression test exercises both the
Fncase (no suggestion) and theFnMutcase (suggestion kept), pinning down the precise behavior.