]> git.lizzy.rs Git - rust.git/commitdiff
Omit 'missing IndexMut impl' suggestion when IndexMut is implemented.
authorDavid Wood <david@davidtw.co>
Thu, 30 Aug 2018 22:54:04 +0000 (00:54 +0200)
committerDavid Wood <david@davidtw.co>
Thu, 30 Aug 2018 22:54:04 +0000 (00:54 +0200)
src/librustc_mir/borrow_check/mutability_errors.rs
src/test/ui/borrowck/index-mut-help-with-impl.nll.stderr [new file with mode: 0644]
src/test/ui/borrowck/index-mut-help-with-impl.rs [new file with mode: 0644]
src/test/ui/borrowck/index-mut-help-with-impl.stderr [new file with mode: 0644]

index 38cdd17a7273cf9049d47601043c226888c94385..78ab772d9ad5b2f3ed5fd9cbd2cbf26d62f53c50 100644 (file)
@@ -423,11 +423,27 @@ pub(super) fn report_mutability_error(
                             }
                         ) = &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,
                                     ),
                                 );
                             }
diff --git a/src/test/ui/borrowck/index-mut-help-with-impl.nll.stderr b/src/test/ui/borrowck/index-mut-help-with-impl.nll.stderr
new file mode 100644 (file)
index 0000000..8e7ffa6
--- /dev/null
@@ -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`.
diff --git a/src/test/ui/borrowck/index-mut-help-with-impl.rs b/src/test/ui/borrowck/index-mut-help-with-impl.rs
new file mode 100644 (file)
index 0000000..a5bab48
--- /dev/null
@@ -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
+}
diff --git a/src/test/ui/borrowck/index-mut-help-with-impl.stderr b/src/test/ui/borrowck/index-mut-help-with-impl.stderr
new file mode 100644 (file)
index 0000000..9c28b86
--- /dev/null
@@ -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`.