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

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 11 additions & 1 deletion
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 3 deletions
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 4 deletions
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

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

Lines changed: 0 additions & 1 deletion
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

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

Lines changed: 0 additions & 2 deletions
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
|

0 commit comments

Comments
 (0)