-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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.
Description
I tried this code:
#[derive(Default)]
struct Foo { i: i32, j: i32 }
impl Foo {
fn modify_and_borrow(&mut self) -> &i32 { self.j += 1; &self.i }
}
fn main() {
let mut foo = Foo::default();
let mut refs = Vec::new();
for _ in 0..2 {
refs.push(foo.modify_and_borrow());
}
}
I expected to see this happen:
The error should say something like, "cannot borrow foo
as mutable while it is already borrowed as immutable."
Instead, this happened:
error[E0499]: cannot borrow `foo` as mutable more than once at a time
--> src/main.rs:12:19
|
12 | refs.push(foo.modify_and_borrow());
| ^^^ mutable borrow starts here in previous iteration of loop
Meta
Playground link:
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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.