1
Fork 0

Do not talk about "trait <Foo = Bar>"

Pass in an appropriate `Option<DefId>` in more cases from hir ty lowering.
This commit is contained in:
Esteban Küber 2024-11-28 23:40:17 +00:00
parent af09423967
commit b466405890
9 changed files with 21 additions and 13 deletions

View file

@ -279,7 +279,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
} else {
let mut err = self.dcx().create_err(err);
if suggest_constraining_type_param(
tcx, generics, &mut err, &qself_str, &trait_ref, None, None,
tcx,
generics,
&mut err,
&qself_str,
&trait_ref,
Some(best_trait),
None,
) && !identically_named
{
// We suggested constraining a type parameter, but the associated item on it

View file

@ -345,6 +345,7 @@ pub fn suggest_constraining_type_params<'a>(
let mut constraint = constraints.iter().map(|&(c, _, _)| c).collect::<Vec<_>>();
constraint.sort();
constraint.dedup();
let all_known = constraints.iter().all(|&(_, def_id, _)| def_id.is_some());
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 {
@ -360,7 +361,8 @@ pub fn suggest_constraining_type_params<'a>(
trait_names.dedup();
let n = trait_names.len();
let stable = if all_stable { "" } else { "unstable " };
format!("{stable}trait{} {}", pluralize!(n), match &trait_names[..] {
let trait_ = if all_known { "trait" } else { "" };
format!("{stable}{trait_}{} {}", pluralize!(n), match &trait_names[..] {
[t] => t.to_string(),
[ts @ .., last] => format!("{} and {last}", ts.join(", ")),
[] => return false,
@ -370,7 +372,7 @@ pub fn suggest_constraining_type_params<'a>(
let mut trait_names = constraints
.iter()
.map(|&(c, def_id, stable)| match def_id {
None => format!("{stable}trait `{c}`"),
None => format!("`{c}`"),
Some(def_id) => format!("{stable}trait `{}`", tcx.item_name(def_id)),
})
.collect::<Vec<_>>();

View file

@ -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 with trait `<Target = T>`
help: consider further restricting this bound with `<Target = T>`
|
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<'_, T> for T {
| ++++++++++++

View file

@ -13,7 +13,7 @@ note: required by a bound in `UnsafeCopy::Item`
|
LL | type Item<'a>: std::ops::Deref<Target = T>;
| ^^^^^^^^^^ required by this bound in `UnsafeCopy::Item`
help: consider further restricting this bound with trait `<Target = T>`
help: consider further restricting this bound with `<Target = T>`
|
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<T> for T {
| ++++++++++++

View file

@ -35,7 +35,7 @@ note: tuple struct defined here
|
LL | struct A<B>(B);
| ^
help: consider further restricting this bound with trait `<Output = B>`
help: consider further restricting this bound with `<Output = B>`
|
LL | impl<B> Add for A<B> where B: Add<Output = B> {
| ++++++++++++
@ -58,7 +58,7 @@ note: tuple struct defined here
|
LL | struct C<B>(B);
| ^
help: consider further restricting this bound with trait `<Output = B>`
help: consider further restricting this bound with `<Output = B>`
|
LL | impl<B: Add<Output = B>> Add for C<B> {
| ++++++++++++
@ -94,7 +94,7 @@ note: tuple struct defined here
|
LL | struct E<B>(B);
| ^
help: consider further restricting this bound with trait `<Output = B>`
help: consider further restricting this bound with `<Output = B>`
|
LL | impl<B: Add<Output = B>> Add for E<B> where <B as Add>::Output = B {
| ++++++++++++

View file

@ -20,7 +20,7 @@ LL | Ok(self)
| this argument influences the type of `Ok`
note: tuple variant defined here
--> $SRC_DIR/core/src/result.rs:LL:COL
help: consider further restricting this bound with trait `<Output = T>`
help: consider further restricting this bound with `<Output = T>`
|
LL | impl<T: TryAdd<Output = T>> TryAdd for Option<T> {
| ++++++++++++
@ -47,7 +47,7 @@ LL | Ok(self)
| this argument influences the type of `Ok`
note: tuple variant defined here
--> $SRC_DIR/core/src/result.rs:LL:COL
help: consider further restricting this bound with trait `, Output = T`
help: consider further restricting this bound with `, Output = T`
|
LL | impl<T: TryAdd<Error = X, Output = T>> TryAdd for Other<T> {
| ++++++++++++

View file

@ -11,7 +11,7 @@ LL | return a.bar();
= note: expected type parameter `B`
found associated type `<A as MyTrait>::T`
= note: the caller chooses a type for `B` which can be different from `<A as MyTrait>::T`
help: consider further restricting this bound with trait `<T = B>`
help: consider further restricting this bound with `<T = B>`
|
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
| +++++++

View file

@ -12,7 +12,7 @@ note: the `Copy` impl for `Foo<'any>` requires that `'any: 'static`
|
LL | struct Bar<'lt>(Foo<'lt>);
| ^^^^^^^^
help: consider restricting type parameter `'any` with trait `'static`
help: consider restricting type parameter `'any` with `'static`
|
LL | impl<'any: 'static> Copy for Bar<'any> {}
| +++++++++

View file

@ -30,7 +30,7 @@ error[E0220]: associated type `Proj` not found for `T`
LL | type ProjOf<T> = T::Proj;
| ^^^^ there is an associated type `Proj` in the trait `Parametrized`
|
help: consider restricting type parameter `T` with trait `Parametrized</* 'a, T, N */>`
help: consider restricting type parameter `T` with trait `Parametrized`
|
LL | type ProjOf<T: Parametrized</* 'a, T, N */>> = T::Proj;
| ++++++++++++++++++++++++++++++