-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
"use of moved value" diagnostic could suggest reborrow when passing &mut to &mut #62112
Copy link
Copy link
Closed
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Spawned off of #45142
Consider the following code (play):
Today, this yields the diagnostic:
We could do better. For example, the diagnostic could additionally say:
Background:
The compiler tries hard to auto-inject
&mut-reborrows when it makes sense; as a way to create a fresh&mutreference to pass into the method invocation, and avoid moving the pre-existing&mutreference.But there are times, often involving generic code, when it cannot automatically inject a reborrow. (After all, there are cases where the intent is to move the borrow into some place whose dynamic extent outlives that of the method invocation itself; and the static analysis cannot tell if this was the case at the point where it is deciding whether to insert an
&mut-reborrow or not.)This can lead to a diagnostic for the user telling them that they have a used of a moved value.
But new-comers to Rust do not always know about the option of putting in a
&mut-reborrow themselves.Our diagnostics could help them here. Even though we choose to not attempt to auto-inject the auto-reborrow in the compiler itself, our diagnostics have enough information about the method signature and the invocation to suggest to the user that they write out the
&mut *valuethemselves.