Skip to content

Commit 78ff8c4

Browse files
authored
Unrolled build for rust-lang#128910
Rollup merge of rust-lang#128910 - estebank:assoc-fn, r=compiler-errors Differentiate between methods and associated functions in diagnostics Accurately refer to assoc fn without receiver as assoc fn instead of methods. Add `AssocItem::descr` method to centralize where we call methods and associated functions.
2 parents 8291d68 + 860c8cd commit 78ff8c4

27 files changed

+79
-72
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ fn check_region_bounds_on_impl_item<'tcx>(
11171117
.dcx()
11181118
.create_err(LifetimesOrBoundsMismatchOnTrait {
11191119
span,
1120-
item_kind: assoc_item_kind_str(&impl_m),
1120+
item_kind: impl_m.descr(),
11211121
ident: impl_m.ident(tcx),
11221122
generics_span,
11231123
bounds_span,
@@ -1294,7 +1294,7 @@ fn compare_number_of_generics<'tcx>(
12941294
("const", trait_own_counts.consts, impl_own_counts.consts),
12951295
];
12961296

1297-
let item_kind = assoc_item_kind_str(&impl_);
1297+
let item_kind = impl_.descr();
12981298

12991299
let mut err_occurred = None;
13001300
for (kind, trait_count, impl_count) in matchings {
@@ -1676,7 +1676,7 @@ fn compare_generic_param_kinds<'tcx>(
16761676
param_impl_span,
16771677
E0053,
16781678
"{} `{}` has an incompatible generic parameter for trait `{}`",
1679-
assoc_item_kind_str(&impl_item),
1679+
impl_item.descr(),
16801680
trait_item.name,
16811681
&tcx.def_path_str(tcx.parent(trait_item.def_id))
16821682
);
@@ -2249,14 +2249,6 @@ fn param_env_with_gat_bounds<'tcx>(
22492249
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing)
22502250
}
22512251

2252-
fn assoc_item_kind_str(impl_item: &ty::AssocItem) -> &'static str {
2253-
match impl_item.kind {
2254-
ty::AssocKind::Const => "const",
2255-
ty::AssocKind::Fn => "method",
2256-
ty::AssocKind::Type => "type",
2257-
}
2258-
}
2259-
22602252
/// Manually check here that `async fn foo()` wasn't matched against `fn foo()`,
22612253
/// and extract a better error if so.
22622254
fn try_report_async_mismatch<'tcx>(

compiler/rustc_middle/src/hir/map/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1165,17 +1165,23 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
11651165
}
11661166
Node::ImplItem(ii) => {
11671167
let kind = match ii.kind {
1168-
ImplItemKind::Const(..) => "assoc const",
1169-
ImplItemKind::Fn(..) => "method",
1170-
ImplItemKind::Type(_) => "assoc type",
1168+
ImplItemKind::Const(..) => "associated constant",
1169+
ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1170+
ImplicitSelfKind::None => "associated function",
1171+
_ => "method",
1172+
},
1173+
ImplItemKind::Type(_) => "associated type",
11711174
};
11721175
format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
11731176
}
11741177
Node::TraitItem(ti) => {
11751178
let kind = match ti.kind {
1176-
TraitItemKind::Const(..) => "assoc constant",
1177-
TraitItemKind::Fn(..) => "trait method",
1178-
TraitItemKind::Type(..) => "assoc type",
1179+
TraitItemKind::Const(..) => "associated constant",
1180+
TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1181+
ImplicitSelfKind::None => "associated function",
1182+
_ => "trait method",
1183+
},
1184+
TraitItemKind::Type(..) => "associated type",
11791185
};
11801186

11811187
format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))

compiler/rustc_middle/src/ty/assoc.rs

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ impl AssocItem {
9898
}
9999
}
100100

101+
pub fn descr(&self) -> &'static str {
102+
match self.kind {
103+
ty::AssocKind::Const => "const",
104+
ty::AssocKind::Fn if self.fn_has_self_parameter => "method",
105+
ty::AssocKind::Fn => "associated function",
106+
ty::AssocKind::Type => "type",
107+
}
108+
}
109+
101110
pub fn is_impl_trait_in_trait(&self) -> bool {
102111
self.opt_rpitit_info.is_some()
103112
}

tests/ui/async-await/in-trait/generics-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait Foo {
66

77
impl Foo for () {
88
async fn foo<const N: usize>() {}
9-
//~^ ERROR: method `foo` has an incompatible generic parameter for trait `Foo` [E0053]
9+
//~^ ERROR: associated function `foo` has an incompatible generic parameter for trait `Foo` [E0053]
1010
}
1111

1212
fn main() {}

tests/ui/async-await/in-trait/generics-mismatch.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0053]: method `foo` has an incompatible generic parameter for trait `Foo`
1+
error[E0053]: associated function `foo` has an incompatible generic parameter for trait `Foo`
22
--> $DIR/generics-mismatch.rs:8:18
33
|
44
LL | trait Foo {

tests/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@ trait Trait {
33
}
44
impl Trait for () {
55
fn foo<const M: u64>() {}
6-
//~^ error: method `foo` has an incompatible generic parameter for trait
6+
//~^ error: associated function `foo` has an incompatible generic parameter for trait
77
}
88

99
trait Other {
1010
fn bar<const M: u8>() {}
1111
}
1212
impl Other for () {
1313
fn bar<T>() {}
14-
//~^ error: method `bar` has an incompatible generic parameter for trait
14+
//~^ error: associated function `bar` has an incompatible generic parameter for trait
1515
}
1616

1717
trait Uwu {
1818
fn baz<const N: u32>() {}
1919
}
2020
impl Uwu for () {
2121
fn baz<const N: i32>() {}
22-
//~^ error: method `baz` has an incompatible generic parameter for trait
22+
//~^ error: associated function `baz` has an incompatible generic parameter for trait
2323
}
2424

2525
trait Aaaaaa {
2626
fn bbbb<const N: u32, T>() {}
2727
}
2828
impl Aaaaaa for () {
2929
fn bbbb<T, const N: u32>() {}
30-
//~^ error: method `bbbb` has an incompatible generic parameter for trait
30+
//~^ error: associated function `bbbb` has an incompatible generic parameter for trait
3131
}
3232

3333
trait Names {
3434
fn abcd<T, const N: u32>() {}
3535
}
3636
impl Names for () {
3737
fn abcd<const N: u32, T>() {}
38-
//~^ error: method `abcd` has an incompatible generic parameter for trait
38+
//~^ error: associated function `abcd` has an incompatible generic parameter for trait
3939
}
4040

4141
fn main() {}

tests/ui/const-generics/defaults/mismatched_ty_const_in_trait_impl.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0053]: method `foo` has an incompatible generic parameter for trait `Trait`
1+
error[E0053]: associated function `foo` has an incompatible generic parameter for trait `Trait`
22
--> $DIR/mismatched_ty_const_in_trait_impl.rs:5:12
33
|
44
LL | trait Trait {
@@ -11,7 +11,7 @@ LL | impl Trait for () {
1111
LL | fn foo<const M: u64>() {}
1212
| ^^^^^^^^^^^^ found const parameter of type `u64`
1313

14-
error[E0053]: method `bar` has an incompatible generic parameter for trait `Other`
14+
error[E0053]: associated function `bar` has an incompatible generic parameter for trait `Other`
1515
--> $DIR/mismatched_ty_const_in_trait_impl.rs:13:12
1616
|
1717
LL | trait Other {
@@ -24,7 +24,7 @@ LL | impl Other for () {
2424
LL | fn bar<T>() {}
2525
| ^ found type parameter
2626

27-
error[E0053]: method `baz` has an incompatible generic parameter for trait `Uwu`
27+
error[E0053]: associated function `baz` has an incompatible generic parameter for trait `Uwu`
2828
--> $DIR/mismatched_ty_const_in_trait_impl.rs:21:12
2929
|
3030
LL | trait Uwu {
@@ -37,7 +37,7 @@ LL | impl Uwu for () {
3737
LL | fn baz<const N: i32>() {}
3838
| ^^^^^^^^^^^^ found const parameter of type `i32`
3939

40-
error[E0053]: method `bbbb` has an incompatible generic parameter for trait `Aaaaaa`
40+
error[E0053]: associated function `bbbb` has an incompatible generic parameter for trait `Aaaaaa`
4141
--> $DIR/mismatched_ty_const_in_trait_impl.rs:29:13
4242
|
4343
LL | trait Aaaaaa {
@@ -50,7 +50,7 @@ LL | impl Aaaaaa for () {
5050
LL | fn bbbb<T, const N: u32>() {}
5151
| ^ found type parameter
5252

53-
error[E0053]: method `abcd` has an incompatible generic parameter for trait `Names`
53+
error[E0053]: associated function `abcd` has an incompatible generic parameter for trait `Names`
5454
--> $DIR/mismatched_ty_const_in_trait_impl.rs:37:13
5555
|
5656
LL | trait Names {

tests/ui/delegation/not-supported.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod generics {
5353
//~| ERROR method `foo2` has 0 type parameters but its trait declaration has 1 type parameter
5454
reuse <F as Trait>::foo3;
5555
//~^ ERROR early bound generics are not supported for associated delegation items
56-
//~| ERROR lifetime parameters or bounds on method `foo3` do not match the trait declaration
56+
//~| ERROR lifetime parameters or bounds on associated function `foo3` do not match the trait declaration
5757
}
5858

5959
struct GenericS<T>(T);

tests/ui/delegation/not-supported.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ LL | fn foo3<'a: 'a>(_: &'a u32) {}
8484
LL | reuse <F as Trait>::foo3;
8585
| ^^^^
8686

87-
error[E0195]: lifetime parameters or bounds on method `foo3` do not match the trait declaration
87+
error[E0195]: lifetime parameters or bounds on associated function `foo3` do not match the trait declaration
8888
--> $DIR/not-supported.rs:54:29
8989
|
9090
LL | fn foo3<'a: 'a>(_: &'a u32) {}
91-
| -------- lifetimes in impl do not match this method in trait
91+
| -------- lifetimes in impl do not match this associated function in trait
9292
...
9393
LL | reuse <F as Trait>::foo3;
94-
| ^^^^ lifetimes do not match method in trait
94+
| ^^^^ lifetimes do not match associated function in trait
9595

9696
error: delegation to a function with effect parameter is not supported yet
9797
--> $DIR/not-supported.rs:112:18

tests/ui/error-codes/E0049.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `foo` has 0 type parameters but its trait declaration has 1 type parameter
1+
error[E0049]: associated function `foo` has 0 type parameters but its trait declaration has 1 type parameter
22
--> $DIR/E0049.rs:8:11
33
|
44
LL | fn foo<T: Default>(x: T) -> Self;
@@ -7,7 +7,7 @@ LL | fn foo<T: Default>(x: T) -> Self;
77
LL | fn foo(x: bool) -> Self { Bar }
88
| ^ found 0 type parameters
99

10-
error[E0049]: method `fuzz` has 0 type parameters but its trait declaration has 2 type parameters
10+
error[E0049]: associated function `fuzz` has 0 type parameters but its trait declaration has 2 type parameters
1111
--> $DIR/E0049.rs:18:12
1212
|
1313
LL | fn fuzz<A: Default, B>(x: A, y: B) -> Self;

tests/ui/error-codes/E0195.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
trait Trait {
22
fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
3-
//~^ NOTE lifetimes in impl do not match this method in trait
3+
//~^ NOTE lifetimes in impl do not match this associated function in trait
44
}
55

66
struct Foo;
77

88
impl Trait for Foo {
99
fn bar<'a,'b>(x: &'a str, y: &'b str) { //~ ERROR E0195
10-
//~^ NOTE lifetimes do not match method in trait
10+
//~^ NOTE lifetimes do not match associated function in trait
1111
}
1212
}
1313

tests/ui/error-codes/E0195.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0195]: lifetime parameters or bounds on method `bar` do not match the trait declaration
1+
error[E0195]: lifetime parameters or bounds on associated function `bar` do not match the trait declaration
22
--> $DIR/E0195.rs:9:11
33
|
44
LL | fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
5-
| ---------- lifetimes in impl do not match this method in trait
5+
| ---------- lifetimes in impl do not match this associated function in trait
66
...
77
LL | fn bar<'a,'b>(x: &'a str, y: &'b str) {
8-
| ^^^^^^^ lifetimes do not match method in trait
8+
| ^^^^^^^ lifetimes do not match associated function in trait
99

1010
error: aborting due to 1 previous error
1111

tests/ui/generic-associated-types/gat-trait-path-missing-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ impl<T> X for T { //~ ERROR: not all trait items implemented
88
fn foo<'a, T1: X<Y = T1>>(t : T1) -> T1::Y<'a> {
99
//~^ ERROR missing generics for associated type
1010
//~^^ ERROR missing generics for associated type
11-
//~| ERROR method `foo` has 1 type parameter but its trait declaration has 0 type parameters
11+
//~| ERROR associated function `foo` has 1 type parameter but its trait declaration has 0 type parameters
1212
t
1313
}
1414
}

tests/ui/generic-associated-types/gat-trait-path-missing-lifetime.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `foo` has 1 type parameter but its trait declaration has 0 type parameters
1+
error[E0049]: associated function `foo` has 1 type parameter but its trait declaration has 0 type parameters
22
--> $DIR/gat-trait-path-missing-lifetime.rs:8:10
33
|
44
LL | fn foo<'a>(t : Self::Y<'a>) -> Self::Y<'a> { t }

tests/ui/impl-trait/in-trait/trait-more-generics-than-impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait Foo {
66

77
impl Foo for S {
88
fn bar() -> impl Sized {}
9-
//~^ ERROR method `bar` has 0 type parameters but its trait declaration has 1 type parameter
9+
//~^ ERROR associated function `bar` has 0 type parameters but its trait declaration has 1 type parameter
1010
}
1111

1212
fn main() {

tests/ui/impl-trait/in-trait/trait-more-generics-than-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `bar` has 0 type parameters but its trait declaration has 1 type parameter
1+
error[E0049]: associated function `bar` has 0 type parameters but its trait declaration has 1 type parameter
22
--> $DIR/trait-more-generics-than-impl.rs:8:11
33
|
44
LL | fn bar<T>() -> impl Sized;

tests/ui/rfcs/rfc-2632-const-trait-impl/const-drop.precise.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: `~const` can only be applied to `#[const_trait]` traits
4040
LL | const fn a<T: ~const Destruct>(_: T) {}
4141
| ^^^^^^^^
4242

43-
error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters
43+
error[E0049]: associated function `foo` has 1 const parameter but its trait declaration has 0 const parameters
4444
--> $DIR/const-drop.rs:54:5
4545
|
4646
LL | #[const_trait]
@@ -49,7 +49,7 @@ LL | pub trait SomeTrait {
4949
LL | fn foo();
5050
| - expected 0 const parameters
5151

52-
error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters
52+
error[E0049]: associated function `foo` has 1 const parameter but its trait declaration has 0 const parameters
5353
--> $DIR/const-drop.rs:54:5
5454
|
5555
LL | #[const_trait]

tests/ui/rfcs/rfc-2632-const-trait-impl/const-drop.stock.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: `~const` can only be applied to `#[const_trait]` traits
4040
LL | const fn a<T: ~const Destruct>(_: T) {}
4141
| ^^^^^^^^
4242

43-
error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters
43+
error[E0049]: associated function `foo` has 1 const parameter but its trait declaration has 0 const parameters
4444
--> $DIR/const-drop.rs:54:5
4545
|
4646
LL | #[const_trait]
@@ -49,7 +49,7 @@ LL | pub trait SomeTrait {
4949
LL | fn foo();
5050
| - expected 0 const parameters
5151

52-
error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters
52+
error[E0049]: associated function `foo` has 1 const parameter but its trait declaration has 0 const parameters
5353
--> $DIR/const-drop.rs:54:5
5454
|
5555
LL | #[const_trait]

tests/ui/rfcs/rfc-2632-const-trait-impl/specialization/const-default-bound-non-const-specialized-bound.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `bar` has 1 const parameter but its trait declaration has 0 const parameters
1+
error[E0049]: associated function `bar` has 1 const parameter but its trait declaration has 0 const parameters
22
--> $DIR/const-default-bound-non-const-specialized-bound.rs:16:1
33
|
44
LL | #[const_trait]
@@ -16,7 +16,7 @@ LL | | T: Foo, //FIXME ~ ERROR missing `~const` qualifier
1616
LL | | T: Specialize,
1717
| |__________________^
1818

19-
error[E0049]: method `baz` has 1 const parameter but its trait declaration has 0 const parameters
19+
error[E0049]: associated function `baz` has 1 const parameter but its trait declaration has 0 const parameters
2020
--> $DIR/const-default-bound-non-const-specialized-bound.rs:36:1
2121
|
2222
LL | #[const_trait]
@@ -25,7 +25,7 @@ LL | trait Baz {
2525
LL | fn baz();
2626
| - expected 0 const parameters
2727

28-
error[E0049]: method `baz` has 1 const parameter but its trait declaration has 0 const parameters
28+
error[E0049]: associated function `baz` has 1 const parameter but its trait declaration has 0 const parameters
2929
--> $DIR/const-default-bound-non-const-specialized-bound.rs:36:1
3030
|
3131
LL | #[const_trait]

tests/ui/rfcs/rfc-2632-const-trait-impl/specialization/const-default-const-specialized.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `value` has 1 const parameter but its trait declaration has 0 const parameters
1+
error[E0049]: associated function `value` has 1 const parameter but its trait declaration has 0 const parameters
22
--> $DIR/const-default-const-specialized.rs:10:1
33
|
44
LL | #[const_trait]
@@ -7,7 +7,7 @@ LL | trait Value {
77
LL | fn value() -> u32;
88
| - expected 0 const parameters
99

10-
error[E0049]: method `value` has 1 const parameter but its trait declaration has 0 const parameters
10+
error[E0049]: associated function `value` has 1 const parameter but its trait declaration has 0 const parameters
1111
--> $DIR/const-default-const-specialized.rs:10:1
1212
|
1313
LL | #[const_trait]

tests/ui/rfcs/rfc-2632-const-trait-impl/specialization/default-keyword.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0049]: method `foo` has 1 const parameter but its trait declaration has 0 const parameters
1+
error[E0049]: associated function `foo` has 1 const parameter but its trait declaration has 0 const parameters
22
--> $DIR/default-keyword.rs:7:1
33
|
44
LL | #[const_trait]

0 commit comments

Comments
 (0)