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

Fix invalid "ref mut mut" sugestion #37531

Merged
merged 1 commit into from Nov 12, 2016
Merged

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Nov 2, 2016

Change output from:

error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
11 |         TestEnum::Item(ref mut x) => {
   |                        --------- use `ref mut mut x` here to make mutable
12 |             test(&mut x);
   |                       ^ cannot borrow mutably

to

error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
12 |             test(&mut x);
   |                       ^
   |                       |
   |                       cannot reborrow mutably
   |                       try removing `&mut` here

Fixes #37139, #34337, #34126

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@estebank estebank force-pushed the fix-ref-mut-mut branch 2 times, most recently from 7498116 to 5495774 Compare November 2, 2016 06:52
@estebank
Copy link
Contributor Author

estebank commented Nov 7, 2016

r? @jonathandturner

@rust-highfive rust-highfive assigned sophiajt and unassigned pnkfelix Nov 7, 2016
@sophiajt
Copy link
Contributor

sophiajt commented Nov 8, 2016

I need to see the full example, but this:

error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
12 |             test(&mut x);
   |                       ^
   |                       |
   |                       cannot reborrow mutably
   |                       try removing `&mut` here

Seems like it might not be correct. If you remove &mut and the function expects a borrow, won't it still error?

@estebank
Copy link
Contributor Author

estebank commented Nov 8, 2016

@jonathandturner the original code was (from #37139):

enum TestEnum {
    Item(i32),
}

fn test(x: &mut i32) {
    println!("hi! {:?}", x);
}

fn main() {
    let mut x = TestEnum::Item(10);
    match x {
        TestEnum::Item(ref mut x) => {
            test(&mut x);
        }
    }
}

When compiling it fails saying that x cannot be borrowed as mutable, while the same code without &mut works correctly, as x is already of type &mut i32.

@alexcrichton alexcrichton added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 10, 2016
@alexcrichton
Copy link
Member

Oh dear that is indeed a confusing error message (ref mut mut)! Perhaps we could test if a reference is needed and assuage @jonathandturner's concerns by not mentioning this change when it would just lead to another compile error?

(although that may just end up being too convoluted)

@estebank
Copy link
Contributor Author

@alexcrichton @jonathandturner I haven't been able to come up with a case where a wrong suggestion would be given. In the cases where it'd be wrong to remove the &mut the compiler throws a type error, never reaching this NOTE.

@sophiajt
Copy link
Contributor

This at least seems like a net improvement, especially if @estebank 's comment that another error will catch the bad case for us.

@bors r+

@bors
Copy link
Contributor

bors commented Nov 12, 2016

📌 Commit 1a5456b has been approved by jonathandturner

@bors
Copy link
Contributor

bors commented Nov 12, 2016

⌛ Testing commit 1a5456b with merge 026add5...

bors added a commit that referenced this pull request Nov 12, 2016
Fix invalid "ref mut mut" sugestion

Change output from:

```nocode
error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
11 |         TestEnum::Item(ref mut x) => {
   |                        --------- use `ref mut mut x` here to make mutable
12 |             test(&mut x);
   |                       ^ cannot borrow mutably
```

to

```nocode
error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
12 |             test(&mut x);
   |                       ^
   |                       |
   |                       cannot reborrow mutably
   |                       try removing `&mut` here
```
Fixes #37139, #34337, #34126
@bors bors merged commit 1a5456b into rust-lang:master Nov 12, 2016
@estebank estebank deleted the fix-ref-mut-mut branch November 9, 2023 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants