From 3c4bc8c8ad4eabed2650208e7037ad01170d7897 Mon Sep 17 00:00:00 2001 From: ThePuzzlemaker Date: Tue, 1 Dec 2020 12:57:19 -0600 Subject: [PATCH 1/3] Ignore trait implementations with negative polarity when suggesting trait implementations in diagnostics --- .../src/traits/error_reporting/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index fe4127fd4d8b9..9feba7bfc492f 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1310,10 +1310,20 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return None; } } + if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative { + return None; + } Some(imp) }) .collect(), - None => all_impls.map(|def_id| self.tcx.impl_trait_ref(def_id).unwrap()).collect(), + None => all_impls + .filter_map(|def_id| { + if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative { + return None; + } + self.tcx.impl_trait_ref(def_id) + }) + .collect(), } } From 859463b869ae25c40e2a6b5c9574acdfb0185258 Mon Sep 17 00:00:00 2001 From: ThePuzzlemaker Date: Tue, 1 Dec 2020 13:04:08 -0600 Subject: [PATCH 2/3] Fix UI tests for negative polarity implementations of traits --- .../ui/associated-types/hr-associated-type-bound-1.stderr | 1 - .../associated-types/hr-associated-type-bound-object.stderr | 1 - .../hr-associated-type-bound-param-1.stderr | 1 - .../hr-associated-type-bound-param-2.stderr | 3 --- .../hr-associated-type-bound-param-3.stderr | 1 - .../hr-associated-type-bound-param-4.stderr | 1 - .../hr-associated-type-bound-param-5.stderr | 4 ---- .../hr-associated-type-bound-param-6.stderr | 1 - src/test/ui/async-await/issue-64130-3-other.stderr | 2 -- .../typeck-default-trait-impl-constituent-types-2.stderr | 2 -- .../typeck-default-trait-impl-constituent-types.stderr | 3 --- .../auto-traits/typeck-default-trait-impl-negation.stderr | 6 ------ 12 files changed, 26 deletions(-) diff --git a/src/test/ui/associated-types/hr-associated-type-bound-1.stderr b/src/test/ui/associated-types/hr-associated-type-bound-1.stderr index c9255c91d2d02..042223f3e9587 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-1.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-1.stderr @@ -12,7 +12,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to previous error diff --git a/src/test/ui/associated-types/hr-associated-type-bound-object.stderr b/src/test/ui/associated-types/hr-associated-type-bound-object.stderr index 225b18a3b0454..848924bf58eff 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-object.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-object.stderr @@ -12,7 +12,6 @@ LL | fn f<'a, T: X<'a> + ?Sized>(x: &>::U) { | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to previous error diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-1.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-1.stderr index 7af261e4b3d4f..d35c127926431 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-1.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-1.stderr @@ -12,7 +12,6 @@ LL | type V = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to previous error diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr index b48ff97f1211b..0afa340f7a148 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr @@ -12,7 +12,6 @@ LL | for<'b> >::W: Clone, | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> >::W: Clone` is not satisfied --> $DIR/hr-associated-type-bound-param-2.rs:16:14 @@ -28,7 +27,6 @@ LL | type W = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> >::W: Clone` is not satisfied --> $DIR/hr-associated-type-bound-param-2.rs:4:8 @@ -44,7 +42,6 @@ LL | for<'b> >::W: Clone, | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to 3 previous errors diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-3.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-3.stderr index 48c4d77dcc7d4..79e1e00e98d77 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-3.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-3.stderr @@ -12,7 +12,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to previous error diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-4.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-4.stderr index 111ca8566b195..e23ac5f56503b 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-4.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-4.stderr @@ -12,7 +12,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to previous error diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-5.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-5.stderr index df25f7ac953e7..38909be59f237 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-5.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-5.stderr @@ -12,7 +12,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> as X<'b, Vec>>::U: Clone` is not satisfied --> $DIR/hr-associated-type-bound-param-5.rs:27:14 @@ -28,7 +27,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> as X<'b, Vec>>::U: Clone` is not satisfied --> $DIR/hr-associated-type-bound-param-5.rs:33:14 @@ -44,7 +42,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> as X<'b, Box>>::U: Clone` is not satisfied --> $DIR/hr-associated-type-bound-param-5.rs:33:14 @@ -60,7 +57,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error: aborting due to 4 previous errors diff --git a/src/test/ui/associated-types/hr-associated-type-bound-param-6.stderr b/src/test/ui/associated-types/hr-associated-type-bound-param-6.stderr index 2efdb2445af21..6e02b42e514c5 100644 --- a/src/test/ui/associated-types/hr-associated-type-bound-param-6.stderr +++ b/src/test/ui/associated-types/hr-associated-type-bound-param-6.stderr @@ -12,7 +12,6 @@ LL | type U = str; | = help: the following implementations were found: <&T as Clone> - <&mut T as Clone> error[E0277]: the trait bound `for<'b> T: X<'b, T>` is not satisfied --> $DIR/hr-associated-type-bound-param-6.rs:12:12 diff --git a/src/test/ui/async-await/issue-64130-3-other.stderr b/src/test/ui/async-await/issue-64130-3-other.stderr index 4bf43f14cc195..9ffdd0524c663 100644 --- a/src/test/ui/async-await/issue-64130-3-other.stderr +++ b/src/test/ui/async-await/issue-64130-3-other.stderr @@ -10,8 +10,6 @@ LL | async fn bar() { LL | is_qux(bar()); | ^^^^^^ within `impl Future`, the trait `Qux` is not implemented for `Foo` | - = help: the following implementations were found: - note: future does not implement `Qux` as this value is used across an await --> $DIR/issue-64130-3-other.rs:18:5 | diff --git a/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr b/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr index 53ba9b8a3f6b4..c2eab1a33b91a 100644 --- a/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr +++ b/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr @@ -7,8 +7,6 @@ LL | fn is_mytrait() {} LL | is_mytrait::<(MyS2, MyS)>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ within `(MyS2, MyS)`, the trait `MyTrait` is not implemented for `MyS2` | - = help: the following implementations were found: - = note: required because it appears within the type `(MyS2, MyS)` error: aborting due to previous error diff --git a/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types.stderr b/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types.stderr index bc50000498463..efb6bde17990f 100644 --- a/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types.stderr +++ b/src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types.stderr @@ -6,9 +6,6 @@ LL | fn is_mytrait() {} ... LL | is_mytrait::(); | ^^^^ the trait `MyTrait` is not implemented for `MyS2` - | - = help: the following implementations were found: - error: aborting due to previous error diff --git a/src/test/ui/auto-traits/typeck-default-trait-impl-negation.stderr b/src/test/ui/auto-traits/typeck-default-trait-impl-negation.stderr index 76a6994cb009a..dae87bc221a6f 100644 --- a/src/test/ui/auto-traits/typeck-default-trait-impl-negation.stderr +++ b/src/test/ui/auto-traits/typeck-default-trait-impl-negation.stderr @@ -6,9 +6,6 @@ LL | fn is_my_trait() {} ... LL | is_my_trait::(); | ^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `ThisImplsUnsafeTrait` - | - = help: the following implementations were found: - error[E0277]: the trait bound `ThisImplsTrait: MyUnsafeTrait` is not satisfied --> $DIR/typeck-default-trait-impl-negation.rs:25:26 @@ -18,9 +15,6 @@ LL | fn is_my_unsafe_trait() {} ... LL | is_my_unsafe_trait::(); | ^^^^^^^^^^^^^^ the trait `MyUnsafeTrait` is not implemented for `ThisImplsTrait` - | - = help: the following implementations were found: - error: aborting due to 2 previous errors From b287806d389d3bc94470c27568ad16dc015769a5 Mon Sep 17 00:00:00 2001 From: ThePuzzlemaker Date: Tue, 1 Dec 2020 13:29:03 -0600 Subject: [PATCH 3/3] Add regression test for #79458 --- src/test/ui/traits/issue-79458.rs | 10 ++++++++++ src/test/ui/traits/issue-79458.stderr | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/ui/traits/issue-79458.rs create mode 100644 src/test/ui/traits/issue-79458.stderr diff --git a/src/test/ui/traits/issue-79458.rs b/src/test/ui/traits/issue-79458.rs new file mode 100644 index 0000000000000..a41add6a1ccea --- /dev/null +++ b/src/test/ui/traits/issue-79458.rs @@ -0,0 +1,10 @@ +// Negative implementations should not be shown in trait suggestions. +// This is a regression test of #79458. + +#[derive(Clone)] +struct Foo<'a, T> { + bar: &'a mut T + //~^ ERROR the trait bound `&mut T: Clone` is not satisfied +} + +fn main() {} diff --git a/src/test/ui/traits/issue-79458.stderr b/src/test/ui/traits/issue-79458.stderr new file mode 100644 index 0000000000000..54947b57c03b1 --- /dev/null +++ b/src/test/ui/traits/issue-79458.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `&mut T: Clone` is not satisfied + --> $DIR/issue-79458.rs:6:5 + | +LL | bar: &'a mut T + | ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `&mut T` + | + = help: the following implementations were found: + <&T as Clone> + = note: `Clone` is implemented for `&T`, but not for `&mut T` + = note: required by `clone` + = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`.