@@ -66,14 +66,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
66
66
let Ok ( sig_id) = sig_id else {
67
67
return false ;
68
68
} ;
69
- if let Some ( local_sig_id) = sig_id. as_local ( ) {
69
+ self . has_self ( sig_id, span)
70
+ }
71
+
72
+ fn has_self ( & self , def_id : DefId , span : Span ) -> bool {
73
+ if let Some ( local_sig_id) = def_id. as_local ( ) {
70
74
// The value may be missing due to recursive delegation.
71
75
// Error will be emmited later during HIR ty lowering.
72
76
self . resolver . delegation_fn_sigs . get ( & local_sig_id) . map_or ( false , |sig| sig. has_self )
73
77
} else {
74
- match self . tcx . def_kind ( sig_id ) {
78
+ match self . tcx . def_kind ( def_id ) {
75
79
DefKind :: Fn => false ,
76
- DefKind :: AssocFn => self . tcx . associated_item ( sig_id ) . fn_has_self_parameter ,
80
+ DefKind :: AssocFn => self . tcx . associated_item ( def_id ) . fn_has_self_parameter ,
77
81
_ => span_bug ! ( span, "unexpected DefKind for delegation item" ) ,
78
82
}
79
83
}
@@ -107,12 +111,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
107
111
span : Span ,
108
112
) -> Result < DefId , ErrorGuaranteed > {
109
113
let sig_id = if self . is_in_trait_impl { item_id } else { path_id } ;
110
- let sig_id =
111
- self . resolver . get_partial_res ( sig_id) . and_then ( |r| r. expect_full_res ( ) . opt_def_id ( ) ) ;
112
- sig_id. ok_or_else ( || {
113
- self . tcx
114
- . dcx ( )
115
- . span_delayed_bug ( span, "LoweringContext: couldn't resolve delegation item" )
114
+ self . get_resolution_id ( sig_id, span)
115
+ }
116
+
117
+ fn get_resolution_id ( & self , node_id : NodeId , span : Span ) -> Result < DefId , ErrorGuaranteed > {
118
+ let def_id =
119
+ self . resolver . get_partial_res ( node_id) . and_then ( |r| r. expect_full_res ( ) . opt_def_id ( ) ) ;
120
+ def_id. ok_or_else ( || {
121
+ self . tcx . dcx ( ) . span_delayed_bug (
122
+ span,
123
+ format ! ( "LoweringContext: couldn't resolve node {:?} in delegation item" , node_id) ,
124
+ )
116
125
} )
117
126
}
118
127
@@ -122,7 +131,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
122
131
predicates : & [ ] ,
123
132
has_where_clause_predicates : false ,
124
133
where_clause_span : span,
125
- span : span ,
134
+ span,
126
135
} )
127
136
}
128
137
@@ -222,12 +231,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
222
231
} ) ) ;
223
232
224
233
let path = self . arena . alloc ( hir:: Path { span, res : Res :: Local ( param_id) , segments } ) ;
225
-
226
- hir:: Expr {
227
- hir_id : self . next_id ( ) ,
228
- kind : hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) ,
229
- span,
230
- }
234
+ self . mk_expr ( hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) , span)
231
235
}
232
236
233
237
fn lower_delegation_body (
@@ -236,19 +240,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
236
240
param_count : usize ,
237
241
span : Span ,
238
242
) -> BodyId {
239
- let path = self . lower_qpath (
240
- delegation. id ,
241
- & delegation. qself ,
242
- & delegation. path ,
243
- ParamMode :: Optional ,
244
- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Path ) ,
245
- None ,
246
- ) ;
247
243
let block = delegation. body . as_deref ( ) ;
248
244
249
245
self . lower_body ( |this| {
250
- let mut parameters: Vec < hir:: Param < ' _ > > = Vec :: new ( ) ;
251
- let mut args: Vec < hir:: Expr < ' hir > > = Vec :: new ( ) ;
246
+ let mut parameters: Vec < hir:: Param < ' _ > > = Vec :: with_capacity ( param_count ) ;
247
+ let mut args: Vec < hir:: Expr < ' _ > > = Vec :: with_capacity ( param_count ) ;
252
248
253
249
for idx in 0 ..param_count {
254
250
let ( param, pat_node_id) = this. generate_param ( span) ;
@@ -264,55 +260,49 @@ impl<'hir> LoweringContext<'_, 'hir> {
264
260
} ;
265
261
self_resolver. visit_block ( block) ;
266
262
let block = this. lower_block ( block, false ) ;
267
- hir:: Expr {
268
- hir_id : this. next_id ( ) ,
269
- kind : hir:: ExprKind :: Block ( block, None ) ,
270
- span : block. span ,
271
- }
263
+ this. mk_expr ( hir:: ExprKind :: Block ( block, None ) , block. span )
272
264
} else {
273
265
let pat_hir_id = this. lower_node_id ( pat_node_id) ;
274
266
this. generate_arg ( pat_hir_id, span)
275
267
} ;
276
268
args. push ( arg) ;
277
269
}
278
270
279
- let args = self . arena . alloc_from_iter ( args) ;
280
- let final_expr = this. generate_call ( path, args) ;
271
+ let final_expr = this. finalize_body_lowering ( delegation, args, span) ;
281
272
( this. arena . alloc_from_iter ( parameters) , final_expr)
282
273
} )
283
274
}
284
275
285
- fn generate_call (
276
+ // Generates fully qualified call for the resulting body.
277
+ fn finalize_body_lowering (
286
278
& mut self ,
287
- path : hir:: QPath < ' hir > ,
288
- args : & ' hir [ hir:: Expr < ' hir > ] ,
279
+ delegation : & Delegation ,
280
+ args : Vec < hir:: Expr < ' hir > > ,
281
+ span : Span ,
289
282
) -> hir:: Expr < ' hir > {
290
- let callee = self . arena . alloc ( hir:: Expr {
291
- hir_id : self . next_id ( ) ,
292
- kind : hir:: ExprKind :: Path ( path) ,
293
- span : path. span ( ) ,
294
- } ) ;
283
+ let path = self . lower_qpath (
284
+ delegation. id ,
285
+ & delegation. qself ,
286
+ & delegation. path ,
287
+ ParamMode :: Optional ,
288
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Path ) ,
289
+ None ,
290
+ ) ;
295
291
296
- let expr = self . arena . alloc ( hir:: Expr {
297
- hir_id : self . next_id ( ) ,
298
- kind : hir:: ExprKind :: Call ( callee, args) ,
299
- span : path. span ( ) ,
300
- } ) ;
292
+ let args = self . arena . alloc_from_iter ( args) ;
293
+ let path_expr = self . arena . alloc ( self . mk_expr ( hir:: ExprKind :: Path ( path) , span) ) ;
294
+ let call = self . arena . alloc ( self . mk_expr ( hir:: ExprKind :: Call ( path_expr, args) , span) ) ;
301
295
302
296
let block = self . arena . alloc ( hir:: Block {
303
297
stmts : & [ ] ,
304
- expr : Some ( expr ) ,
298
+ expr : Some ( call ) ,
305
299
hir_id : self . next_id ( ) ,
306
300
rules : hir:: BlockCheckMode :: DefaultBlock ,
307
- span : path . span ( ) ,
301
+ span,
308
302
targeted_by_break : false ,
309
303
} ) ;
310
304
311
- hir:: Expr {
312
- hir_id : self . next_id ( ) ,
313
- kind : hir:: ExprKind :: Block ( block, None ) ,
314
- span : path. span ( ) ,
315
- }
305
+ self . mk_expr ( hir:: ExprKind :: Block ( block, None ) , span)
316
306
}
317
307
318
308
fn generate_delegation_error (
@@ -333,11 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
333
323
let header = self . generate_header_error ( ) ;
334
324
let sig = hir:: FnSig { decl, header, span } ;
335
325
336
- let body_id = self . lower_body ( |this| {
337
- let expr =
338
- hir:: Expr { hir_id : this. next_id ( ) , kind : hir:: ExprKind :: Err ( err) , span : span } ;
339
- ( & [ ] , expr)
340
- } ) ;
326
+ let body_id = self . lower_body ( |this| ( & [ ] , this. mk_expr ( hir:: ExprKind :: Err ( err) , span) ) ) ;
341
327
DelegationResults { generics, body_id, sig }
342
328
}
343
329
@@ -349,6 +335,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
349
335
abi : abi:: Abi :: Rust ,
350
336
}
351
337
}
338
+
339
+ #[ inline]
340
+ fn mk_expr ( & mut self , kind : hir:: ExprKind < ' hir > , span : Span ) -> hir:: Expr < ' hir > {
341
+ hir:: Expr { hir_id : self . next_id ( ) , kind, span }
342
+ }
352
343
}
353
344
354
345
struct SelfResolver < ' a > {
0 commit comments