Skip to content

Commit 0f2251c

Browse files
authored
Unrolled build for rust-lang#127844
Rollup merge of rust-lang#127844 - chenyukang:yukang-fix-type-bound-127555, r=jieyouxu Remove invalid further restricting suggestion for type bound This PR partially addresses rust-lang#127555, it will remove the obvious error suggestion: ```console | ^^^^ required by this bound in `<Baz as Foo>::bar` help: consider further restricting this bound | 12 | F: FnMut() + Send + std::marker::Send, | +++++++++++++++++++ ``` I may create another PR to get a better diagnostic for `impl has stricter requirements than trait` scenario.
2 parents fcc325f + 40e07a3 commit 0f2251c

9 files changed

+70
-33
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+14
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ pub fn suggest_constraining_type_params<'a>(
271271
}
272272
}
273273

274+
// in the scenario like impl has stricter requirements than trait,
275+
// we should not suggest restrict bound on the impl, here we double check
276+
// the whether the param already has the constraint by checking `def_id`
277+
let bound_trait_defs: Vec<DefId> = generics
278+
.bounds_for_param(param.def_id)
279+
.flat_map(|bound| {
280+
bound.bounds.iter().flat_map(|b| b.trait_ref().and_then(|t| t.trait_def_id()))
281+
})
282+
.collect();
283+
284+
constraints
285+
.retain(|(_, def_id)| def_id.map_or(true, |def| !bound_trait_defs.contains(&def)));
286+
274287
if constraints.is_empty() {
275288
continue;
276289
}
@@ -332,6 +345,7 @@ pub fn suggest_constraining_type_params<'a>(
332345
// --
333346
// |
334347
// replace with: `T: Bar +`
348+
335349
if let Some((span, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
336350
suggest_restrict(span, true, open_paren_sp);
337351
continue;

tests/ui/associated-types/associated-types-invalid-trait-ref-issue-18865.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0277]: the trait bound `T: Foo<usize>` is not satisfied
33
|
44
LL | let u: <T as Foo<usize>>::Bar = t.get_bar();
55
| ^ the trait `Foo<usize>` is not implemented for `T`
6-
|
7-
help: consider further restricting this bound
8-
|
9-
LL | fn f<T:Foo<isize> + Foo<usize>>(t: &T) {
10-
| ++++++++++++
116

127
error: aborting due to 1 previous error
138

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ edition:2021
2+
// issue: rust-lang/rust#127555
3+
4+
pub trait Foo {
5+
fn bar<F>(&mut self, func: F) -> impl std::future::Future<Output = ()> + Send
6+
where
7+
F: FnMut();
8+
}
9+
10+
struct Baz {}
11+
12+
impl Foo for Baz {
13+
async fn bar<F>(&mut self, _func: F) -> ()
14+
//~^ ERROR `F` cannot be sent between threads safely
15+
where
16+
F: FnMut() + Send,
17+
//~^ ERROR impl has stricter requirements than trait
18+
{
19+
()
20+
}
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: `F` cannot be sent between threads safely
2+
--> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:13:5
3+
|
4+
LL | / async fn bar<F>(&mut self, _func: F) -> ()
5+
LL | |
6+
LL | | where
7+
LL | | F: FnMut() + Send,
8+
| |__________________________^ `F` cannot be sent between threads safely
9+
|
10+
note: required by a bound in `<Baz as Foo>::bar`
11+
--> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:16:22
12+
|
13+
LL | async fn bar<F>(&mut self, _func: F) -> ()
14+
| --- required by a bound in this associated function
15+
...
16+
LL | F: FnMut() + Send,
17+
| ^^^^ required by this bound in `<Baz as Foo>::bar`
18+
19+
error[E0276]: impl has stricter requirements than trait
20+
--> $DIR/remove-invalid-type-bound-suggest-issue-127555.rs:16:22
21+
|
22+
LL | / fn bar<F>(&mut self, func: F) -> impl std::future::Future<Output = ()> + Send
23+
LL | | where
24+
LL | | F: FnMut();
25+
| |___________________- definition of `bar` from trait
26+
...
27+
LL | F: FnMut() + Send,
28+
| ^^^^ impl has extra requirement `F: Send`
29+
30+
error: aborting due to 2 previous errors
31+
32+
Some errors have detailed explanations: E0276, E0277.
33+
For more information about an error, try `rustc --explain E0276`.

tests/ui/async-await/in-trait/unconstrained-impl-region.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ note: required by a bound in `<() as Actor>::on_mount`
99
|
1010
LL | async fn on_mount(self, _: impl Inbox<&'a ()>) {}
1111
| ^^^^^^^^^^^^^ required by this bound in `<() as Actor>::on_mount`
12-
help: consider further restricting this bound
13-
|
14-
LL | async fn on_mount(self, _: impl Inbox<&'a ()> + Inbox<&'a ()>) {}
15-
| +++++++++++++++
1612

1713
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
1814
--> $DIR/unconstrained-impl-region.rs:13:6

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ note: required by a bound in `impl_hr`
99
|
1010
LL | fn impl_hr<'b, T: for<'a> Trait<'a, 'b>>() {}
1111
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `impl_hr`
12-
help: consider further restricting this bound
13-
|
14-
LL | fn not_hr<'a, T: for<'b> Trait<'a, 'b> + OtherTrait<'static> + for<'a> Trait<'a, '_>>() {
15-
| +++++++++++++++++++++++
1612

1713
error: aborting due to 1 previous error
1814

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.next.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ note: required by a bound in `trait_bound`
99
|
1010
LL | fn trait_bound<T: for<'a> Trait<'a>>() {}
1111
| ^^^^^^^^^^^^^^^^^ required by this bound in `trait_bound`
12-
help: consider further restricting this bound
13-
|
14-
LL | fn function1<T: Trait<'static> + for<'a> Trait<'a>>() {
15-
| +++++++++++++++++++
1612

1713
error[E0277]: the trait bound `for<'a> T: Trait<'a>` is not satisfied
1814
--> $DIR/candidate-from-env-universe-err-project.rs:38:24
@@ -25,10 +21,6 @@ note: required by a bound in `projection_bound`
2521
|
2622
LL | fn projection_bound<T: for<'a> Trait<'a, Assoc = usize>>() {}
2723
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `projection_bound`
28-
help: consider further restricting this bound
29-
|
30-
LL | fn function2<T: Trait<'static, Assoc = usize> + for<'a> Trait<'a>>() {
31-
| +++++++++++++++++++
3224

3325
error[E0271]: type mismatch resolving `<T as Trait<'a>>::Assoc == usize`
3426
--> $DIR/candidate-from-env-universe-err-project.rs:38:24

tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ note: required by a bound in `want_foo_for_any_tcx`
1111
|
1212
LL | fn want_foo_for_any_tcx<F: for<'tcx> Foo<'tcx>>(f: &F) {
1313
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `want_foo_for_any_tcx`
14-
help: consider further restricting this bound
15-
|
16-
LL | fn want_foo_for_some_tcx<'x, F: Foo<'x> + for<'tcx> Foo<'tcx>>(f: &'x F) {
17-
| +++++++++++++++++++++
1814

1915
error[E0277]: the trait bound `for<'ccx> B: Bar<'ccx>` is not satisfied
2016
--> $DIR/hrtb-higher-ranker-supertraits.rs:28:26
@@ -29,10 +25,6 @@ note: required by a bound in `want_bar_for_any_ccx`
2925
|
3026
LL | fn want_bar_for_any_ccx<B: for<'ccx> Bar<'ccx>>(b: &B) {
3127
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `want_bar_for_any_ccx`
32-
help: consider further restricting this bound
33-
|
34-
LL | fn want_bar_for_some_ccx<'x, B: Bar<'x> + for<'ccx> Bar<'ccx>>(b: &B) {
35-
| +++++++++++++++++++++
3628

3729
error: aborting due to 2 previous errors
3830

tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ note: required by a bound in `<Bar as Foo<char>>::foo`
2222
|
2323
LL | fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
2424
| ^^^^^^^ required by this bound in `<Bar as Foo<char>>::foo`
25-
help: consider further restricting this bound
26-
|
27-
LL | fn foo<F2: Foo<u8> + Foo<u8>>(self) -> impl Foo<u8> {
28-
| +++++++++
2925

3026
error[E0276]: impl has stricter requirements than trait
3127
--> $DIR/return-dont-satisfy-bounds.rs:8:16

0 commit comments

Comments
 (0)