Skip to content

Commit 96d282c

Browse files
authored
Rollup merge of #139654 - nnethercote:AssocKind-descr, r=compiler-errors
Improve `AssocItem::descr`. The commit adds "associated" to the description of associated types and associated consts, to match the description of associated functions. This increases error message precision and consistency with `AssocKind::fmt`. The commit also notes an imperfection in `AssocKind::fmt`; fixing this imperfection is possible but beyond the scope of this PR. r? `@estebank`
2 parents 25d282e + 7e8184f commit 96d282c

21 files changed

+53
-51
lines changed

Diff for: compiler/rustc_middle/src/ty/assoc.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ impl AssocItem {
9898

9999
pub fn descr(&self) -> &'static str {
100100
match self.kind {
101-
ty::AssocKind::Const => "const",
101+
ty::AssocKind::Const => "associated const",
102102
ty::AssocKind::Fn if self.fn_has_self_parameter => "method",
103103
ty::AssocKind::Fn => "associated function",
104-
ty::AssocKind::Type => "type",
104+
ty::AssocKind::Type => "associated type",
105105
}
106106
}
107107

@@ -155,6 +155,8 @@ impl AssocKind {
155155
impl std::fmt::Display for AssocKind {
156156
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
157157
match self {
158+
// FIXME: fails to distinguish between "associated function" and
159+
// "method" because `has_self` isn't known here.
158160
AssocKind::Fn => write!(f, "method"),
159161
AssocKind::Const => write!(f, "associated const"),
160162
AssocKind::Type => write!(f, "associated type"),

Diff for: tests/ui/consts/static-default-lifetime/elided-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Bar for Foo<'_> {
1616
const STATIC: &str = "";
1717
//~^ ERROR `&` without an explicit lifetime name cannot be used here
1818
//~| WARN this was previously accepted by the compiler but is being phased out
19-
//~| ERROR lifetime parameters or bounds on const `STATIC` do not match the trait declaration
19+
//~| ERROR lifetime parameters or bounds on associated const `STATIC` do not match the trait declaration
2020
}
2121

2222
fn main() {}

Diff for: tests/ui/consts/static-default-lifetime/elided-lifetime.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ help: use the `'static` lifetime
3939
LL | const STATIC: &'static str = "";
4040
| +++++++
4141

42-
error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
42+
error[E0195]: lifetime parameters or bounds on associated const `STATIC` do not match the trait declaration
4343
--> $DIR/elided-lifetime.rs:16:17
4444
|
4545
LL | const STATIC: &str;
46-
| - lifetimes in impl do not match this const in trait
46+
| - lifetimes in impl do not match this associated const in trait
4747
...
4848
LL | const STATIC: &str = "";
49-
| ^ lifetimes do not match const in trait
49+
| ^ lifetimes do not match associated const in trait
5050

5151
error: aborting due to 3 previous errors
5252

Diff for: tests/ui/consts/static-default-lifetime/static-trait-impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Bar<'_> for A {
99
const STATIC: &str = "";
1010
//~^ ERROR `&` without an explicit lifetime name cannot be used here
1111
//~| WARN this was previously accepted by the compiler but is being phased out
12-
//~| ERROR lifetime parameters or bounds on const `STATIC` do not match the trait declaration
12+
//~| ERROR lifetime parameters or bounds on associated const `STATIC` do not match the trait declaration
1313
}
1414

1515
struct B;

Diff for: tests/ui/consts/static-default-lifetime/static-trait-impl.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ help: use the `'static` lifetime
2121
LL | const STATIC: &'static str = "";
2222
| +++++++
2323

24-
error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
24+
error[E0195]: lifetime parameters or bounds on associated const `STATIC` do not match the trait declaration
2525
--> $DIR/static-trait-impl.rs:9:17
2626
|
2727
LL | const STATIC: &'a str;
28-
| - lifetimes in impl do not match this const in trait
28+
| - lifetimes in impl do not match this associated const in trait
2929
...
3030
LL | const STATIC: &str = "";
31-
| ^ lifetimes do not match const in trait
31+
| ^ lifetimes do not match associated const in trait
3232

3333
error: aborting due to 2 previous errors
3434

Diff for: tests/ui/generic-associated-types/const_params_have_right_type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0053]: type `Foo` has an incompatible generic parameter for trait `Trait`
1+
error[E0053]: associated type `Foo` has an incompatible generic parameter for trait `Trait`
22
--> $DIR/const_params_have_right_type.rs:6:14
33
|
44
LL | trait Trait {

Diff for: tests/ui/generic-associated-types/issue-102114.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: type `B` has 1 type parameter but its trait declaration has 0 type parameters
1+
error[E0049]: associated type `B` has 1 type parameter but its trait declaration has 0 type parameters
22
--> $DIR/issue-102114.rs:15:12
33
|
44
LL | type B<'b>;

Diff for: tests/ui/generic-associated-types/issue-102114.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: type `B` has 1 type parameter but its trait declaration has 0 type parameters
1+
error[E0049]: associated type `B` has 1 type parameter but its trait declaration has 0 type parameters
22
--> $DIR/issue-102114.rs:15:12
33
|
44
LL | type B<'b>;

Diff for: tests/ui/generic-associated-types/parameter_number_and_kind_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ struct Fooy;
1212

1313
impl Foo for Fooy {
1414
type A = u32;
15-
//~^ ERROR lifetime parameters or bounds on type `A` do not match the trait declaration
15+
//~^ ERROR lifetime parameters or bounds on associated type `A` do not match the trait declaration
1616
type B<'a, T> = Vec<T>;
1717
//~^ ERROR type `B` has 1 type parameter but its trait declaration has 0 type parameters
1818
type C<'a> = u32;
19-
//~^ ERROR lifetime parameters or bounds on type `C` do not match the trait declaration
19+
//~^ ERROR lifetime parameters or bounds on associated type `C` do not match the trait declaration
2020
}
2121

2222
struct Fooer;
@@ -25,7 +25,7 @@ impl Foo for Fooer {
2525
type A<T> = u32;
2626
//~^ ERROR type `A` has 1 type parameter but its trait declaration has 0 type parameters
2727
type B<'a> = u32;
28-
//~^ ERROR lifetime parameters or bounds on type `B` do not match the trait declaration
28+
//~^ ERROR lifetime parameters or bounds on associated type `B` do not match the trait declaration
2929
type C<T> = T;
3030
//~^ ERROR type `C` has 1 type parameter but its trait declaration has 0 type parameters
3131
}

Diff for: tests/ui/generic-associated-types/parameter_number_and_kind_impl.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0195]: lifetime parameters or bounds on type `A` do not match the trait declaration
1+
error[E0195]: lifetime parameters or bounds on associated type `A` do not match the trait declaration
22
--> $DIR/parameter_number_and_kind_impl.rs:14:11
33
|
44
LL | type A<'a>;
5-
| ---- lifetimes in impl do not match this type in trait
5+
| ---- lifetimes in impl do not match this associated type in trait
66
...
77
LL | type A = u32;
8-
| ^ lifetimes do not match type in trait
8+
| ^ lifetimes do not match associated type in trait
99

10-
error[E0049]: type `B` has 1 type parameter but its trait declaration has 0 type parameters
10+
error[E0049]: associated type `B` has 1 type parameter but its trait declaration has 0 type parameters
1111
--> $DIR/parameter_number_and_kind_impl.rs:16:12
1212
|
1313
LL | type B<'a, 'b>;
@@ -20,16 +20,16 @@ LL | type B<'a, T> = Vec<T>;
2020
| |
2121
| found 1 type parameter
2222

23-
error[E0195]: lifetime parameters or bounds on type `C` do not match the trait declaration
23+
error[E0195]: lifetime parameters or bounds on associated type `C` do not match the trait declaration
2424
--> $DIR/parameter_number_and_kind_impl.rs:18:11
2525
|
2626
LL | type C;
27-
| - lifetimes in impl do not match this type in trait
27+
| - lifetimes in impl do not match this associated type in trait
2828
...
2929
LL | type C<'a> = u32;
30-
| ^^^^ lifetimes do not match type in trait
30+
| ^^^^ lifetimes do not match associated type in trait
3131

32-
error[E0049]: type `A` has 1 type parameter but its trait declaration has 0 type parameters
32+
error[E0049]: associated type `A` has 1 type parameter but its trait declaration has 0 type parameters
3333
--> $DIR/parameter_number_and_kind_impl.rs:25:12
3434
|
3535
LL | type A<'a>;
@@ -38,16 +38,16 @@ LL | type A<'a>;
3838
LL | type A<T> = u32;
3939
| ^ found 1 type parameter
4040

41-
error[E0195]: lifetime parameters or bounds on type `B` do not match the trait declaration
41+
error[E0195]: lifetime parameters or bounds on associated type `B` do not match the trait declaration
4242
--> $DIR/parameter_number_and_kind_impl.rs:27:11
4343
|
4444
LL | type B<'a, 'b>;
45-
| -------- lifetimes in impl do not match this type in trait
45+
| -------- lifetimes in impl do not match this associated type in trait
4646
...
4747
LL | type B<'a> = u32;
48-
| ^^^^ lifetimes do not match type in trait
48+
| ^^^^ lifetimes do not match associated type in trait
4949

50-
error[E0049]: type `C` has 1 type parameter but its trait declaration has 0 type parameters
50+
error[E0049]: associated type `C` has 1 type parameter but its trait declaration has 0 type parameters
5151
--> $DIR/parameter_number_and_kind_impl.rs:29:12
5252
|
5353
LL | type C;

Diff for: tests/ui/generic-const-items/assoc-const-missing-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Trait for () {
1414
//~| ERROR mismatched types
1515
const Q = "";
1616
//~^ ERROR missing type for `const` item
17-
//~| ERROR lifetime parameters or bounds on const `Q` do not match the trait declaration
17+
//~| ERROR lifetime parameters or bounds on associated const `Q` do not match the trait declaration
1818
}
1919

2020
fn main() {}

Diff for: tests/ui/generic-const-items/assoc-const-missing-type.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ error: missing type for `const` item
1515
LL | const K<T> = ();
1616
| ^ help: provide a type for the associated constant: `()`
1717

18-
error[E0195]: lifetime parameters or bounds on const `Q` do not match the trait declaration
18+
error[E0195]: lifetime parameters or bounds on associated const `Q` do not match the trait declaration
1919
--> $DIR/assoc-const-missing-type.rs:15:12
2020
|
2121
LL | const Q<'a>: &'a str;
22-
| ---- lifetimes in impl do not match this const in trait
22+
| ---- lifetimes in impl do not match this associated const in trait
2323
...
2424
LL | const Q = "";
25-
| ^ lifetimes do not match const in trait
25+
| ^ lifetimes do not match associated const in trait
2626

2727
error: missing type for `const` item
2828
--> $DIR/assoc-const-missing-type.rs:15:12

Diff for: tests/ui/generic-const-items/compare-impl-item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<P> Trait<P> for () {
2222
const D<const N: u16>: u16 = N;
2323
//~^ ERROR const `D` has an incompatible generic parameter for trait `Trait`
2424
const E: &'static () = &();
25-
//~^ ERROR lifetime parameters or bounds on const `E` do not match the trait declaration
25+
//~^ ERROR lifetime parameters or bounds on associated const `E` do not match the trait declaration
2626

2727
const F: usize = 1024
2828
where

Diff for: tests/ui/generic-const-items/compare-impl-item.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: const `A` has 1 type parameter but its trait declaration has 0 type parameters
1+
error[E0049]: associated const `A` has 1 type parameter but its trait declaration has 0 type parameters
22
--> $DIR/compare-impl-item.rs:16:13
33
|
44
LL | const A: ();
@@ -7,7 +7,7 @@ LL | const A: ();
77
LL | const A<T>: () = ();
88
| ^ found 1 type parameter
99

10-
error[E0049]: const `B` has 1 const parameter but its trait declaration has 2 const parameters
10+
error[E0049]: associated const `B` has 1 const parameter but its trait declaration has 2 const parameters
1111
--> $DIR/compare-impl-item.rs:18:13
1212
|
1313
LL | const B<const K: u64, const Q: u64>: u64;
@@ -18,7 +18,7 @@ LL | const B<const K: u64, const Q: u64>: u64;
1818
LL | const B<const K: u64>: u64 = 0;
1919
| ^^^^^^^^^^^^ found 1 const parameter
2020

21-
error[E0049]: const `C` has 0 type parameters but its trait declaration has 1 type parameter
21+
error[E0049]: associated const `C` has 0 type parameters but its trait declaration has 1 type parameter
2222
--> $DIR/compare-impl-item.rs:20:13
2323
|
2424
LL | const C<T>: T;
@@ -27,7 +27,7 @@ LL | const C<T>: T;
2727
LL | const C<'a>: &'a str = "";
2828
| ^^ found 0 type parameters
2929

30-
error[E0053]: const `D` has an incompatible generic parameter for trait `Trait`
30+
error[E0053]: associated const `D` has an incompatible generic parameter for trait `Trait`
3131
--> $DIR/compare-impl-item.rs:22:13
3232
|
3333
LL | trait Trait<P> {
@@ -42,14 +42,14 @@ LL | impl<P> Trait<P> for () {
4242
LL | const D<const N: u16>: u16 = N;
4343
| ^^^^^^^^^^^^ found const parameter of type `u16`
4444

45-
error[E0195]: lifetime parameters or bounds on const `E` do not match the trait declaration
45+
error[E0195]: lifetime parameters or bounds on associated const `E` do not match the trait declaration
4646
--> $DIR/compare-impl-item.rs:24:12
4747
|
4848
LL | const E<'a>: &'a ();
49-
| ---- lifetimes in impl do not match this const in trait
49+
| ---- lifetimes in impl do not match this associated const in trait
5050
...
5151
LL | const E: &'static () = &();
52-
| ^ lifetimes do not match const in trait
52+
| ^ lifetimes do not match associated const in trait
5353

5454
error[E0276]: impl has stricter requirements than trait
5555
--> $DIR/compare-impl-item.rs:29:12

Diff for: tests/ui/impl-trait/ice-unexpected-param-type-whensubstituting-in-region-112823.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0407]: method `line_stream` is not a member of trait `X`
44
LL | fn line_stream<'a, Repr>(&'a self) -> Self::LineStreamFut<'a, Repr> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `X`
66

7-
error[E0049]: type `LineStream` has 0 type parameters but its trait declaration has 1 type parameter
7+
error[E0049]: associated type `LineStream` has 0 type parameters but its trait declaration has 1 type parameter
88
--> $DIR/ice-unexpected-param-type-whensubstituting-in-region-112823.rs:25:21
99
|
1010
LL | type LineStream<'a, Repr>

Diff for: tests/ui/impl-trait/ice-unexpected-param-type-whensubstituting-in-region-112823.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0407]: method `line_stream` is not a member of trait `X`
44
LL | fn line_stream<'a, Repr>(&'a self) -> Self::LineStreamFut<'a, Repr> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `X`
66

7-
error[E0049]: type `LineStream` has 0 type parameters but its trait declaration has 1 type parameter
7+
error[E0049]: associated type `LineStream` has 0 type parameters but its trait declaration has 1 type parameter
88
--> $DIR/ice-unexpected-param-type-whensubstituting-in-region-112823.rs:25:21
99
|
1010
LL | type LineStream<'a, Repr>

Diff for: tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub trait Iterable {
88

99
impl<'a, I: 'a + Iterable> Iterable for &'a I {
1010
type Item = u32;
11-
//~^ ERROR lifetime parameters or bounds on type `Item` do not match the trait declaration
11+
//~^ ERROR lifetime parameters or bounds on associated type `Item` do not match the trait declaration
1212

1313
fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missing>> {}
1414
//~^ ERROR binding for associated type `Item` references lifetime `'missing`

Diff for: tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ LL | fn iter(&self) -> impl for<'missing> Iterator<Item = Self::Item<'missin
1212
|
1313
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1414

15-
error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration
15+
error[E0195]: lifetime parameters or bounds on associated type `Item` do not match the trait declaration
1616
--> $DIR/span-bug-issue-121457.rs:10:14
1717
|
1818
LL | type Item<'a>
19-
| ---- lifetimes in impl do not match this type in trait
19+
| ---- lifetimes in impl do not match this associated type in trait
2020
LL | where
2121
LL | Self: 'a;
2222
| -- this bound might be missing in the impl
2323
...
2424
LL | type Item = u32;
25-
| ^ lifetimes do not match type in trait
25+
| ^ lifetimes do not match associated type in trait
2626

2727
error[E0277]: `()` is not an iterator
2828
--> $DIR/span-bug-issue-121457.rs:13:23

Diff for: tests/ui/lifetimes/no_lending_iterators.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Bar for usize {
2525

2626
impl Bar for isize {
2727
type Item<'a> = &'a isize;
28-
//~^ ERROR 27:14: 27:18: lifetime parameters or bounds on type `Item` do not match the trait declaration [E0195]
28+
//~^ ERROR 27:14: 27:18: lifetime parameters or bounds on associated type `Item` do not match the trait declaration [E0195]
2929

3030
fn poke(&mut self, item: Self::Item) {
3131
self += *item;

Diff for: tests/ui/lifetimes/no_lending_iterators.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ error: in the trait associated type is declared without lifetime parameters, so
1616
LL | type Item = &usize;
1717
| ^ this lifetime must come from the implemented type
1818

19-
error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration
19+
error[E0195]: lifetime parameters or bounds on associated type `Item` do not match the trait declaration
2020
--> $DIR/no_lending_iterators.rs:27:14
2121
|
2222
LL | type Item;
23-
| - lifetimes in impl do not match this type in trait
23+
| - lifetimes in impl do not match this associated type in trait
2424
...
2525
LL | type Item<'a> = &'a isize;
26-
| ^^^^ lifetimes do not match type in trait
26+
| ^^^^ lifetimes do not match associated type in trait
2727

2828
error: aborting due to 3 previous errors
2929

Diff for: tests/ui/type-alias-impl-trait/impl-trait-in-type-alias-with-bad-substs.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: type `Baz` has 1 type parameter but its trait declaration has 0 type parameters
1+
error[E0049]: associated type `Baz` has 1 type parameter but its trait declaration has 0 type parameters
22
--> $DIR/impl-trait-in-type-alias-with-bad-substs.rs:19:14
33
|
44
LL | type Baz<'a>;

0 commit comments

Comments
 (0)