@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
4747use rustc_hir:: def_id:: DefId ;
4848use rustc_middle:: span_bug;
4949use rustc_middle:: ty:: { Asyncness , ResolverAstLowering } ;
50+ use rustc_span:: symbol:: kw;
5051use rustc_span:: { Ident , Span , Symbol } ;
5152use { rustc_ast as ast, rustc_hir as hir} ;
5253
@@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
6162}
6263
6364impl < ' 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-
7965 fn is_method ( & self , def_id : DefId , span : Span ) -> bool {
8066 match self . tcx . def_kind ( def_id) {
8167 DefKind :: Fn => false ,
@@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10187 let sig_id = self . get_delegation_sig_id ( item_id, delegation. id , span, is_in_trait_impl) ;
10288 match sig_id {
10389 Ok ( sig_id) => {
90+ let is_method = self . is_method ( sig_id, span) ;
10491 let ( param_count, c_variadic) = self . param_count ( sig_id) ;
10592 let decl = self . lower_delegation_decl ( sig_id, param_count, c_variadic, span) ;
10693 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) ;
10895 let ident = self . lower_ident ( delegation. ident ) ;
10996 let generics = self . lower_delegation_generics ( span) ;
11097 DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234221 hir:: FnSig { decl, header, span }
235222 }
236223
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 ) {
238230 let pat_node_id = self . next_node_id ( ) ;
239231 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) ;
241239 let pat = self . arena . alloc ( hir:: Pat {
242240 hir_id : pat_id,
243241 kind : hir:: PatKind :: Binding ( hir:: BindingMode :: NONE , pat_id, ident, None ) ,
@@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248246 ( hir:: Param { hir_id : self . next_id ( ) , pat, ty_span : span, span } , pat_node_id)
249247 }
250248
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+ } ;
252262 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 ) ,
254264 hir_id : self . next_id ( ) ,
255265 res : Res :: Local ( param_id) ,
256266 args : None ,
@@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264274 fn lower_delegation_body (
265275 & mut self ,
266276 delegation : & Delegation ,
277+ is_method : bool ,
267278 param_count : usize ,
268279 span : Span ,
269280 ) -> BodyId {
@@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274285 let mut args: Vec < hir:: Expr < ' _ > > = Vec :: with_capacity ( param_count) ;
275286
276287 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) ;
278289 parameters. push ( param) ;
279290
280291 let arg = if let Some ( block) = block
@@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290301 this. ident_and_label_to_local_id . insert ( pat_node_id, param. pat . hir_id . local_id ) ;
291302 this. lower_target_expr ( & block)
292303 } else {
293- this. generate_arg ( idx, param. pat . hir_id , span)
304+ this. generate_arg ( is_method , idx, param. pat . hir_id , span)
294305 } ;
295306 args. push ( arg) ;
296307 }
0 commit comments