Remove invalid further restricting for type bound
diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs
index 4bf2233..f479b18 100644
--- a/compiler/rustc_middle/src/ty/diagnostics.rs
+++ b/compiler/rustc_middle/src/ty/diagnostics.rs
@@ -271,6 +271,19 @@ pub fn suggest_constraining_type_params<'a>(
}
}
+ // in the scenario like impl has stricter requirements than trait,
+ // we should not suggest restrict bound on the impl, here we double check
+ // the whether the param already has the constraint by checking `def_id`
+ let bound_trait_defs: Vec<DefId> = generics
+ .bounds_for_param(param.def_id)
+ .flat_map(|bound| {
+ bound.bounds.iter().flat_map(|b| b.trait_ref().and_then(|t| t.trait_def_id()))
+ })
+ .collect();
+
+ constraints
+ .retain(|(_, def_id)| def_id.map_or(true, |def| !bound_trait_defs.contains(&def)));
+
if constraints.is_empty() {
continue;
}
@@ -332,6 +345,7 @@ pub fn suggest_constraining_type_params<'a>(
// --
// |
// replace with: `T: Bar +`
+
if let Some((span, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
suggest_restrict(span, true, open_paren_sp);
continue;
diff --git a/tests/ui/associated-types/associated-types-invalid-trait-ref-issue-18865.stderr b/tests/ui/associated-types/associated-types-invalid-trait-ref-issue-18865.stderr
index adde31b..b4012d2 100644
--- a/tests/ui/associated-types/associated-types-invalid-trait-ref-issue-18865.stderr
+++ b/tests/ui/associated-types/associated-types-invalid-trait-ref-issue-18865.stderr
@@ -3,11 +3,6 @@
|
LL | let u: <T as Foo<usize>>::Bar = t.get_bar();
| ^ the trait `Foo<usize>` is not implemented for `T`
- |
-help: consider further restricting this bound
- |
-LL | fn f<T:Foo<isize> + Foo<usize>>(t: &T) {
- | ++++++++++++
error: aborting due to 1 previous error
diff --git a/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.rs b/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.rs
new file mode 100644
index 0000000..6083cc7
--- /dev/null
+++ b/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.rs
@@ -0,0 +1,23 @@
+//@ edition:2021
+// issue: rust-lang/rust#127555
+
+pub trait Foo {
+ fn bar<F>(&mut self, func: F) -> impl std::future::Future<Output = ()> + Send
+ where
+ F: FnMut();
+}
+
+struct Baz {}
+
+impl Foo for Baz {
+ async fn bar<F>(&mut self, _func: F) -> ()
+ //~^ ERROR `F` cannot be sent between threads safely
+ where
+ F: FnMut() + Send,
+ //~^ ERROR impl has stricter requirements than trait
+ {
+ ()
+ }
+}
+
+fn main() {}
diff --git a/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.stderr b/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.stderr
new file mode 100644
index 0000000..7f3cd2a
--- /dev/null
+++ b/tests/ui/associated-types/remove-invalid-type-bound-suggest-issue-127555.stderr
@@ -0,0 +1,33 @@
+error[E0277]: `F` cannot be sent between threads safely
+ --> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:13:5
+ |
+LL | / async fn bar<F>(&mut self, _func: F) -> ()
+LL | |
+LL | | where
+LL | | F: FnMut() + Send,
+ | |__________________________^ `F` cannot be sent between threads safely
+ |
+note: required by a bound in `<Baz as Foo>::bar`
+ --> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:16:22
+ |
+LL | async fn bar<F>(&mut self, _func: F) -> ()
+ | --- required by a bound in this associated function
+...
+LL | F: FnMut() + Send,
+ | ^^^^ required by this bound in `<Baz as Foo>::bar`
+
+error[E0276]: impl has stricter requirements than trait
+ --> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:16:22
+ |
+LL | / fn bar<F>(&mut self, func: F) -> impl std::future::Future<Output = ()> + Send
+LL | | where
+LL | | F: FnMut();
+ | |___________________- definition of `bar` from trait
+...
+LL | F: FnMut() + Send,
+ | ^^^^ impl has extra requirement `F: Send`
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0276, E0277.
+For more information about an error, try `rustc --explain E0276`.
diff --git a/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr b/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr
index 66819d1..80dc5fd 100644
--- a/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr
+++ b/tests/ui/async-await/in-trait/unconstrained-impl-region.stderr
@@ -9,10 +9,6 @@
|
LL | async fn on_mount(self, _: impl Inbox<&'a ()>) {}
| ^^^^^^^^^^^^^ required by this bound in `<() as Actor>::on_mount`
-help: consider further restricting this bound
- |
-LL | async fn on_mount(self, _: impl Inbox<&'a ()> + Inbox<&'a ()>) {}
- | +++++++++++++++
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/unconstrained-impl-region.rs:13:6
diff --git a/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr b/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr
index 8771de8..3697bd9 100644
--- a/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr
+++ b/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr
@@ -9,10 +9,6 @@
|
LL | fn impl_hr<'b, T: for<'a> Trait<'a, 'b>>() {}
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `impl_hr`
-help: consider further restricting this bound
- |
-LL | fn not_hr<'a, T: for<'b> Trait<'a, 'b> + OtherTrait<'static> + for<'a> Trait<'a, '_>>() {
- | +++++++++++++++++++++++
error: aborting due to 1 previous error
diff --git a/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.next.stderr b/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.next.stderr
index 90df487..6e0ec56 100644
--- a/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.next.stderr
+++ b/tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.next.stderr
@@ -9,10 +9,6 @@
|
LL | fn trait_bound<T: for<'a> Trait<'a>>() {}
| ^^^^^^^^^^^^^^^^^ required by this bound in `trait_bound`
-help: consider further restricting this bound
- |
-LL | fn function1<T: Trait<'static> + for<'a> Trait<'a>>() {
- | +++++++++++++++++++
error[E0277]: the trait bound `for<'a> T: Trait<'a>` is not satisfied
--> $DIR/candidate-from-env-universe-err-project.rs:38:24
@@ -25,10 +21,6 @@
|
LL | fn projection_bound<T: for<'a> Trait<'a, Assoc = usize>>() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `projection_bound`
-help: consider further restricting this bound
- |
-LL | fn function2<T: Trait<'static, Assoc = usize> + for<'a> Trait<'a>>() {
- | +++++++++++++++++++
error[E0271]: type mismatch resolving `<T as Trait<'a>>::Assoc == usize`
--> $DIR/candidate-from-env-universe-err-project.rs:38:24
diff --git a/tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits.stderr b/tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits.stderr
index af76377..dc1a4c9 100644
--- a/tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits.stderr
+++ b/tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits.stderr
@@ -11,10 +11,6 @@
|
LL | fn want_foo_for_any_tcx<F: for<'tcx> Foo<'tcx>>(f: &F) {
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `want_foo_for_any_tcx`
-help: consider further restricting this bound
- |
-LL | fn want_foo_for_some_tcx<'x, F: Foo<'x> + for<'tcx> Foo<'tcx>>(f: &'x F) {
- | +++++++++++++++++++++
error[E0277]: the trait bound `for<'ccx> B: Bar<'ccx>` is not satisfied
--> $DIR/hrtb-higher-ranker-supertraits.rs:28:26
@@ -29,10 +25,6 @@
|
LL | fn want_bar_for_any_ccx<B: for<'ccx> Bar<'ccx>>(b: &B) {
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `want_bar_for_any_ccx`
-help: consider further restricting this bound
- |
-LL | fn want_bar_for_some_ccx<'x, B: Bar<'x> + for<'ccx> Bar<'ccx>>(b: &B) {
- | +++++++++++++++++++++
error: aborting due to 2 previous errors
diff --git a/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr b/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr
index fbf82a2..ae44909 100644
--- a/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr
+++ b/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr
@@ -22,10 +22,6 @@
|
LL | fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
| ^^^^^^^ required by this bound in `<Bar as Foo<char>>::foo`
-help: consider further restricting this bound
- |
-LL | fn foo<F2: Foo<u8> + Foo<u8>>(self) -> impl Foo<u8> {
- | +++++++++
error[E0276]: impl has stricter requirements than trait
--> $DIR/return-dont-satisfy-bounds.rs:8:16