@@ -46,12 +46,15 @@ use rustc_ast::*;
46
46
use rustc_errors:: ErrorGuaranteed ;
47
47
use rustc_hir:: def_id:: DefId ;
48
48
use rustc_middle:: span_bug;
49
- use rustc_middle:: ty:: { Asyncness , ResolverAstLowering } ;
49
+ use rustc_middle:: ty:: Asyncness ;
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
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
+ } ;
55
58
56
59
pub ( crate ) struct DelegationResults < ' hir > {
57
60
pub body_id : hir:: BodyId ,
@@ -60,22 +63,7 @@ pub(crate) struct DelegationResults<'hir> {
60
63
pub generics : & ' hir hir:: Generics < ' hir > ,
61
64
}
62
65
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 > {
79
67
fn is_method ( & self , def_id : DefId , span : Span ) -> bool {
80
68
match self . tcx . def_kind ( def_id) {
81
69
DefKind :: Fn => false ,
@@ -101,10 +89,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
101
89
let sig_id = self . get_delegation_sig_id ( item_id, delegation. id , span, is_in_trait_impl) ;
102
90
match sig_id {
103
91
Ok ( sig_id) => {
92
+ let is_method = self . is_method ( sig_id, span) ;
104
93
let ( param_count, c_variadic) = self . param_count ( sig_id) ;
105
94
let decl = self . lower_delegation_decl ( sig_id, param_count, c_variadic, span) ;
106
95
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) ;
108
97
let ident = self . lower_ident ( delegation. ident ) ;
109
98
let generics = self . lower_delegation_generics ( span) ;
110
99
DelegationResults { body_id, sig, ident, generics }
@@ -125,8 +114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
125
114
}
126
115
127
116
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 ( ) ) ;
130
118
def_id. ok_or_else ( || {
131
119
self . tcx . dcx ( ) . span_delayed_bug (
132
120
span,
@@ -234,10 +222,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234
222
hir:: FnSig { decl, header, span }
235
223
}
236
224
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 ) {
238
231
let pat_node_id = self . next_node_id ( ) ;
239
232
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) ;
241
240
let pat = self . arena . alloc ( hir:: Pat {
242
241
hir_id : pat_id,
243
242
kind : hir:: PatKind :: Binding ( hir:: BindingMode :: NONE , pat_id, ident, None ) ,
@@ -248,9 +247,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248
247
( hir:: Param { hir_id : self . next_id ( ) , pat, ty_span : span, span } , pat_node_id)
249
248
}
250
249
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
+ } ;
252
263
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 ) ,
254
265
hir_id : self . next_id ( ) ,
255
266
res : Res :: Local ( param_id) ,
256
267
args : None ,
@@ -264,6 +275,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264
275
fn lower_delegation_body (
265
276
& mut self ,
266
277
delegation : & Delegation ,
278
+ is_method : bool ,
267
279
param_count : usize ,
268
280
span : Span ,
269
281
) -> BodyId {
@@ -274,14 +286,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
274
286
let mut args: Vec < hir:: Expr < ' _ > > = Vec :: with_capacity ( param_count) ;
275
287
276
288
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) ;
278
290
parameters. push ( param) ;
279
291
280
292
let arg = if let Some ( block) = block
281
293
&& idx == 0
282
294
{
283
295
let mut self_resolver = SelfResolver {
284
- resolver : this. resolver ,
296
+ ctxt : this,
285
297
path_id : delegation. id ,
286
298
self_param_id : pat_node_id,
287
299
} ;
@@ -290,7 +302,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290
302
this. ident_and_label_to_local_id . insert ( pat_node_id, param. pat . hir_id . local_id ) ;
291
303
this. lower_target_expr ( & block)
292
304
} else {
293
- this. generate_arg ( idx, param. pat . hir_id , span)
305
+ this. generate_arg ( is_method , idx, param. pat . hir_id , span)
294
306
} ;
295
307
args. push ( arg) ;
296
308
}
@@ -427,25 +439,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
427
439
}
428
440
}
429
441
430
- struct SelfResolver < ' a > {
431
- resolver : & ' a mut ResolverAstLowering ,
442
+ struct SelfResolver < ' r , ' hir > {
443
+ ctxt : & ' r mut LoweringContext < ' hir > ,
432
444
path_id : NodeId ,
433
445
self_param_id : NodeId ,
434
446
}
435
447
436
- impl < ' a > SelfResolver < ' a > {
448
+ impl SelfResolver < ' _ , ' _ > {
437
449
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)
439
451
&& let Some ( Res :: Local ( sig_id) ) = res. full_res ( )
440
452
&& sig_id == self . path_id
441
453
{
442
454
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) ;
444
456
}
445
457
}
446
458
}
447
459
448
- impl < ' ast , ' a > Visitor < ' ast > for SelfResolver < ' a > {
460
+ impl < ' ast > Visitor < ' ast > for SelfResolver < ' _ , ' _ > {
449
461
fn visit_id ( & mut self , id : NodeId ) {
450
462
self . try_replace_id ( id) ;
451
463
}
0 commit comments