From 9d59c6b76e96005201d3b66dae946cc01dc5e565 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 27 Jul 2018 20:44:09 +0200 Subject: [PATCH 1/3] Add help message for missing `IndexMut` impl Before this change it simply says "cannot assign to immutable indexed content". This might be confusing for Rust beginners, as implementing two traits for "one operator" is not intuitive. --- src/librustc_borrowck/borrowck/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 3ae1e5aac5076..97348c168de8c 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -881,6 +881,29 @@ 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 + )); + } + } + db } BorrowViolation(euv::ClosureCapture(_)) => { From 764d472b1f04041adfcf91b4a5486712b6de41f2 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 7 Aug 2018 22:38:14 +0200 Subject: [PATCH 2/3] Make `IndexMut` note apply to more cases Previously it was only emitted for assigments, which was an unnecessary restriction. Now it doesn't care where the mutability comes from. This commit also adds `` quotes around the printed type. --- src/librustc_borrowck/borrowck/mod.rs | 44 +++++++++++++-------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 97348c168de8c..2e16e316a18bd 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -882,28 +882,6 @@ 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 - )); - } - } - db } BorrowViolation(euv::ClosureCapture(_)) => { @@ -924,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, From 24abef3689840ec2ad0bb6ccdbc7cbcfb3844a82 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 7 Aug 2018 23:09:08 +0200 Subject: [PATCH 3/3] Add and update tests for `IndexMut` help message --- .../ui/borrowck/index-mut-help.nll.stderr | 22 +++++++++++++++ src/test/ui/borrowck/index-mut-help.rs | 24 ++++++++++++++++ src/test/ui/borrowck/index-mut-help.stderr | 28 +++++++++++++++++++ src/test/ui/issue-41726.stderr | 2 ++ 4 files changed, 76 insertions(+) create mode 100644 src/test/ui/borrowck/index-mut-help.nll.stderr create mode 100644 src/test/ui/borrowck/index-mut-help.rs create mode 100644 src/test/ui/borrowck/index-mut-help.stderr diff --git a/src/test/ui/borrowck/index-mut-help.nll.stderr b/src/test/ui/borrowck/index-mut-help.nll.stderr new file mode 100644 index 0000000000000..cc058f1fde5da --- /dev/null +++ b/src/test/ui/borrowck/index-mut-help.nll.stderr @@ -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 + +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`. diff --git a/src/test/ui/borrowck/index-mut-help.rs b/src/test/ui/borrowck/index-mut-help.rs new file mode 100644 index 0000000000000..a4df0ced2f292 --- /dev/null +++ b/src/test/ui/borrowck/index-mut-help.rs @@ -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 or the MIT license +// , 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 +} diff --git a/src/test/ui/borrowck/index-mut-help.stderr b/src/test/ui/borrowck/index-mut-help.stderr new file mode 100644 index 0000000000000..b8b35ed3ed30a --- /dev/null +++ b/src/test/ui/borrowck/index-mut-help.stderr @@ -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`. diff --git a/src/test/ui/issue-41726.stderr b/src/test/ui/issue-41726.stderr index 172309537bf04..c79196e014042 100644 --- a/src/test/ui/issue-41726.stderr +++ b/src/test/ui/issue-41726.stderr @@ -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>` error: aborting due to previous error