Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 860c8cd

Browse files
committed
Differentiate between methods and associated functions
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.
1 parent 899eb03 commit 860c8cd

27 files changed

+79
-72
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 11 deletions
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

Lines changed: 12 additions & 6 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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;

0 commit comments

Comments
 (0)