Skip to content

Commit d13c348

Browse files
committed
reword trait bound suggestion message to include the bounds
1 parent 68253e1 commit d13c348

File tree

155 files changed

+331
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+331
-314
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ enum SuggestChangingConstraintsMessage<'a> {
171171

172172
fn suggest_changing_unsized_bound(
173173
generics: &hir::Generics<'_>,
174-
suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>,
174+
suggestions: &mut Vec<(Span, String, String, SuggestChangingConstraintsMessage<'_>)>,
175175
param: &hir::GenericParam<'_>,
176176
def_id: Option<DefId>,
177177
) {
@@ -206,7 +206,8 @@ fn suggest_changing_unsized_bound(
206206
continue;
207207
}
208208

209-
let mut push_suggestion = |sp, msg| suggestions.push((sp, String::new(), msg));
209+
let mut push_suggestion =
210+
|sp, msg| suggestions.push((sp, "Sized".to_string(), String::new(), msg));
210211

211212
if predicate.bounds.len() == unsized_bounds.len() {
212213
// All the bounds are unsized bounds, e.g.
@@ -348,10 +349,20 @@ pub fn suggest_constraining_type_params<'a>(
348349
use SuggestChangingConstraintsMessage::RestrictBoundFurther;
349350

350351
if let Some(open_paren_sp) = open_paren_sp {
351-
suggestions.push((open_paren_sp, "(".to_string(), RestrictBoundFurther));
352-
suggestions.push((span, format!("){suggestion}"), RestrictBoundFurther));
352+
suggestions.push((
353+
open_paren_sp,
354+
constraint.clone(),
355+
"(".to_string(),
356+
RestrictBoundFurther,
357+
));
358+
suggestions.push((
359+
span,
360+
constraint.clone(),
361+
format!("){suggestion}"),
362+
RestrictBoundFurther,
363+
));
353364
} else {
354-
suggestions.push((span, suggestion, RestrictBoundFurther));
365+
suggestions.push((span, constraint.clone(), suggestion, RestrictBoundFurther));
355366
}
356367
};
357368

@@ -409,6 +420,7 @@ pub fn suggest_constraining_type_params<'a>(
409420
// - insert: `, X: Bar`
410421
suggestions.push((
411422
generics.tail_span_for_predicate_suggestion(),
423+
constraint.clone(),
412424
constraints.iter().fold(String::new(), |mut string, &(constraint, _)| {
413425
write!(string, ", {param_name}: {constraint}").unwrap();
414426
string
@@ -438,6 +450,7 @@ pub fn suggest_constraining_type_params<'a>(
438450
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
439451
suggestions.push((
440452
generics.tail_span_for_predicate_suggestion(),
453+
constraint.clone(),
441454
format!("{where_prefix} {param_name}: {constraint}"),
442455
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
443456
));
@@ -451,6 +464,7 @@ pub fn suggest_constraining_type_params<'a>(
451464
if let Some(colon_span) = param.colon_span {
452465
suggestions.push((
453466
colon_span.shrink_to_hi(),
467+
constraint.clone(),
454468
format!(" {constraint}"),
455469
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
456470
));
@@ -463,6 +477,7 @@ pub fn suggest_constraining_type_params<'a>(
463477
// - help: consider restricting this type parameter with `T: Foo`
464478
suggestions.push((
465479
param.span.shrink_to_hi(),
480+
constraint.clone(),
466481
format!(": {constraint}"),
467482
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
468483
));
@@ -471,12 +486,16 @@ pub fn suggest_constraining_type_params<'a>(
471486
// FIXME: remove the suggestions that are from derive, as the span is not correct
472487
suggestions = suggestions
473488
.into_iter()
474-
.filter(|(span, _, _)| !span.in_derive_expansion())
489+
.filter(|(span, _, _, _)| !span.in_derive_expansion())
475490
.collect::<Vec<_>>();
476491

477492
if suggestions.len() == 1 {
478-
let (span, suggestion, msg) = suggestions.pop().unwrap();
479-
let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" };
493+
let (span, constraint, suggestion, msg) = suggestions.pop().unwrap();
494+
let post = format!(
495+
" with {}trait{} `{constraint}`",
496+
if unstable_suggestion { "unstable " } else { "" },
497+
if constraint.contains('+') { "s" } else { "" },
498+
);
480499
let msg = match msg {
481500
SuggestChangingConstraintsMessage::RestrictBoundFurther => {
482501
format!("consider further restricting this bound{post}")
@@ -488,21 +507,19 @@ pub fn suggest_constraining_type_params<'a>(
488507
format!("consider further restricting type parameter `{ty}`{post}")
489508
}
490509
SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
491-
format!(
492-
"consider removing the `?Sized` bound to make the type parameter `Sized`{post}"
493-
)
510+
format!("consider removing the `?Sized` bound to make the type parameter `Sized`")
494511
}
495512
SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
496-
format!("consider replacing `?Sized` with `Sized`{post}")
513+
format!("consider replacing `?Sized` with `Sized`")
497514
}
498515
};
499516

500517
err.span_suggestion_verbose(span, msg, suggestion, applicability);
501518
} else if suggestions.len() > 1 {
502-
let post = if unstable_suggestion { " but some of them are `unstable` traits" } else { "" };
519+
let post = if unstable_suggestion { " (some of them are unstable traits)" } else { "" };
503520
err.multipart_suggestion_verbose(
504521
format!("consider restricting type parameters{post}"),
505-
suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
522+
suggestions.into_iter().map(|(span, _, suggestion, _)| (span, suggestion)).collect(),
506523
applicability,
507524
);
508525
}

tests/rustdoc-ui/issues/issue-96287.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0220]: associated type `Assoc` not found for `V`
44
LL | pub type Foo<V> = impl Trait<V::Assoc>;
55
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
66
|
7-
help: consider restricting type parameter `V`
7+
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
88
|
99
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
1010
| ++++++++++++++++
@@ -16,7 +16,7 @@ LL | pub type Foo<V> = impl Trait<V::Assoc>;
1616
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
1717
|
1818
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
19-
help: consider restricting type parameter `V`
19+
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
2020
|
2121
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
2222
| ++++++++++++++++

tests/rustdoc-ui/synthetic-auto-trait-impls/projections-in-super-trait-bound-unsatisfied.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
44
LL | pub struct Structure<C: Tec> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
66
|
7-
help: consider further restricting this bound
7+
help: consider further restricting this bound with trait `Bar<5>`
88
|
99
LL | pub struct Structure<C: Tec + Bar<5>> {
1010
| ++++++++
@@ -15,7 +15,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
1515
LL | _field: C::BarType,
1616
| ^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
1717
|
18-
help: consider further restricting this bound
18+
help: consider further restricting this bound with trait `Bar<5>`
1919
|
2020
LL | pub struct Structure<C: Tec + Bar<5>> {
2121
| ++++++++

tests/ui/associated-types/associated-types-no-suitable-bound.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
44
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
55
| ^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `T`
66
|
7-
help: consider restricting type parameter `T`
7+
help: consider restricting type parameter `T` with trait `Get`
88
|
99
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
1010
| +++++
@@ -15,7 +15,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
1515
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
1616
| ^^ the trait `Get` is not implemented for `T`
1717
|
18-
help: consider restricting type parameter `T`
18+
help: consider restricting type parameter `T` with trait `Get`
1919
|
2020
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
2121
| +++++

tests/ui/associated-types/defaults-suitability.current.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
4747
|
4848
LL | type Bar: Clone = Vec<T>;
4949
| ^^^^^ required by this bound in `Foo::Bar`
50-
help: consider restricting type parameter `T`
50+
help: consider restricting type parameter `T` with trait `std::clone::Clone`
5151
|
5252
LL | trait Foo<T: std::clone::Clone> {
5353
| +++++++++++++++++++
@@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
132132
...
133133
LL | type Baz = T;
134134
| --- required by a bound in this associated type
135-
help: consider further restricting type parameter `T`
135+
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
136136
|
137137
LL | Self::Baz: Clone, T: std::clone::Clone
138138
| ~~~~~~~~~~~~~~~~~~~~~~

tests/ui/associated-types/defaults-suitability.next.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
4747
|
4848
LL | type Bar: Clone = Vec<T>;
4949
| ^^^^^ required by this bound in `Foo::Bar`
50-
help: consider restricting type parameter `T`
50+
help: consider restricting type parameter `T` with trait `std::clone::Clone`
5151
|
5252
LL | trait Foo<T: std::clone::Clone> {
5353
| +++++++++++++++++++
@@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
132132
...
133133
LL | type Baz = T;
134134
| --- required by a bound in this associated type
135-
help: consider further restricting type parameter `T`
135+
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
136136
|
137137
LL | Self::Baz: Clone, T: std::clone::Clone
138138
| ~~~~~~~~~~~~~~~~~~~~~~

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `for<'b> T: X<'b, T>` is not satisfied
44
LL | impl<S, T> X<'_, T> for (S,) {
55
| ^^^^^^^^ the trait `for<'b> X<'b, T>` is not implemented for `T`
66
|
7-
help: consider restricting type parameter `T`
7+
help: consider restricting type parameter `T` with trait `for<'b> X<'b, T>`
88
|
99
LL | impl<S, T: for<'b> X<'b, T>> X<'_, T> for (S,) {
1010
| ++++++++++++++++++

tests/ui/associated-types/hr-associated-type-projection-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | trait UnsafeCopy<'a, T: Copy>
1616
LL | where
1717
LL | for<'b> <Self as UnsafeCopy<'b, T>>::Item: std::ops::Deref<Target = T>,
1818
| ^^^^^^^^^^ required by this bound in `UnsafeCopy`
19-
help: consider further restricting this bound
19+
help: consider further restricting this bound with trait `<Target = T>`
2020
|
2121
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<'_, T> for T {
2222
| ++++++++++++

tests/ui/associated-types/issue-27675-unchecked-bounds.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: required by a bound in `copy`
99
|
1010
LL | fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
1111
| ^^^^^ required by this bound in `copy`
12-
help: consider restricting type parameter `T`
12+
help: consider restricting type parameter `T` with trait `std::marker::Copy`
1313
|
1414
LL | pub fn copy_any<T: std::marker::Copy>(t: &T) -> T {
1515
| +++++++++++++++++++

tests/ui/associated-types/issue-43784-associated-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: required by a bound in `Complete::Assoc`
1414
|
1515
LL | type Assoc: Partial<Self>;
1616
| ^^^^^^^^^^^^^ required by this bound in `Complete::Assoc`
17-
help: consider restricting type parameter `T`
17+
help: consider restricting type parameter `T` with trait `std::marker::Copy`
1818
|
1919
LL | impl<T: std::marker::Copy> Complete for T {
2020
| +++++++++++++++++++

tests/ui/associated-types/issue-59324.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | |
77
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
88
| |______________________________________________^ the trait `Foo` is not implemented for `Bug`
99
|
10-
help: consider further restricting this bound
10+
help: consider further restricting this bound with trait `Foo`
1111
|
1212
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
1313
| +++++
@@ -24,7 +24,7 @@ LL | |
2424
LL | | }
2525
| |_^ the trait `Foo` is not implemented for `Bug`
2626
|
27-
help: consider further restricting this bound
27+
help: consider further restricting this bound with trait `Foo`
2828
|
2929
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
3030
| +++++
@@ -38,7 +38,7 @@ LL | | &self,
3838
LL | | ) -> Self::AssocType;
3939
| |_________________________^ the trait `Foo` is not implemented for `Bug`
4040
|
41-
help: consider further restricting this bound
41+
help: consider further restricting this bound with trait `Foo`
4242
|
4343
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
4444
| +++++
@@ -61,7 +61,7 @@ error[E0277]: the trait bound `Bug: Foo` is not satisfied
6161
LL | ) -> Self::AssocType;
6262
| ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
6363
|
64-
help: consider further restricting this bound
64+
help: consider further restricting this bound with trait `Foo`
6565
|
6666
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
6767
| +++++

tests/ui/async-await/issue-70818.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: captured value is not `Send`
99
|
1010
LL | async { (ty, ty1) }
1111
| ^^^ has type `U` which is not `Send`
12-
help: consider restricting type parameter `U`
12+
help: consider restricting type parameter `U` with trait `std::marker::Send`
1313
|
1414
LL | fn foo<T: Send, U: std::marker::Send>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + Send {
1515
| +++++++++++++++++++

tests/ui/async-await/issue-86507.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
1414
LL | let x = x;
1515
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
1616
= note: required for the cast from `Pin<Box<{async block@$DIR/issue-86507.rs:18:17: 18:27}>>` to `Pin<Box<(dyn Future<Output = ()> + Send + 'async_trait)>>`
17-
help: consider further restricting this bound
17+
help: consider further restricting this bound with trait `std::marker::Sync`
1818
|
1919
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)
2020
| +++++++++++++++++++

tests/ui/auto-traits/typeck-auto-trait-no-supertraits-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
3030
| ^ - you could clone this value
3131
| |
3232
| consider constraining this type parameter with `Clone`
33-
help: consider further restricting this bound
33+
help: consider further restricting this bound with trait `Copy`
3434
|
3535
LL | fn copy<T: Magic + Copy>(x: T) -> (T, T) { (x, x) }
3636
| ++++++

0 commit comments

Comments
 (0)