]> git.lizzy.rs Git - rust.git/commitdiff
Prevent incorrect help message for dereference suggestion
authorDonough Liu <ldm2993593805@163.com>
Sat, 20 Jun 2020 12:00:36 +0000 (20:00 +0800)
committerDonough Liu <ldm2993593805@163.com>
Sat, 20 Jun 2020 12:00:36 +0000 (20:00 +0800)
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
src/test/ui/traits/trait-suggest-deferences-multiple-0.fixed [new file with mode: 0644]
src/test/ui/traits/trait-suggest-deferences-multiple-0.rs [new file with mode: 0644]
src/test/ui/traits/trait-suggest-deferences-multiple-0.stderr [new file with mode: 0644]
src/test/ui/traits/trait-suggest-deferences-multiple-1.rs [new file with mode: 0644]
src/test/ui/traits/trait-suggest-deferences-multiple-1.stderr [new file with mode: 0644]
src/test/ui/traits/trait-suggest-deferences-multiple.fixed [deleted file]
src/test/ui/traits/trait-suggest-deferences-multiple.rs [deleted file]
src/test/ui/traits/trait-suggest-deferences-multiple.stderr [deleted file]

index 0c86e33884d6a30f7adf4982186a6a4e44bcdf22..176bd90303dddfe93230763329fd14597dd5a59e 100644 (file)
@@ -500,7 +500,7 @@ fn suggest_dereferences(
                     if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(span) {
                         // Don't care about `&mut` because `DerefMut` is used less
                         // often and user will not expect autoderef happens.
-                        if src.starts_with("&") {
+                        if src.starts_with("&") && !src.starts_with("&mut ") {
                             let derefs = "*".repeat(steps);
                             err.span_suggestion(
                                 span,
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple-0.fixed b/src/test/ui/traits/trait-suggest-deferences-multiple-0.fixed
new file mode 100644 (file)
index 0000000..b7160b7
--- /dev/null
@@ -0,0 +1,36 @@
+// run-rustfix
+use std::ops::Deref;
+
+trait Happy {}
+struct LDM;
+impl Happy for &LDM {}
+
+struct Foo(LDM);
+struct Bar(Foo);
+struct Baz(Bar);
+impl Deref for Foo {
+    type Target = LDM;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Bar {
+    type Target = Foo;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Baz {
+    type Target = Bar;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+fn foo<T>(_: T) where T: Happy {}
+
+fn main() {
+    let baz = Baz(Bar(Foo(LDM)));
+    foo(&***baz);
+    //~^ ERROR the trait bound `&Baz: Happy` is not satisfied
+}
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple-0.rs b/src/test/ui/traits/trait-suggest-deferences-multiple-0.rs
new file mode 100644 (file)
index 0000000..9ac5517
--- /dev/null
@@ -0,0 +1,36 @@
+// run-rustfix
+use std::ops::Deref;
+
+trait Happy {}
+struct LDM;
+impl Happy for &LDM {}
+
+struct Foo(LDM);
+struct Bar(Foo);
+struct Baz(Bar);
+impl Deref for Foo {
+    type Target = LDM;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Bar {
+    type Target = Foo;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Baz {
+    type Target = Bar;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+fn foo<T>(_: T) where T: Happy {}
+
+fn main() {
+    let baz = Baz(Bar(Foo(LDM)));
+    foo(&baz);
+    //~^ ERROR the trait bound `&Baz: Happy` is not satisfied
+}
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple-0.stderr b/src/test/ui/traits/trait-suggest-deferences-multiple-0.stderr
new file mode 100644 (file)
index 0000000..add34a5
--- /dev/null
@@ -0,0 +1,15 @@
+error[E0277]: the trait bound `&Baz: Happy` is not satisfied
+  --> $DIR/trait-suggest-deferences-multiple-0.rs:34:9
+   |
+LL | fn foo<T>(_: T) where T: Happy {}
+   |                          ----- required by this bound in `foo`
+...
+LL |     foo(&baz);
+   |         ^^^^
+   |         |
+   |         the trait `Happy` is not implemented for `&Baz`
+   |         help: consider adding dereference here: `&***baz`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple-1.rs b/src/test/ui/traits/trait-suggest-deferences-multiple-1.rs
new file mode 100644 (file)
index 0000000..91c6c79
--- /dev/null
@@ -0,0 +1,54 @@
+use std::ops::{Deref, DerefMut};
+
+trait Happy {}
+struct LDM;
+impl Happy for &mut LDM {}
+
+struct Foo(LDM);
+struct Bar(Foo);
+struct Baz(Bar);
+impl Deref for Foo {
+    type Target = LDM;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Bar {
+    type Target = Foo;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl Deref for Baz {
+    type Target = Bar;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+impl DerefMut for Foo {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.0
+    }
+}
+impl DerefMut for Bar {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.0
+    }
+}
+impl DerefMut for Baz {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.0
+    }
+}
+
+
+fn foo<T>(_: T) where T: Happy {}
+
+fn main() {
+    // Currently the compiler doesn't try to suggest dereferences for situations
+    // where DerefMut involves. So this test is meant to ensure compiler doesn't
+    // generate incorrect help message.
+    let mut baz = Baz(Bar(Foo(LDM)));
+    foo(&mut baz);
+    //~^ ERROR the trait bound `&mut Baz: Happy` is not satisfied
+}
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple-1.stderr b/src/test/ui/traits/trait-suggest-deferences-multiple-1.stderr
new file mode 100644 (file)
index 0000000..e90278f
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0277]: the trait bound `&mut Baz: Happy` is not satisfied
+  --> $DIR/trait-suggest-deferences-multiple-1.rs:52:9
+   |
+LL | fn foo<T>(_: T) where T: Happy {}
+   |                          ----- required by this bound in `foo`
+...
+LL |     foo(&mut baz);
+   |         ^^^^^^^^ the trait `Happy` is not implemented for `&mut Baz`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple.fixed b/src/test/ui/traits/trait-suggest-deferences-multiple.fixed
deleted file mode 100644 (file)
index b7160b7..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-// run-rustfix
-use std::ops::Deref;
-
-trait Happy {}
-struct LDM;
-impl Happy for &LDM {}
-
-struct Foo(LDM);
-struct Bar(Foo);
-struct Baz(Bar);
-impl Deref for Foo {
-    type Target = LDM;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-impl Deref for Bar {
-    type Target = Foo;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-impl Deref for Baz {
-    type Target = Bar;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-
-fn foo<T>(_: T) where T: Happy {}
-
-fn main() {
-    let baz = Baz(Bar(Foo(LDM)));
-    foo(&***baz);
-    //~^ ERROR the trait bound `&Baz: Happy` is not satisfied
-}
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple.rs b/src/test/ui/traits/trait-suggest-deferences-multiple.rs
deleted file mode 100644 (file)
index 9ac5517..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-// run-rustfix
-use std::ops::Deref;
-
-trait Happy {}
-struct LDM;
-impl Happy for &LDM {}
-
-struct Foo(LDM);
-struct Bar(Foo);
-struct Baz(Bar);
-impl Deref for Foo {
-    type Target = LDM;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-impl Deref for Bar {
-    type Target = Foo;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-impl Deref for Baz {
-    type Target = Bar;
-    fn deref(&self) -> &Self::Target {
-        &self.0
-    }
-}
-
-fn foo<T>(_: T) where T: Happy {}
-
-fn main() {
-    let baz = Baz(Bar(Foo(LDM)));
-    foo(&baz);
-    //~^ ERROR the trait bound `&Baz: Happy` is not satisfied
-}
diff --git a/src/test/ui/traits/trait-suggest-deferences-multiple.stderr b/src/test/ui/traits/trait-suggest-deferences-multiple.stderr
deleted file mode 100644 (file)
index f9b8bba..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-error[E0277]: the trait bound `&Baz: Happy` is not satisfied
-  --> $DIR/trait-suggest-deferences-multiple.rs:34:9
-   |
-LL | fn foo<T>(_: T) where T: Happy {}
-   |                          ----- required by this bound in `foo`
-...
-LL |     foo(&baz);
-   |         ^^^^
-   |         |
-   |         the trait `Happy` is not implemented for `&Baz`
-   |         help: consider adding dereference here: `&***baz`
-
-error: aborting due to previous error
-
-For more information about this error, try `rustc --explain E0277`.