Skip to content

Commit d37afad

Browse files
committed
Auto merge of #79606 - ThePuzzlemaker:issue-79458-fix, r=scottmcm
Do not show negative polarity trait implementations in diagnostic messages for similar implementations This fixes #79458. Previously, this code: ```rust #[derive(Clone)] struct Foo<'a, T> { x: &'a mut T, } ``` would have suggested that `<&mut T as Clone>` was an implementation that was found. This is due to the fact that the standard library now has `impl<'_, T> !Clone for &'_ mut T`, and explicit negative polarity implementations were not filtered out in diagnostic output when suggesting similar implementations. This PR fixes this issue by filtering out negative polarity trait implementations in `find_similar_impl_candidates` within `rustc_trait_selection::traits::error_reporting::InferCtxtPrivExt<'tcx>`. It also adds a UI regression test for this issue and fixes UI tests that had incorrectly been modified to expect the invalid output. r? `@scottmcm`
2 parents 92e4fb0 + b287806 commit d37afad

15 files changed

+36
-27
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,20 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
13101310
return None;
13111311
}
13121312
}
1313+
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative {
1314+
return None;
1315+
}
13131316
Some(imp)
13141317
})
13151318
.collect(),
1316-
None => all_impls.map(|def_id| self.tcx.impl_trait_ref(def_id).unwrap()).collect(),
1319+
None => all_impls
1320+
.filter_map(|def_id| {
1321+
if self.tcx.impl_polarity(def_id) == ty::ImplPolarity::Negative {
1322+
return None;
1323+
}
1324+
self.tcx.impl_trait_ref(def_id)
1325+
})
1326+
.collect(),
13171327
}
13181328
}
13191329

Diff for: src/test/ui/associated-types/hr-associated-type-bound-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type U = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/associated-types/hr-associated-type-bound-object.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | fn f<'a, T: X<'a> + ?Sized>(x: &<T as X<'a>>::U) {
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type V = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-2.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | for<'b> <T as Z<'b, u16>>::W: Clone,
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error[E0277]: the trait bound `for<'b> <u16 as Z<'b, u16>>::W: Clone` is not satisfied
1817
--> $DIR/hr-associated-type-bound-param-2.rs:16:14
@@ -28,7 +27,6 @@ LL | type W = str;
2827
|
2928
= help: the following implementations were found:
3029
<&T as Clone>
31-
<&mut T as Clone>
3230

3331
error[E0277]: the trait bound `for<'b> <u16 as Z<'b, u16>>::W: Clone` is not satisfied
3432
--> $DIR/hr-associated-type-bound-param-2.rs:4:8
@@ -44,7 +42,6 @@ LL | for<'b> <T as Z<'b, u16>>::W: Clone,
4442
|
4543
= help: the following implementations were found:
4644
<&T as Clone>
47-
<&mut T as Clone>
4845

4946
error: aborting due to 3 previous errors
5047

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-3.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type U = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-4.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type U = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-5.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type U = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error[E0277]: the trait bound `for<'b> <Vec<T> as X<'b, Vec<T>>>::U: Clone` is not satisfied
1817
--> $DIR/hr-associated-type-bound-param-5.rs:27:14
@@ -28,7 +27,6 @@ LL | type U = str;
2827
|
2928
= help: the following implementations were found:
3029
<&T as Clone>
31-
<&mut T as Clone>
3230

3331
error[E0277]: the trait bound `for<'b> <Vec<T> as X<'b, Vec<T>>>::U: Clone` is not satisfied
3432
--> $DIR/hr-associated-type-bound-param-5.rs:33:14
@@ -44,7 +42,6 @@ LL | type U = str;
4442
|
4543
= help: the following implementations were found:
4644
<&T as Clone>
47-
<&mut T as Clone>
4845

4946
error[E0277]: the trait bound `for<'b> <Box<T> as X<'b, Box<T>>>::U: Clone` is not satisfied
5047
--> $DIR/hr-associated-type-bound-param-5.rs:33:14
@@ -60,7 +57,6 @@ LL | type U = str;
6057
|
6158
= help: the following implementations were found:
6259
<&T as Clone>
63-
<&mut T as Clone>
6460

6561
error: aborting due to 4 previous errors
6662

Diff for: src/test/ui/associated-types/hr-associated-type-bound-param-6.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | type U = str;
1212
|
1313
= help: the following implementations were found:
1414
<&T as Clone>
15-
<&mut T as Clone>
1615

1716
error[E0277]: the trait bound `for<'b> T: X<'b, T>` is not satisfied
1817
--> $DIR/hr-associated-type-bound-param-6.rs:12:12

Diff for: src/test/ui/async-await/issue-64130-3-other.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ LL | async fn bar() {
1010
LL | is_qux(bar());
1111
| ^^^^^^ within `impl Future`, the trait `Qux` is not implemented for `Foo`
1212
|
13-
= help: the following implementations were found:
14-
<Foo as Qux>
1513
note: future does not implement `Qux` as this value is used across an await
1614
--> $DIR/issue-64130-3-other.rs:18:5
1715
|

Diff for: src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ LL | fn is_mytrait<T: MyTrait>() {}
77
LL | is_mytrait::<(MyS2, MyS)>();
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^ within `(MyS2, MyS)`, the trait `MyTrait` is not implemented for `MyS2`
99
|
10-
= help: the following implementations were found:
11-
<MyS2 as MyTrait>
1210
= note: required because it appears within the type `(MyS2, MyS)`
1311

1412
error: aborting due to previous error

Diff for: src/test/ui/auto-traits/typeck-default-trait-impl-constituent-types.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ LL | fn is_mytrait<T: MyTrait>() {}
66
...
77
LL | is_mytrait::<MyS2>();
88
| ^^^^ the trait `MyTrait` is not implemented for `MyS2`
9-
|
10-
= help: the following implementations were found:
11-
<MyS2 as MyTrait>
129

1310
error: aborting due to previous error
1411

Diff for: src/test/ui/auto-traits/typeck-default-trait-impl-negation.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ LL | fn is_my_trait<T: MyTrait>() {}
66
...
77
LL | is_my_trait::<ThisImplsUnsafeTrait>();
88
| ^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `ThisImplsUnsafeTrait`
9-
|
10-
= help: the following implementations were found:
11-
<ThisImplsUnsafeTrait as MyTrait>
129

1310
error[E0277]: the trait bound `ThisImplsTrait: MyUnsafeTrait` is not satisfied
1411
--> $DIR/typeck-default-trait-impl-negation.rs:25:26
@@ -18,9 +15,6 @@ LL | fn is_my_unsafe_trait<T: MyUnsafeTrait>() {}
1815
...
1916
LL | is_my_unsafe_trait::<ThisImplsTrait>();
2017
| ^^^^^^^^^^^^^^ the trait `MyUnsafeTrait` is not implemented for `ThisImplsTrait`
21-
|
22-
= help: the following implementations were found:
23-
<ThisImplsTrait as MyUnsafeTrait>
2418

2519
error: aborting due to 2 previous errors
2620

Diff for: src/test/ui/traits/issue-79458.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Negative implementations should not be shown in trait suggestions.
2+
// This is a regression test of #79458.
3+
4+
#[derive(Clone)]
5+
struct Foo<'a, T> {
6+
bar: &'a mut T
7+
//~^ ERROR the trait bound `&mut T: Clone` is not satisfied
8+
}
9+
10+
fn main() {}

Diff for: src/test/ui/traits/issue-79458.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `&mut T: Clone` is not satisfied
2+
--> $DIR/issue-79458.rs:6:5
3+
|
4+
LL | bar: &'a mut T
5+
| ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `&mut T`
6+
|
7+
= help: the following implementations were found:
8+
<&T as Clone>
9+
= note: `Clone` is implemented for `&T`, but not for `&mut T`
10+
= note: required by `clone`
11+
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)