@@ -46,12 +46,15 @@ use rustc_ast::*;
4646use rustc_errors:: ErrorGuaranteed ;
4747use rustc_hir:: def_id:: DefId ;
4848use rustc_middle:: span_bug;
49- use rustc_middle:: ty:: { Asyncness , ResolverAstLowering } ;
49+ use rustc_middle:: ty:: Asyncness ;
50+ use rustc_span:: symbol:: kw;
5051use rustc_span:: { Ident , Span , Symbol } ;
5152use { rustc_ast as ast, rustc_hir as hir} ;
5253
53- use super :: { GenericArgsMode , ImplTraitContext , LoweringContext , ParamMode } ;
54- use crate :: { AllowReturnTypeNotation , ImplTraitPosition , ResolverAstLoweringExt } ;
54+ use super :: {
55+ AllowReturnTypeNotation , GenericArgsMode , ImplTraitContext , ImplTraitPosition , LoweringContext ,
56+ ParamMode ,
57+ } ;
5558
5659pub ( crate ) struct DelegationResults < ' hir > {
5760 pub body_id : hir:: BodyId ,
@@ -60,22 +63,7 @@ pub(crate) struct DelegationResults<'hir> {
6063 pub generics : & ' hir hir:: Generics < ' hir > ,
6164}
6265
63- 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-
66+ impl < ' hir > LoweringContext < ' hir > {
7967 fn is_method ( & self , def_id : DefId , span : Span ) -> bool {
8068 match self . tcx . def_kind ( def_id) {
8169 DefKind :: Fn => false ,
@@ -101,10 +89,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10189 let sig_id = self . get_delegation_sig_id ( item_id, delegation. id , span, is_in_trait_impl) ;
10290 match sig_id {
10391 Ok ( sig_id) => {
92+ let is_method = self . is_method ( sig_id, span) ;
10493 let ( param_count, c_variadic) = self . param_count ( sig_id) ;
10594 let decl = self . lower_delegation_decl ( sig_id, param_count, c_variadic, span) ;
10695 let sig = self . lower_delegation_sig ( sig_id, decl, span) ;
107- let body_id = self . lower_delegation_body ( delegation, param_count, span) ;
96+ let body_id = self . lower_delegation_body ( delegation, is_method , param_count, span) ;
10897 let ident = self . lower_ident ( delegation. ident ) ;
10998 let generics = self . lower_delegation_generics ( span) ;
11099 DelegationResults { body_id, sig, ident, generics }
@@ -125,8 +114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
125114 }
126115
127116 fn get_resolution_id ( & self , node_id : NodeId , span : Span ) -> Result < DefId , ErrorGuaranteed > {
128- let def_id =
129- self . resolver . get_partial_res ( node_id) . and_then ( |r| r. expect_full_res ( ) . opt_def_id ( ) ) ;
117+ let def_id = self . get_partial_res ( node_id) . and_then ( |r| r. expect_full_res ( ) . opt_def_id ( ) ) ;
130118 def_id. ok_or_else ( || {
131119 self . tcx . dcx ( ) . span_delayed_bug (
132120 span,
@@ -234,10 +222,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234222 hir:: FnSig { decl, header, span }
235223 }
236224
237- fn generate_param ( & mut self , idx : usize , span : Span ) -> ( hir:: Param < ' hir > , NodeId ) {
225+ fn generate_param (
226+ & mut self ,
227+ is_method : bool ,
228+ idx : usize ,
229+ span : Span ,
230+ ) -> ( hir:: Param < ' hir > , NodeId ) {
238231 let pat_node_id = self . next_node_id ( ) ;
239232 let pat_id = self . lower_node_id ( pat_node_id) ;
240- let ident = Ident :: with_dummy_span ( Symbol :: intern ( & format ! ( "arg{idx}" ) ) ) ;
233+ // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
234+ let name = if is_method && idx == 0 {
235+ kw:: SelfLower
236+ } else {
237+ Symbol :: intern ( & format ! ( "arg{idx}" ) )
238+ } ;
239+ let ident = Ident :: with_dummy_span ( name) ;
241240 let pat = self . arena . alloc ( hir:: Pat {
242241 hir_id : pat_id,
243242 kind : hir:: PatKind :: Binding ( hir:: BindingMode :: NONE , pat_id, ident, None ) ,
@@ -248,9 +247,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248247 ( hir:: Param { hir_id : self . next_id ( ) , pat, ty_span : span, span } , pat_node_id)
249248 }
250249
251- fn generate_arg ( & mut self , idx : usize , param_id : HirId , span : Span ) -> hir:: Expr < ' hir > {
250+ fn generate_arg (
251+ & mut self ,
252+ is_method : bool ,
253+ idx : usize ,
254+ param_id : HirId ,
255+ span : Span ,
256+ ) -> hir:: Expr < ' hir > {
257+ // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
258+ let name = if is_method && idx == 0 {
259+ kw:: SelfLower
260+ } else {
261+ Symbol :: intern ( & format ! ( "arg{idx}" ) )
262+ } ;
252263 let segments = self . arena . alloc_from_iter ( iter:: once ( hir:: PathSegment {
253- ident : Ident :: with_dummy_span ( Symbol :: intern ( & format ! ( "arg{idx}" ) ) ) ,
264+ ident : Ident :: with_dummy_span ( name ) ,
254265 hir_id : self . next_id ( ) ,
255266 res : Res :: Local ( param_id) ,
256267 args : None ,
@@ -264,6 +275,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264275 fn lower_delegation_body (
265276 & mut self ,
266277 delegation : & Delegation ,
278+ is_method : bool ,
267279 param_count : usize ,
268280 span : Span ,
269281 ) -> BodyId {
@@ -274,14 +286,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
274286 let mut args: Vec < hir:: Expr < ' _ > > = Vec :: with_capacity ( param_count) ;
275287
276288 for idx in 0 ..param_count {
277- let ( param, pat_node_id) = this. generate_param ( idx, span) ;
289+ let ( param, pat_node_id) = this. generate_param ( is_method , idx, span) ;
278290 parameters. push ( param) ;
279291
280292 let arg = if let Some ( block) = block
281293 && idx == 0
282294 {
283295 let mut self_resolver = SelfResolver {
284- resolver : this. resolver ,
296+ ctxt : this,
285297 path_id : delegation. id ,
286298 self_param_id : pat_node_id,
287299 } ;
@@ -290,7 +302,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290302 this. ident_and_label_to_local_id . insert ( pat_node_id, param. pat . hir_id . local_id ) ;
291303 this. lower_target_expr ( & block)
292304 } else {
293- this. generate_arg ( idx, param. pat . hir_id , span)
305+ this. generate_arg ( is_method , idx, param. pat . hir_id , span)
294306 } ;
295307 args. push ( arg) ;
296308 }
@@ -427,25 +439,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
427439 }
428440}
429441
430- struct SelfResolver < ' a > {
431- resolver : & ' a mut ResolverAstLowering ,
442+ struct SelfResolver < ' r , ' hir > {
443+ ctxt : & ' r mut LoweringContext < ' hir > ,
432444 path_id : NodeId ,
433445 self_param_id : NodeId ,
434446}
435447
436- impl < ' a > SelfResolver < ' a > {
448+ impl SelfResolver < ' _ , ' _ > {
437449 fn try_replace_id ( & mut self , id : NodeId ) {
438- if let Some ( res) = self . resolver . partial_res_map . get ( & id)
450+ if let Some ( res) = self . ctxt . get_partial_res ( id)
439451 && let Some ( Res :: Local ( sig_id) ) = res. full_res ( )
440452 && sig_id == self . path_id
441453 {
442454 let new_res = PartialRes :: new ( Res :: Local ( self . self_param_id ) ) ;
443- self . resolver . partial_res_map . insert ( id, new_res) ;
455+ self . ctxt . partial_res_overrides . insert ( id, new_res) ;
444456 }
445457 }
446458}
447459
448- impl < ' ast , ' a > Visitor < ' ast > for SelfResolver < ' a > {
460+ impl < ' ast > Visitor < ' ast > for SelfResolver < ' _ , ' _ > {
449461 fn visit_id ( & mut self , id : NodeId ) {
450462 self . try_replace_id ( id) ;
451463 }
0 commit comments