Skip to content

Commit 6b758e2

Browse files
committed
Do not talk about "trait <Foo = Bar>"
Pass in an appropriate `Option<DefId>` in more cases from hir ty lowering.
1 parent 055dbc5 commit 6b758e2

11 files changed

+27
-19
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
279279
} else {
280280
let mut err = self.dcx().create_err(err);
281281
if suggest_constraining_type_param(
282-
tcx, generics, &mut err, &qself_str, &trait_ref, None, None,
282+
tcx,
283+
generics,
284+
&mut err,
285+
&qself_str,
286+
&trait_ref,
287+
Some(best_trait),
288+
None,
283289
) && !identically_named
284290
{
285291
// We suggested constraining a type parameter, but the associated item on it

compiler/rustc_middle/src/ty/diagnostics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ pub fn suggest_constraining_type_params<'a>(
345345
let mut constraint = constraints.iter().map(|&(c, _, _)| c).collect::<Vec<_>>();
346346
constraint.sort();
347347
constraint.dedup();
348+
let all_known = constraints.iter().all(|&(_, def_id, _)| def_id.is_some());
348349
let all_stable = constraints.iter().all(|&(_, _, stable)| stable.is_empty());
349350
let all_unstable = constraints.iter().all(|&(_, _, stable)| !stable.is_empty());
350351
let post = if all_stable || all_unstable {
@@ -360,7 +361,8 @@ pub fn suggest_constraining_type_params<'a>(
360361
trait_names.dedup();
361362
let n = trait_names.len();
362363
let stable = if all_stable { "" } else { "unstable " };
363-
format!("{stable}trait{} {}", pluralize!(n), match &trait_names[..] {
364+
let trait_ = if all_known { "trait" } else { "" };
365+
format!("{stable}{trait_}{} {}", pluralize!(n), match &trait_names[..] {
364366
[t] => t.to_string(),
365367
[ts @ .., last] => format!("{} and {last}", ts.join(", ")),
366368
[] => return false,
@@ -370,7 +372,7 @@ pub fn suggest_constraining_type_params<'a>(
370372
let mut trait_names = constraints
371373
.iter()
372374
.map(|&(c, def_id, stable)| match def_id {
373-
None => format!("{stable}trait `{c}`"),
375+
None => format!("`{c}`"),
374376
Some(def_id) => format!("{stable}trait `{}`", tcx.item_name(def_id)),
375377
})
376378
.collect::<Vec<_>>();

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

tests/ui/generic-associated-types/issue-68656-unsized-values.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: required by a bound in `UnsafeCopy::Item`
1313
|
1414
LL | type Item<'a>: std::ops::Deref<Target = T>;
1515
| ^^^^^^^^^^ required by this bound in `UnsafeCopy::Item`
16-
help: consider further restricting this bound with trait `<Target = T>`
16+
help: consider further restricting this bound with `<Target = T>`
1717
|
1818
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<T> for T {
1919
| ++++++++++++

tests/ui/generic-associated-types/missing-bounds.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ note: tuple struct defined here
3535
|
3636
LL | struct A<B>(B);
3737
| ^
38-
help: consider further restricting this bound with trait `<Output = B>`
38+
help: consider further restricting this bound with `<Output = B>`
3939
|
4040
LL | impl<B> Add for A<B> where B: Add<Output = B> {
4141
| ++++++++++++
@@ -58,7 +58,7 @@ note: tuple struct defined here
5858
|
5959
LL | struct C<B>(B);
6060
| ^
61-
help: consider further restricting this bound with trait `<Output = B>`
61+
help: consider further restricting this bound with `<Output = B>`
6262
|
6363
LL | impl<B: Add<Output = B>> Add for C<B> {
6464
| ++++++++++++
@@ -94,7 +94,7 @@ note: tuple struct defined here
9494
|
9595
LL | struct E<B>(B);
9696
| ^
97-
help: consider further restricting this bound with trait `<Output = B>`
97+
help: consider further restricting this bound with `<Output = B>`
9898
|
9999
LL | impl<B: Add<Output = B>> Add for E<B> where <B as Add>::Output = B {
100100
| ++++++++++++

tests/ui/moves/use_of_moved_value_copy_suggestions.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ fn duplicate_custom_1<T: Copy + Trait>(t: S<T>) -> (S<T>, S<T>) where {
4848
fn duplicate_custom_2<T>(t: S<T>) -> (S<T>, S<T>)
4949
where
5050
T: A + Copy + Trait,
51-
//~^ HELP consider further restricting this bound
51+
//~^ HELP consider further restricting
5252
{
5353
(t, t) //~ use of moved value: `t`
5454
}
5555

5656
fn duplicate_custom_3<T>(t: S<T>) -> (S<T>, S<T>)
5757
where
5858
T: A + Copy + Trait,
59-
//~^ HELP consider further restricting this bound
59+
//~^ HELP consider further restricting
6060
T: B,
6161
{
6262
(t, t) //~ use of moved value: `t`
6363
}
6464

6565
fn duplicate_custom_4<T: A + Copy + Trait>(t: S<T>) -> (S<T>, S<T>)
66-
//~^ HELP consider further restricting this bound
66+
//~^ HELP consider further restricting
6767
where
6868
T: B,
6969
{

tests/ui/moves/use_of_moved_value_copy_suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ fn duplicate_custom_1<T>(t: S<T>) -> (S<T>, S<T>) where {
4848
fn duplicate_custom_2<T>(t: S<T>) -> (S<T>, S<T>)
4949
where
5050
T: A,
51-
//~^ HELP consider further restricting this bound
51+
//~^ HELP consider further restricting
5252
{
5353
(t, t) //~ use of moved value: `t`
5454
}
5555

5656
fn duplicate_custom_3<T>(t: S<T>) -> (S<T>, S<T>)
5757
where
5858
T: A,
59-
//~^ HELP consider further restricting this bound
59+
//~^ HELP consider further restricting
6060
T: B,
6161
{
6262
(t, t) //~ use of moved value: `t`
6363
}
6464

6565
fn duplicate_custom_4<T: A>(t: S<T>) -> (S<T>, S<T>)
66-
//~^ HELP consider further restricting this bound
66+
//~^ HELP consider further restricting
6767
where
6868
T: B,
6969
{

tests/ui/suggestions/restrict-existing-type-bounds.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | Ok(self)
2020
| this argument influences the type of `Ok`
2121
note: tuple variant defined here
2222
--> $SRC_DIR/core/src/result.rs:LL:COL
23-
help: consider further restricting this bound with trait `<Output = T>`
23+
help: consider further restricting this bound with `<Output = T>`
2424
|
2525
LL | impl<T: TryAdd<Output = T>> TryAdd for Option<T> {
2626
| ++++++++++++
@@ -47,7 +47,7 @@ LL | Ok(self)
4747
| this argument influences the type of `Ok`
4848
note: tuple variant defined here
4949
--> $SRC_DIR/core/src/result.rs:LL:COL
50-
help: consider further restricting this bound with trait `, Output = T`
50+
help: consider further restricting this bound with `, Output = T`
5151
|
5252
LL | impl<T: TryAdd<Error = X, Output = T>> TryAdd for Other<T> {
5353
| ++++++++++++

tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr

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

tests/ui/traits/copy-is-not-modulo-regions.not_static.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: the `Copy` impl for `Foo<'any>` requires that `'any: 'static`
1212
|
1313
LL | struct Bar<'lt>(Foo<'lt>);
1414
| ^^^^^^^^
15-
help: consider restricting type parameter `'any` with trait `'static`
15+
help: consider restricting type parameter `'any` with `'static`
1616
|
1717
LL | impl<'any: 'static> Copy for Bar<'any> {}
1818
| +++++++++

tests/ui/type-alias/unresolved-assoc-ty-suggest-trait.lazy.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ error[E0220]: associated type `Proj` not found for `T`
3030
LL | type ProjOf<T> = T::Proj;
3131
| ^^^^ there is an associated type `Proj` in the trait `Parametrized`
3232
|
33-
help: consider restricting type parameter `T` with trait `Parametrized</* 'a, T, N */>`
33+
help: consider restricting type parameter `T` with trait `Parametrized`
3434
|
3535
LL | type ProjOf<T: Parametrized</* 'a, T, N */>> = T::Proj;
3636
| ++++++++++++++++++++++++++++++

0 commit comments

Comments
 (0)