Skip to content

Commit 764d472

Browse files
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.
1 parent 9d59c6b commit 764d472

File tree

1 file changed

+22
-22
lines changed
  • src/librustc_borrowck/borrowck

1 file changed

+22
-22
lines changed

src/librustc_borrowck/borrowck/mod.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -882,28 +882,6 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
882882
}
883883
}
884884

885-
// We add a special note about `IndexMut`, if the source of this error
886-
// is the fact that `Index` is implemented, but `IndexMut` is not. Needing
887-
// to implement two traits for "one operator" is not very intuitive for
888-
// many programmers.
889-
if err.cmt.note == mc::NoteIndex {
890-
let node_id = self.tcx.hir.hir_to_node_id(err.cmt.hir_id);
891-
let node = self.tcx.hir.get(node_id);
892-
893-
// This pattern probably always matches.
894-
if let hir_map::NodeExpr(
895-
hir::Expr { node: hir::ExprKind::Index(lhs, _), ..}
896-
) = node {
897-
let ty = self.tables.expr_ty(lhs);
898-
899-
db.help(&format!(
900-
"trait `IndexMut` is required to modify indexed content, but \
901-
it is not implemented for {}",
902-
ty
903-
));
904-
}
905-
}
906-
907885
db
908886
}
909887
BorrowViolation(euv::ClosureCapture(_)) => {
@@ -924,6 +902,28 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
924902
}
925903
};
926904

905+
// We add a special note about `IndexMut`, if the source of this error
906+
// is the fact that `Index` is implemented, but `IndexMut` is not. Needing
907+
// to implement two traits for "one operator" is not very intuitive for
908+
// many programmers.
909+
if err.cmt.note == mc::NoteIndex {
910+
let node_id = self.tcx.hir.hir_to_node_id(err.cmt.hir_id);
911+
let node = self.tcx.hir.get(node_id);
912+
913+
// This pattern probably always matches.
914+
if let hir_map::NodeExpr(
915+
hir::Expr { node: hir::ExprKind::Index(lhs, _), ..}
916+
) = node {
917+
let ty = self.tables.expr_ty(lhs);
918+
919+
db.help(&format!(
920+
"trait `IndexMut` is required to modify indexed content, but \
921+
it is not implemented for `{}`",
922+
ty
923+
));
924+
}
925+
}
926+
927927
self.note_and_explain_mutbl_error(&mut db, &err, &error_span);
928928
self.note_immutability_blame(
929929
&mut db,

0 commit comments

Comments
 (0)