-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Add error help to "cannot borrow ... as mutable more than once at a time" error that recommends assigning to a variable first #77834
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 lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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 lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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.
A newcomer to Rust may have some code that looks like this (playground):
Which you would think would work, since the call to
self.foo()happens afterself.bar()returns, and so they have unique access toself. However, this desugars into something like this:Which, due to something else in the Rust aliasing rules that can be confusing, means that there are actually two unique borrows of
selflive at the same time, and produces this error:However, this error is confusing because the error is only visible when the code is desugared. Because of how the code is desugared, this code – which is functionally equivalent – compiles fine (playground):
Therefore I propose adding a help message to the error that suggests extracting the result into a temporary. Something like this:
Cc @jyn514