-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-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.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
It is a common mistake, especially for beginners, to attempt to reborrow function arguments when calling other functions.
I tried this code:
struct T;
fn foo(x: &mut T) {}
fn bar(x: &mut T) {
foo(&mut x)
}
I expected to see this happen: Compile error following what the above code does if foo(&x)
is attempted instead.
error[Exxxx]: mismatched types
--> src/lib.rs:6:9
|
6 | foo(&mut x)
| ^^ expected `&mut T`, found `&mut &mut T`
|
= note; you don't need to use `&mut` when you already have a mutable reference
Instead, this happened: Misleading error.
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> src/lib.rs:6:9
|
5 | fn bar(x: &mut T) {
| - help: consider changing this to be mutable: `mut x`
6 | foo(&mut x)
| ^^^^^^ cannot borrow as mutable
Meta
1.45.2 srable & 1.47.0 nightly on the playground
yaymukund and arifd
Metadata
Metadata
Assignees
Labels
A-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.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.