-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[WIP] Suggest use mut when mutating a non mutable local #51260
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1716,10 +1716,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { | |||||||||||||
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir); | ||||||||||||||
err.span_label(span, "cannot borrow as mutable"); | ||||||||||||||
|
||||||||||||||
if place != place_err { | ||||||||||||||
if let Some(name) = self.describe_place(place_err) { | ||||||||||||||
err.note(&format!("the value which is causing this path not to be \ | ||||||||||||||
mutable is...: `{}`", name)); | ||||||||||||||
if let &Place::Local(local) = place_err { | ||||||||||||||
let local_decl = &self.mir.local_decls[local]; | ||||||||||||||
if local_decl.is_user_variable && local_decl.mutability == Mutability::Not { | ||||||||||||||
err.span_label(local_decl.source_info.span, | ||||||||||||||
format!("consider changing this to `mut {}`", local_decl.name.unwrap())); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I was noting on Zulip at 4:40pm (ET) yesterday, I think we want to move this suggestion code so that it comes after the match. That will require restructuring this method somewhat. The problem with the current setup is that your suggestion will only fire when you are trying to borrow something, but not — I think — in tests like let mut err = match kind {
Reservation(...) | Write(...) => {
if no error { return } else { construct_err_object() }
}
Reservation(...) | Write(...) => /* some variant of the preivous arm */,
Reservation(...) | Write(...) => ...,
};
// If we get here, then there *is* an error
match place_err {
Place::Local(l) => {
// suggest changing to `mut` variable
}
Place::Projection(...) => {
// suggest changing to `&mut` borrow (see [link 1])
}
}
err.emit() link 1 is rust/src/librustc_mir/borrow_check/mod.rs Lines 1723 to 1728 in 61f35e5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are also going to want to watch out for "upvars" — those are cases where the |
||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
error[E0596]: cannot borrow immutable item `x` as mutable | ||
--> $DIR/issue-37139.rs:22:18 | ||
| | ||
LL | TestEnum::Item(ref mut x) => { | ||
| --------- consider changing this to `mut x` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah we definitely don't want to suggest this... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, need to look at this more carefully. |
||
LL | test(&mut x); //~ ERROR cannot borrow immutable | ||
| ^^^^^^ cannot borrow as mutable | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we should use the
span_suggestion
method. Something like this (I took the prompt etc from AST borrowck):This will encode the suggestion into the JSON etc so that tools like rustfix or IDEs can apply it automatically.