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

Possible improvement in mutability error message #36412

Closed
alexcrichton opened this issue Sep 12, 2016 · 0 comments · Fixed by #37863
Closed

Possible improvement in mutability error message #36412

alexcrichton opened this issue Sep 12, 2016 · 0 comments · Fixed by #37863
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@alexcrichton
Copy link
Member

Talking to some folks at RustConf one particularly confusing case was "where do I put mut?" to get mutability. We've got a few locations, to place it (e.g. on the binding or in the type of a reference), and this can be confusing when you're just starting out. As a specific case, consider:

pub fn foo(mut a: &String)  {
    a.push_str("foo");
}

which when compiled yields the error message:

error: cannot borrow immutable borrowed content `*a` as mutable
 --> foo.rs:2:5
  |
2 |     a.push_str("foo");
  |     ^

error: aborting due to previous error

To a beginner this can be quite confusing because a is mutable, but the fix here is to take &mut String, not &String. Perhaps we can detect this and provide a more tailored error message for cases like this?

cc @jonathandturner

@alexcrichton alexcrichton added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 12, 2016
bors added a commit that referenced this issue Nov 29, 2016
add hint to fix error for immutable ref in arg

fix  #36412 part of #35233
r? @jonathandturner
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant