Skip to content

Commit 08a4a37

Browse files
committed
Omit 'missing IndexMut impl' suggestion when IndexMut is implemented.
1 parent 1ad2f5c commit 08a4a37

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/librustc_mir/borrow_check/mutability_errors.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,27 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
423423
}
424424
) = &self.mir.basic_blocks()[location.block].terminator {
425425
if self.tcx.parent(id) == self.tcx.lang_items().index_trait() {
426+
427+
let mut found = false;
428+
self.tcx.for_each_relevant_impl(
429+
self.tcx.lang_items().index_mut_trait().unwrap(),
430+
substs.type_at(0),
431+
|_relevant_impl| {
432+
found = true;
433+
}
434+
);
435+
436+
let extra = if found {
437+
String::from("")
438+
} else {
439+
format!(", but it is not implemented for `{}`",
440+
substs.type_at(0))
441+
};
442+
426443
err.help(
427444
&format!(
428-
"trait `IndexMut` is required to modify indexed content, \
429-
but it is not implemented for `{}`",
430-
substs.type_at(0),
445+
"trait `IndexMut` is required to modify indexed content{}",
446+
extra,
431447
),
432448
);
433449
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0596]: cannot borrow data in a `&` reference as mutable
2+
--> $DIR/index-mut-help-with-impl.rs:19:5
3+
|
4+
LL | Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
5+
| ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
6+
|
7+
= help: trait `IndexMut` is required to modify indexed content
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// When mutably indexing a type that implements `Index` and `IndexMut` but
12+
// `Index::index` is being used specifically, the normal special help message
13+
// should not mention a missing `IndexMut` impl.
14+
15+
fn main() {
16+
use std::ops::Index;
17+
18+
let v = String::from("dinosaur");
19+
Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0596]: cannot borrow immutable borrowed content as mutable
2+
--> $DIR/index-mut-help-with-impl.rs:19:5
3+
|
4+
LL | Index::index(&v, 1..2).make_ascii_uppercase(); //~ ERROR
5+
| ^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0596`.

0 commit comments

Comments
 (0)