Skip to content

Commit

Permalink
Omit 'missing IndexMut impl' suggestion when IndexMut is implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtwco committed Aug 30, 2018
1 parent 1ad2f5c commit 08a4a37
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/librustc_mir/borrow_check/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,27 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
}
) = &self.mir.basic_blocks()[location.block].terminator {
if self.tcx.parent(id) == self.tcx.lang_items().index_trait() {

let mut found = false;
self.tcx.for_each_relevant_impl(
self.tcx.lang_items().index_mut_trait().unwrap(),
substs.type_at(0),
|_relevant_impl| {
found = true;
}
);

let extra = if found {
String::from("")
} else {
format!(", but it is not implemented for `{}`",
substs.type_at(0))
};

err.help(
&format!(
"trait `IndexMut` is required to modify indexed content, \
but it is not implemented for `{}`",
substs.type_at(0),
"trait `IndexMut` is required to modify indexed content{}",
extra,
),
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/borrowck/index-mut-help-with-impl.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/index-mut-help-with-impl.rs:19:5
|
LL | Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
| ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
20 changes: 20 additions & 0 deletions src/test/ui/borrowck/index-mut-help-with-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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` and `IndexMut` but
// `Index::index` is being used specifically, the normal special help message
// should not mention a missing `IndexMut` impl.

fn main() {
use std::ops::Index;

let v = String::from("dinosaur");
Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
}
9 changes: 9 additions & 0 deletions src/test/ui/borrowck/index-mut-help-with-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0596]: cannot borrow immutable borrowed content as mutable
--> $DIR/index-mut-help-with-impl.rs:19:5
|
LL | Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
| ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.

0 comments on commit 08a4a37

Please sign in to comment.