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

Taking a mutable reference to self in a closure leads to suggestion to change "self" in function signature #111554

Closed
simon-frankau opened this issue May 14, 2023 · 1 comment · Fixed by #112019
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@simon-frankau
Copy link

Code

struct Test {
}

impl Test {
    pub fn test(&mut self) {
        // Removing || changes the suggestion to 
        || test2(&mut self);
    }
}

fn test2(_: &mut Test) {
}

Current output

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
   --> src/sound_player.rs:870:11
    |
869 |     pub fn test(&mut self) {
    |                      ---- help: consider changing this to be mutable: `mut self`
870 |     || test2(&mut self);
    |              ^^^^^^^^^ cannot borrow as mutable

Desired output

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
   --> src/sound_player.rs:870:8
    |
870 |     test2(&mut self);
    |           ^^^^^^^^^ cannot borrow as mutable
    |
note: the binding is already a mutable borrow
   --> src/sound_player.rs:869:17
    |
869 |     pub fn test(&mut self) {
    |                 ^^^^^^^^^
help: try removing `&mut` here
    |
870 -     test2(&mut self);
870 +     test2(self);

Rationale and extra context

The output is already good without a closure. Somehow, putting the call in a closure changes the error message to one that's less helpful.

Other cases

Removing the "||" fixes the suggestion.

Anything else?

I can't think of anything. I'm happy to answer any questions you have.

I'm building through cargo, so I'm not 100% sure of the rustc version, but I get:

$ rustc --version
rustc 1.66.0 (69f9c33d7 2022-12-12)
@simon-frankau simon-frankau added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 14, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 6, 2023
…errors

Don't suggest changing `&self` and `&mut self` in function signature to be mutable when taking `&mut self` in closure

Current suggestion for when taking a mutable reference to `self` in a closure (as an upvar) will produce a machine-applicable suggestion to change the `self` in the function signature to `mut self`, but does not account for the specialness of implicit self in that it can already have `&` and `&mut` (see rust-lang#111554). This causes the function signature to become `test(&mut mut self)` which does not seem desirable.

```
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
   --> src/sound_player.rs:870:11
    |
869 |     pub fn test(&mut self) {
    |                      ---- help: consider changing this to be mutable: `mut self`
870 |     || test2(&mut self);
    |              ^^^^^^^^^ cannot borrow as mutable
```

This PR suppresses the "changing this to be mutable" suggestion if the implicit self is either `ImplicitSelfKind::ImmRef` or `ImplicitSelfKind::MutRef`.

Fixes rust-lang#111554.
@bors bors closed this as completed in 21e7463 Jun 6, 2023
@simon-frankau
Copy link
Author

Thanks!

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
1 participant