Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error when trying to re-borrow a function argument #75871

Closed
JaniM opened this issue Aug 24, 2020 · 2 comments · Fixed by #77110
Closed

Confusing error when trying to re-borrow a function argument #75871

JaniM opened this issue Aug 24, 2020 · 2 comments · Fixed by #77110
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JaniM
Copy link
Contributor

JaniM commented Aug 24, 2020

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

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=768e5a2d690393f89aed38be3fa5dee6

@JaniM JaniM added the C-bug Category: This is a bug. label Aug 24, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Aug 24, 2020
@pickfire
Copy link
Contributor

pickfire commented Aug 24, 2020

Seemed similar to #71389 except that is *x = xxx but this is f(x). I believe it may even be possible to have a fix for this like Maybe try removing '&mut '?

@estebank
Copy link
Contributor

It not incorrect, just stylistically suspect given the circumstances. &mut x will reborrow, and it is what to use if you were calling foo multiple times. Only in this case, where you only need to pass x around once is the suggestion not what you want to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants