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

Add help message for missing IndexMut impl #52788

Merged
merged 3 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}
}
}

db
}
BorrowViolation(euv::ClosureCapture(_)) => {
Expand All @@ -901,6 +902,28 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
}
};

// We add a special note about `IndexMut`, if the source of this error
// is the fact that `Index` is implemented, but `IndexMut` is not. Needing
// to implement two traits for "one operator" is not very intuitive for
// many programmers.
if err.cmt.note == mc::NoteIndex {
let node_id = self.tcx.hir.hir_to_node_id(err.cmt.hir_id);
let node = self.tcx.hir.get(node_id);

// This pattern probably always matches.
if let hir_map::NodeExpr(
hir::Expr { node: hir::ExprKind::Index(lhs, _), ..}
) = node {
let ty = self.tables.expr_ty(lhs);

db.help(&format!(
"trait `IndexMut` is required to modify indexed content, but \
it is not implemented for `{}`",
ty
));
}
}

self.note_and_explain_mutbl_error(&mut db, &err, &error_span);
self.note_immutability_blame(
&mut db,
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/borrowck/index-mut-help.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/index-mut-help.rs:21:5
|
LL | map["peter"].clear(); //~ ERROR
| ^^^^^^^^^^^^ cannot borrow as mutable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add the message to the NLL errors? Eventually we'll switch over and lose the new note otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do that in the past couple of hours and now I really don't know how to proceed.

So I'm pretty sure I have to add my code to report_mutability_error() in librustc_mir/borrow_check/mutability_errors.rs. But I don't know how I can get the information whether this error was caused by an index operation. I have access to the following things:

  • access_place and the_place_err: these were my biggest hope of getting information about the expression causing the error. Sadly, in all three cases mentioned in this thread, they are ProjectionElem::Deref { base: Place::Local } which doesn't help me. I would have expected one of them to be a ProjectionElem::Index.
  • span and error_access: not helpful AFAICT
  • location: it's only a reference to the enclosing basic block, right? So not really helpful either
  • self.mir_def_id: this is a DefId and thus references a definition of an item, right? So I guess it references the enclosing function. So not useful either.
  • self.{other_fields}: AFAICT most of them are global structures and nothing pointing to the expression in question. I don't think anything is helpful here.

So I'm stuck and can't invest a lot more time into this. Could someone help me to get this done fairly quickly? If you people in this thread don't know about this stuff, could you ping someone who does know the code? Or tell me where I should go and ask about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProjectionElem::Index is only for simple arrays and slices. Everything else goes through the IndexMut/Index trait methods. I'm assuming rustc decides on Index due to the lack of IndexMut and then tries to take a mutable reference to the result of the method call (which is where your Deref comes from: &mut Index::index(&map, "peter"))

cc @rust-lang/wg-compiler-nll any ideas how to do this in the MIR borrowchecker? Right now rustc (with the nll feature gate active) produces

error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/main.rs:7:5
  |
7 |     map["peter"].clear();           //~ ERROR
  |     ^^^^^^^^^^^^ cannot borrow as mutable

which actually is less informative than the message of old borrowck even before this PR: cannot borrow immutable indexed content as mutable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the thing to do would be to look at the & reference and try to figure out where it came from. It'll probably be a temporary here, which is a good indicator that we should try to get more info about it. We'd see it as the result of an Index call -- that is probably a good signal for us to insert this advice, since unless the call to Index was manually written by the user, it must imply that IndexMut does not exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth filing a bug for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice! Would it be possible to create a bug about this and then merge this PR as is? I don't think I will have time to work on this anytime soon. Then someone else can work on adding the same advice to the NLL version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #53228 to follow up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)


error[E0594]: cannot assign to data in a `&` reference
--> $DIR/index-mut-help.rs:22:5
|
LL | map["peter"] = "0".to_string(); //~ ERROR
| ^^^^^^^^^^^^ cannot assign

error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/index-mut-help.rs:23:13
|
LL | let _ = &mut map["peter"]; //~ ERROR
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error: aborting due to 3 previous errors

Some errors occurred: E0594, E0596.
For more information about an error, try `rustc --explain E0594`.
24 changes: 24 additions & 0 deletions src/test/ui/borrowck/index-mut-help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// When mutably indexing a type that implements `Index` but not `IndexMut`, a
// special 'help' message is added to the output.


fn main() {
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("peter", "23".to_string());

map["peter"].clear(); //~ ERROR
map["peter"] = "0".to_string(); //~ ERROR
let _ = &mut map["peter"]; //~ ERROR
}
28 changes: 28 additions & 0 deletions src/test/ui/borrowck/index-mut-help.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0596]: cannot borrow immutable indexed content as mutable
--> $DIR/index-mut-help.rs:21:5
|
LL | map["peter"].clear(); //~ ERROR
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`

error[E0594]: cannot assign to immutable indexed content
--> $DIR/index-mut-help.rs:22:5
|
LL | map["peter"] = "0".to_string(); //~ ERROR
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`

error[E0596]: cannot borrow immutable indexed content as mutable
--> $DIR/index-mut-help.rs:23:18
|
LL | let _ = &mut map["peter"]; //~ ERROR
| ^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`

error: aborting due to 3 previous errors

Some errors occurred: E0594, E0596.
For more information about an error, try `rustc --explain E0594`.
2 changes: 2 additions & 0 deletions src/test/ui/issue-41726.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0596]: cannot borrow immutable indexed content as mutable
|
LL | things[src.as_str()].sort(); //~ ERROR cannot borrow immutable
| ^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>`

error: aborting due to previous error

Expand Down