Skip to content

Commit

Permalink
Add help message for missing IndexMut impl
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LukasKalbertodt committed Jul 28, 2018
1 parent 45b48b9 commit 9d59c6b
Showing 1 changed file with 23 additions and 0 deletions.
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,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(_)) => {
Expand Down

0 comments on commit 9d59c6b

Please sign in to comment.