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 message with closures #47774

Closed
est31 opened this issue Jan 26, 2018 · 4 comments
Closed

Confusing error message with closures #47774

est31 opened this issue Jan 26, 2018 · 4 comments
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

@est31
Copy link
Member

est31 commented Jan 26, 2018

struct Foo {
    u :u32,
}
impl Foo {
    fn foo(&mut self) {
        || {
            let r = &mut self;
            r.u += 1;
        };
    }
}

gives

error[E0595]: closure cannot assign to immutable argument `self`
 --> src/main.rs:6:9
  |
6 |         || {
  |         ^^
  |         |
  |         cannot reborrow mutably
  |         try removing `&mut` here

error: aborting due to previous error

error: Could not compile `playground`.

Which is very cryptic and doesn't really show what's going on.

@est31
Copy link
Member Author

est31 commented Jan 26, 2018

cc @estebank

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 26, 2018
@cuviper cuviper added 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. labels Jan 27, 2018
@estebank
Copy link
Contributor

Just created a PR to address part of the wording. The full fix would be to have the span pointing at the reborrow instead of the closure. Even when changing that, the diagnostic should still point at the current span to provide the context of the closure.

The MIR output is slightly better in that we get an error pointing at the reborrow, but it is still not great:

% rustc file.rs -Zborrowck=compare
error[E0596]: cannot borrow immutable item `self` as mutable (Mir)
 --> file.rs:7:21
  |
7 |             let r = &mut self;
  |                     ^^^^^^^^^ cannot borrow as mutable
  |
  = note: Value not mutable causing this error: `self`

error[E0595]: closure cannot assign to immutable argument `self` (Ast)
 --> file.rs:6:9
  |
5 |     fn foo(&mut self) {
  |            --------- given this existing mutable borrow...
6 |         || {
  |         ^^ cannot reborrow immutable argument `self` mutably in this closure

error[E0596]: cannot borrow immutable item `self` as mutable (Mir)
 --> file.rs:6:9
  |
6 | /         || {
7 | |             let r = &mut self;
8 | |             r.u += 1;
9 | |         };
  | |_________^ cannot borrow as mutable

error: aborting due to 3 previous errors

@crlf0710
Copy link
Member

Just suggesting changing &mut self to &mut *self for this pattern is enough, i believe...

@est31 est31 closed this as completed Oct 19, 2018
@rust-lang rust-lang locked and limited conversation to collaborators Oct 19, 2018
@estebank
Copy link
Contributor

Why was this ticket closed (and locked)? The subpar diagnostics are still being emitted...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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

No branches or pull requests

4 participants