@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
47
47
use rustc_hir:: def_id:: DefId ;
48
48
use rustc_middle:: span_bug;
49
49
use rustc_middle:: ty:: { Asyncness , ResolverAstLowering } ;
50
+ use rustc_span:: symbol:: kw;
50
51
use rustc_span:: { Ident , Span , Symbol } ;
51
52
use { rustc_ast as ast, rustc_hir as hir} ;
52
53
@@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
61
62
}
62
63
63
64
impl < ' hir > LoweringContext < ' _ , ' hir > {
64
- /// Defines whether the delegatee is an associated function whose first parameter is `self`.
65
- pub ( crate ) fn delegatee_is_method (
66
- & self ,
67
- item_id : NodeId ,
68
- path_id : NodeId ,
69
- span : Span ,
70
- is_in_trait_impl : bool ,
71
- ) -> bool {
72
- let sig_id = self . get_delegation_sig_id ( item_id, path_id, span, is_in_trait_impl) ;
73
- let Ok ( sig_id) = sig_id else {
74
- return false ;
75
- } ;
76
- self . is_method ( sig_id, span)
77
- }
78
-
79
65
fn is_method ( & self , def_id : DefId , span : Span ) -> bool {
80
66
match self . tcx . def_kind ( def_id) {
81
67
DefKind :: Fn => false ,
@@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
101
87
let sig_id = self . get_delegation_sig_id ( item_id, delegation. id , span, is_in_trait_impl) ;
102
88
match sig_id {
103
89
Ok ( sig_id) => {
90
+ let is_method = self . is_method ( sig_id, span) ;
104
91
let ( param_count, c_variadic) = self . param_count ( sig_id) ;
105
92
let decl = self . lower_delegation_decl ( sig_id, param_count, c_variadic, span) ;
106
93
let sig = self . lower_delegation_sig ( sig_id, decl, span) ;
107
- let body_id = self . lower_delegation_body ( delegation, param_count, span) ;
94
+ let body_id = self . lower_delegation_body ( delegation, is_method , param_count, span) ;
108
95
let ident = self . lower_ident ( delegation. ident ) ;
109
96
let generics = self . lower_delegation_generics ( span) ;
110
97
DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234
221
hir:: FnSig { decl, header, span }
235
222
}
236
223
237
- fn generate_param ( & mut self , idx : usize , span : Span ) -> ( hir:: Param < ' hir > , NodeId ) {
224
+ fn generate_param (
225
+ & mut self ,
226
+ is_method : bool ,
227
+ idx : usize ,
228
+ span : Span ,
229
+ ) -> ( hir:: Param < ' hir > , NodeId ) {
238
230
let pat_node_id = self . next_node_id ( ) ;
239
231
let pat_id = self . lower_node_id ( pat_node_id) ;
240
- let ident = Ident :: with_dummy_span ( Symbol :: intern ( & format ! ( "arg{idx}" ) ) ) ;
232
+ // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
233
+ let name = if is_method && idx == 0 {
234
+ kw:: SelfLower
235
+ } else {
236
+ Symbol :: intern ( & format ! ( "arg{idx}" ) )
237
+ } ;
238
+ let ident = Ident :: with_dummy_span ( name) ;
241
239
let pat = self . arena . alloc ( hir:: Pat {
242
240
hir_id : pat_id,
243
241
kind : hir:: PatKind :: Binding ( hir:: BindingMode :: NONE , pat_id, ident, None ) ,
@@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248
246
( hir:: Param { hir_id : self . next_id ( ) , pat, ty_span : span, span } , pat_node_id)
249
247
}
250
248
251
- fn generate_arg ( & mut self , idx : usize , param_id : HirId , span : Span ) -> hir:: Expr < ' hir > {
249
+ fn generate_arg (
250
+ & mut self ,
251
+ is_method : bool ,
252
+ idx : usize ,
253
+ param_id : HirId ,
254
+ span : Span ,
255
+ ) -> hir:: Expr < ' hir > {
256
+ // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
257
+ let name = if is_method && idx == 0 {
258
+ kw:: SelfLower
259
+ } else {
260
+ Symbol :: intern ( & format ! ( "arg{idx}" ) )
261
+ } ;
252
262
let segments = self . arena . alloc_from_iter ( iter:: once ( hir:: PathSegment {
253
- ident : Ident :: with_dummy_span ( Symbol :: intern ( & format ! ( "arg{idx}" ) ) ) ,
263
+ ident : Ident :: with_dummy_span ( name ) ,
254
264
hir_id : self . next_id ( ) ,
255
265
res : Res :: Local ( param_id) ,
256
266
args : None ,
@@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264
274
fn lower_delegation_body (
265
275
& mut self ,
266
276
delegation : & Delegation ,
277
+ is_method : bool ,
267
278
param_count : usize ,
268
279
span : Span ,
269
280
) -> BodyId {
@@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274
285
let mut args: Vec < hir:: Expr < ' _ > > = Vec :: with_capacity ( param_count) ;
275
286
276
287
for idx in 0 ..param_count {
277
- let ( param, pat_node_id) = this. generate_param ( idx, span) ;
288
+ let ( param, pat_node_id) = this. generate_param ( is_method , idx, span) ;
278
289
parameters. push ( param) ;
279
290
280
291
let arg = if let Some ( block) = block
@@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290
301
this. ident_and_label_to_local_id . insert ( pat_node_id, param. pat . hir_id . local_id ) ;
291
302
this. lower_target_expr ( & block)
292
303
} else {
293
- this. generate_arg ( idx, param. pat . hir_id , span)
304
+ this. generate_arg ( is_method , idx, param. pat . hir_id , span)
294
305
} ;
295
306
args. push ( arg) ;
296
307
}
0 commit comments