Skip to content

Commit

Permalink
reword trait bound suggestion message to include the bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 28, 2024
1 parent 0554993 commit e3dfae8
Show file tree
Hide file tree
Showing 154 changed files with 331 additions and 314 deletions.
45 changes: 31 additions & 14 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ enum SuggestChangingConstraintsMessage<'a> {

fn suggest_changing_unsized_bound(
generics: &hir::Generics<'_>,
suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>,
suggestions: &mut Vec<(Span, String, String, SuggestChangingConstraintsMessage<'_>)>,
param: &hir::GenericParam<'_>,
def_id: Option<DefId>,
) {
Expand Down Expand Up @@ -206,7 +206,8 @@ fn suggest_changing_unsized_bound(
continue;
}

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

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

if let Some(open_paren_sp) = open_paren_sp {
suggestions.push((open_paren_sp, "(".to_string(), RestrictBoundFurther));
suggestions.push((span, format!("){suggestion}"), RestrictBoundFurther));
suggestions.push((
open_paren_sp,
constraint.clone(),
"(".to_string(),
RestrictBoundFurther,
));
suggestions.push((
span,
constraint.clone(),
format!("){suggestion}"),
RestrictBoundFurther,
));
} else {
suggestions.push((span, suggestion, RestrictBoundFurther));
suggestions.push((span, constraint.clone(), suggestion, RestrictBoundFurther));
}
};

Expand Down Expand Up @@ -409,6 +420,7 @@ pub fn suggest_constraining_type_params<'a>(
// - insert: `, X: Bar`
suggestions.push((
generics.tail_span_for_predicate_suggestion(),
constraint.clone(),
constraints.iter().fold(String::new(), |mut string, &(constraint, _)| {
write!(string, ", {param_name}: {constraint}").unwrap();
string
Expand Down Expand Up @@ -438,6 +450,7 @@ pub fn suggest_constraining_type_params<'a>(
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
suggestions.push((
generics.tail_span_for_predicate_suggestion(),
constraint.clone(),
format!("{where_prefix} {param_name}: {constraint}"),
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
));
Expand All @@ -451,6 +464,7 @@ pub fn suggest_constraining_type_params<'a>(
if let Some(colon_span) = param.colon_span {
suggestions.push((
colon_span.shrink_to_hi(),
constraint.clone(),
format!(" {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -463,6 +477,7 @@ pub fn suggest_constraining_type_params<'a>(
// - help: consider restricting this type parameter with `T: Foo`
suggestions.push((
param.span.shrink_to_hi(),
constraint.clone(),
format!(": {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -471,12 +486,16 @@ pub fn suggest_constraining_type_params<'a>(
// FIXME: remove the suggestions that are from derive, as the span is not correct
suggestions = suggestions
.into_iter()
.filter(|(span, _, _)| !span.in_derive_expansion())
.filter(|(span, _, _, _)| !span.in_derive_expansion())
.collect::<Vec<_>>();

if suggestions.len() == 1 {
let (span, suggestion, msg) = suggestions.pop().unwrap();
let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" };
let (span, constraint, suggestion, msg) = suggestions.pop().unwrap();
let post = format!(
" with {}trait{} `{constraint}`",
if unstable_suggestion { "unstable " } else { "" },
if constraint.contains('+') { "s" } else { "" },
);
let msg = match msg {
SuggestChangingConstraintsMessage::RestrictBoundFurther => {
format!("consider further restricting this bound{post}")
Expand All @@ -488,21 +507,19 @@ pub fn suggest_constraining_type_params<'a>(
format!("consider further restricting type parameter `{ty}`{post}")
}
SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
format!(
"consider removing the `?Sized` bound to make the type parameter `Sized`{post}"
)
format!("consider removing the `?Sized` bound to make the type parameter `Sized`")
}
SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
format!("consider replacing `?Sized` with `Sized`{post}")
format!("consider replacing `?Sized` with `Sized`")
}
};

err.span_suggestion_verbose(span, msg, suggestion, applicability);
} else if suggestions.len() > 1 {
let post = if unstable_suggestion { " but some of them are `unstable` traits" } else { "" };
let post = if unstable_suggestion { " (some of them are unstable traits)" } else { "" };
err.multipart_suggestion_verbose(
format!("consider restricting type parameters{post}"),
suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
suggestions.into_iter().map(|(span, _, suggestion, _)| (span, suggestion)).collect(),
applicability,
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/issues/issue-96287.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0220]: associated type `Assoc` not found for `V`
LL | pub type Foo<V> = impl Trait<V::Assoc>;
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
|
help: consider restricting type parameter `V`
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
|
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
| ++++++++++++++++
Expand All @@ -16,7 +16,7 @@ LL | pub type Foo<V> = impl Trait<V::Assoc>;
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `V`
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
|
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
| ++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
LL | pub struct Structure<C: Tec> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Bar<5>`
|
LL | pub struct Structure<C: Tec + Bar<5>> {
| ++++++++
Expand All @@ -15,7 +15,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
LL | _field: C::BarType,
| ^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Bar<5>`
|
LL | pub struct Structure<C: Tec + Bar<5>> {
| ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
| ^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `Get`
|
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
| +++++
Expand All @@ -15,7 +15,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
| ^^ the trait `Get` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `Get`
|
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
| +++++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-suitability.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
|
LL | type Bar: Clone = Vec<T>;
| ^^^^^ required by this bound in `Foo::Bar`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::clone::Clone`
|
LL | trait Foo<T: std::clone::Clone> {
| +++++++++++++++++++
Expand Down Expand Up @@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
...
LL | type Baz = T;
| --- required by a bound in this associated type
help: consider further restricting type parameter `T`
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
|
LL | Self::Baz: Clone, T: std::clone::Clone
| ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-suitability.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
|
LL | type Bar: Clone = Vec<T>;
| ^^^^^ required by this bound in `Foo::Bar`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::clone::Clone`
|
LL | trait Foo<T: std::clone::Clone> {
| +++++++++++++++++++
Expand Down Expand Up @@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
...
LL | type Baz = T;
| --- required by a bound in this associated type
help: consider further restricting type parameter `T`
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
|
LL | Self::Baz: Clone, T: std::clone::Clone
| ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `for<'b> T: X<'b, T>` is not satisfied
LL | impl<S, T> X<'_, T> for (S,) {
| ^^^^^^^^ the trait `for<'b> X<'b, T>` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `for<'b> X<'b, T>`
|
LL | impl<S, T: for<'b> X<'b, T>> X<'_, T> for (S,) {
| ++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | trait UnsafeCopy<'a, T: Copy>
LL | where
LL | for<'b> <Self as UnsafeCopy<'b, T>>::Item: std::ops::Deref<Target = T>,
| ^^^^^^^^^^ required by this bound in `UnsafeCopy`
help: consider further restricting this bound
help: consider further restricting this bound with trait `<Target = T>`
|
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<'_, T> for T {
| ++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: required by a bound in `copy`
|
LL | fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
| ^^^^^ required by this bound in `copy`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::marker::Copy`
|
LL | pub fn copy_any<T: std::marker::Copy>(t: &T) -> T {
| +++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: required by a bound in `Complete::Assoc`
|
LL | type Assoc: Partial<Self>;
| ^^^^^^^^^^^^^ required by this bound in `Complete::Assoc`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::marker::Copy`
|
LL | impl<T: std::marker::Copy> Complete for T {
| +++++++++++++++++++
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | |
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
| |______________________________________________^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -24,7 +24,7 @@ LL | |
LL | | }
| |_^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -38,7 +38,7 @@ LL | | &self,
LL | | ) -> Self::AssocType;
| |_________________________^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -61,7 +61,7 @@ error[E0277]: the trait bound `Bug: Foo` is not satisfied
LL | ) -> Self::AssocType;
| ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issue-70818.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: captured value is not `Send`
|
LL | async { (ty, ty1) }
| ^^^ has type `U` which is not `Send`
help: consider restricting type parameter `U`
help: consider restricting type parameter `U` with trait `std::marker::Send`
|
LL | fn foo<T: Send, U: std::marker::Send>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + Send {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issue-86507.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
LL | let x = x;
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
= 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)>>`
help: consider further restricting this bound
help: consider further restricting this bound with trait `std::marker::Sync`
|
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)
| +++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
| ^ - you could clone this value
| |
| consider constraining this type parameter with `Clone`
help: consider further restricting this bound
help: consider further restricting this bound with trait `Copy`
|
LL | fn copy<T: Magic + Copy>(x: T) -> (T, T) { (x, x) }
| ++++++
Expand Down
Loading

0 comments on commit e3dfae8

Please sign in to comment.