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

Provide hint for assignment to &mut variable that should use * #36251

Closed
joshtriplett opened this issue Sep 3, 2016 · 2 comments
Closed

Provide hint for assignment to &mut variable that should use * #36251

joshtriplett opened this issue Sep 3, 2016 · 2 comments
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

@joshtriplett
Copy link
Member

Consider the following code:

fn main() {
    let mut x = 5;
    {
        let r = &mut x;
        r = 10;
    }
    println!("{}", x);
}

Compiling this produces the following error:

mutref.rs:5:13: 5:15 error: mismatched types [E0308]
mutref.rs:5         r = 10;
                        ^~
mutref.rs:5:13: 5:15 help: run `rustc --explain E0308` to see a detailed explanation
mutref.rs:5:13: 5:15 note: expected type `&mut _`
mutref.rs:5:13: 5:15 note:    found type `_`
error: aborting due to previous error

Ideally, this error should include a suggestion that the user probably meant to assign to *r.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 3, 2016
@steveklabnik steveklabnik added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

We now sort of do the opposite -- we suggest &mut 10 which is almost certainly not what the user wanted, I believe.

error[E0308]: mismatched types
 --> test.rs:5:13
  |
5 |         r = 10;
  |             ^^ expected mutable reference, found integral variable
  |
  = note: expected type `&mut {integer}`
             found type `{integer}`
  = help: try with `&mut 10`

@Mark-Simulacrum
Copy link
Member

Closing in favor of #33570.

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
Development

No branches or pull requests

3 participants