Skip to content

Commit

Permalink
Use trait name instead of full constraint in suggestion message
Browse files Browse the repository at this point in the history
```
help: consider restricting type parameter `T` with traits `Copy` and `Trait`
   |
LL | fn duplicate_custom<T: Copy + Trait>(t: S<T>) -> (S<T>, S<T>) {
   |                      ++++++++++++++
```

```
help: consider restricting type parameter `V` with trait `Copy`
   |
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V {
   |                  +++++++++++++++++++
```
  • Loading branch information
estebank committed Nov 28, 2024
1 parent 7e38a45 commit c34079a
Show file tree
Hide file tree
Showing 99 changed files with 230 additions and 192 deletions.
84 changes: 61 additions & 23 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::fmt::Write;
use std::ops::ControlFlow;

use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diag, DiagArgValue, IntoDiagArg, into_diag_arg_using_display};
use rustc_errors::{
Applicability, Diag, DiagArgValue, IntoDiagArg, into_diag_arg_using_display, pluralize,
};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, LangItem, PredicateOrigin, WherePredicateKind};
Expand Down Expand Up @@ -288,7 +290,11 @@ pub fn suggest_constraining_type_params<'a>(
None => true,
};
if stable || tcx.sess.is_nightly_build() {
grouped.entry(param_name).or_insert(Vec::new()).push((constraint, def_id));
grouped.entry(param_name).or_insert(Vec::new()).push((
constraint,
def_id,
if stable { "" } else { "unstable " },
));
if !stable {
unstable_suggestion = true;
}
Expand All @@ -303,10 +309,10 @@ pub fn suggest_constraining_type_params<'a>(
let Some(param) = param else { return false };

{
let mut sized_constraints = constraints.extract_if(|(_, def_id)| {
let mut sized_constraints = constraints.extract_if(|(_, def_id, _)| {
def_id.is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Sized))
});
if let Some((_, def_id)) = sized_constraints.next() {
if let Some((_, def_id, _)) = sized_constraints.next() {
applicability = Applicability::MaybeIncorrect;

err.span_label(param.span, "this type parameter needs to be `Sized`");
Expand All @@ -325,15 +331,52 @@ pub fn suggest_constraining_type_params<'a>(
.collect();

constraints
.retain(|(_, def_id)| def_id.map_or(true, |def| !bound_trait_defs.contains(&def)));
.retain(|(_, def_id, _)| def_id.map_or(true, |def| !bound_trait_defs.contains(&def)));

if constraints.is_empty() {
continue;
}

let mut constraint = constraints.iter().map(|&(c, _)| c).collect::<Vec<_>>();
let mut constraint = constraints.iter().map(|&(c, _, _)| c).collect::<Vec<_>>();
constraint.sort();
constraint.dedup();
let all_stable = constraints.iter().all(|&(_, _, stable)| stable.is_empty());
let all_unstable = constraints.iter().all(|&(_, _, stable)| !stable.is_empty());
let post = if all_stable || all_unstable {
// Don't redundantly say "trait `X`, trait `Y`", instead "traits `X` and `Y`"
let mut trait_names = constraints
.iter()
.map(|&(c, def_id, _)| match def_id {
None => format!("`{c}`"),
Some(def_id) => format!("`{}`", tcx.item_name(def_id)),
})
.collect::<Vec<_>>();
trait_names.sort();
trait_names.dedup();
let n = trait_names.len();
let stable = if all_stable { "" } else { "unstable " };
format!("{stable}trait{} {}", pluralize!(n), match &trait_names[..] {
[t] => t.to_string(),
[ts @ .., last] => format!("{} and {last}", ts.join(", ")),
[] => return false,
},)
} else {
// We're more explicit when there's a mix of stable and unstable traits.
let mut trait_names = constraints
.iter()
.map(|&(c, def_id, stable)| match def_id {
None => format!("{stable}trait `{c}`"),
Some(def_id) => format!("{stable}trait `{}`", tcx.item_name(def_id)),
})
.collect::<Vec<_>>();
trait_names.sort();
trait_names.dedup();
match &trait_names[..] {
[t] => t.to_string(),
[ts @ .., last] => format!("{} and {last}", ts.join(", ")),
[] => return false,
}
};
let constraint = constraint.join(" + ");
let mut suggest_restrict = |span, bound_list_non_empty, open_paren_sp| {
let suggestion = if span_to_replace.is_some() {
Expand All @@ -351,18 +394,18 @@ pub fn suggest_constraining_type_params<'a>(
if let Some(open_paren_sp) = open_paren_sp {
suggestions.push((
open_paren_sp,
constraint.clone(),
post.clone(),
"(".to_string(),
RestrictBoundFurther,
));
suggestions.push((
span,
constraint.clone(),
post.clone(),
format!("){suggestion}"),
RestrictBoundFurther,
));
} else {
suggestions.push((span, constraint.clone(), suggestion, RestrictBoundFurther));
suggestions.push((span, post.clone(), suggestion, RestrictBoundFurther));
}
};

Expand Down Expand Up @@ -420,8 +463,8 @@ 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, _)| {
post,
constraints.iter().fold(String::new(), |mut string, &(constraint, _, _)| {
write!(string, ", {param_name}: {constraint}").unwrap();
string
}),
Expand Down Expand Up @@ -450,7 +493,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(),
post,
format!("{where_prefix} {param_name}: {constraint}"),
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
));
Expand All @@ -464,7 +507,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(),
post,
format!(" {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -477,7 +520,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(),
post,
format!(": {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -490,21 +533,16 @@ pub fn suggest_constraining_type_params<'a>(
.collect::<Vec<_>>();
let suggested = !suggestions.is_empty();
if suggestions.len() == 1 {
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 (span, post, suggestion, msg) = suggestions.pop().unwrap();
let msg = match msg {
SuggestChangingConstraintsMessage::RestrictBoundFurther => {
format!("consider further restricting this bound{post}")
format!("consider further restricting this bound with {post}")
}
SuggestChangingConstraintsMessage::RestrictType { ty } => {
format!("consider restricting type parameter `{ty}`{post}")
format!("consider restricting type parameter `{ty}` with {post}")
}
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => {
format!("consider further restricting type parameter `{ty}`{post}")
format!("consider further restricting type parameter `{ty}` with {post}")
}
SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
format!("consider removing the `?Sized` bound to make the type parameter `Sized`")
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 with trait `Bar<5>`
help: consider further restricting this bound with trait `Bar`
|
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 with trait `Bar<5>`
help: consider further restricting this bound with trait `Bar`
|
LL | pub struct Structure<C: Tec + Bar<5>> {
| ++++++++
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` with trait `std::clone::Clone`
help: consider restricting type parameter `T` with trait `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` with trait `std::clone::Clone`
help: consider further restricting type parameter `T` with trait `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` with trait `std::clone::Clone`
help: consider restricting type parameter `T` with trait `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` with trait `std::clone::Clone`
help: consider further restricting type parameter `T` with trait `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` with trait `for<'b> X<'b, T>`
help: consider restricting type parameter `T` with trait `X`
|
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 @@ -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` with trait `std::marker::Copy`
help: consider restricting type parameter `T` with trait `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` with trait `std::marker::Copy`
help: consider restricting type parameter `T` with trait `Copy`
|
LL | impl<T: std::marker::Copy> Complete for T {
| +++++++++++++++++++
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` with trait `std::marker::Send`
help: consider restricting type parameter `U` with trait `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 with trait `std::marker::Sync`
help: consider further restricting this bound with trait `Sync`
|
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/binop/issue-93927.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | val == val
| |
| MyType<T>
|
help: consider further restricting this bound with trait `std::cmp::Eq`
help: consider further restricting this bound with trait `Eq`
|
LL | fn cond<T: PartialEq + std::cmp::Eq>(val: MyType<T>) -> bool {
| ++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ note: required by a bound in `Foo`
|
LL | trait Foo : Send+Sync { }
| ^^^^ required by this bound in `Foo`
help: consider further restricting this bound with trait `std::marker::Send`
help: consider further restricting this bound with trait `Send`
|
LL | impl <T: Sync+'static + std::marker::Send> Foo for (T,) { }
| +++++++++++++++++++
Expand All @@ -27,7 +27,7 @@ note: required by a bound in `Foo`
|
LL | trait Foo : Send+Sync { }
| ^^^^ required by this bound in `Foo`
help: consider further restricting this bound with trait `std::marker::Sync`
help: consider further restricting this bound with trait `Sync`
|
LL | impl <T: Send + std::marker::Sync> Foo for (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 `RequiresRequiresShareAndSend`
|
LL | pub trait RequiresRequiresShareAndSend : RequiresShare + Send { }
| ^^^^ required by this bound in `RequiresRequiresShareAndSend`
help: consider further restricting this bound with trait `std::marker::Send`
help: consider further restricting this bound with trait `Send`
|
LL | impl <T:Sync+'static + std::marker::Send> RequiresRequiresShareAndSend for X<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 `Foo`
|
LL | trait Foo : Send { }
| ^^^^ required by this bound in `Foo`
help: consider further restricting this bound with trait `std::marker::Send`
help: consider further restricting this bound with trait `Send`
|
LL | impl <T: Sync+'static + std::marker::Send> Foo 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 `X`
|
LL | struct X<F> where F: FnOnce() + 'static + Send {
| ^^^^ required by this bound in `X`
help: consider further restricting this bound with trait `std::marker::Send`
help: consider further restricting this bound with trait `Send`
|
LL | fn foo<F>(blk: F) -> X<F> where F: FnOnce() + 'static + std::marker::Send {
| +++++++++++++++++++
Expand All @@ -25,7 +25,7 @@ note: required by a bound in `X`
|
LL | struct X<F> where F: FnOnce() + 'static + Send {
| ^^^^ required by this bound in `X`
help: consider further restricting this bound with trait `std::marker::Send`
help: consider further restricting this bound with trait `Send`
|
LL | fn foo<F>(blk: F) -> X<F> where F: FnOnce() + 'static + std::marker::Send {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/closure-bounds-subtype.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ help: use parentheses to call this type parameter
|
LL | take_const_owned(f());
| ++
help: consider further restricting this bound with trait `std::marker::Sync`
help: consider further restricting this bound with trait `Sync`
|
LL | fn give_owned<F>(f: F) where F: FnOnce() + Send + std::marker::Sync {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-61336-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | [x; { N }]
= note: the `Copy` trait is required because this value will be copied for each element of the array
= help: consider using `core::array::from_fn` to initialize the array
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information
help: consider restricting type parameter `T` with trait `std::marker::Copy`
help: consider restricting type parameter `T` with trait `Copy`
|
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-61336.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | [x; N]
= note: the `Copy` trait is required because this value will be copied for each element of the array
= help: consider using `core::array::from_fn` to initialize the array
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information
help: consider restricting type parameter `T` with trait `std::marker::Copy`
help: consider restricting type parameter `T` with trait `Copy`
|
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/ct-var-in-collect_all_mismatches.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | fn unsatisfied(self)
LL | where
LL | T: Bar<N>,
| ^^^^^^ required by this bound in `Foo::<T, N>::unsatisfied`
help: consider restricting type parameter `T` with trait `Bar<N>`
help: consider restricting type parameter `T` with trait `Bar`
|
LL | impl<T: Bar<N>, const N: usize> Foo<T, N> {
| ++++++++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dropck/explicit-drop-bounds.bad1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: required by a bound in `DropMe`
|
LL | struct DropMe<T: Copy>(T);
| ^^^^ required by this bound in `DropMe`
help: consider further restricting type parameter `T` with trait `std::marker::Copy`
help: consider further restricting type parameter `T` with trait `Copy`
|
LL | [T; 1]: Copy, T: std::marker::Copy // But `[T; 1]: Copy` does not imply `T: Copy`
| ~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -25,7 +25,7 @@ note: required by a bound in `DropMe`
|
LL | struct DropMe<T: Copy>(T);
| ^^^^ required by this bound in `DropMe`
help: consider further restricting type parameter `T` with trait `std::marker::Copy`
help: consider further restricting type parameter `T` with trait `Copy`
|
LL | [T; 1]: Copy, T: std::marker::Copy // But `[T; 1]: Copy` does not imply `T: Copy`
| ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Loading

0 comments on commit c34079a

Please sign in to comment.