@@ -66,14 +66,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
6666 let Ok(sig_id) = sig_id else {
6767 return false;
6868 };
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() {
7074 // The value may be missing due to recursive delegation.
7175 // Error will be emmited later during HIR ty lowering.
7276 self.resolver.delegation_fn_sigs.get(&local_sig_id).map_or(false, |sig| sig.has_self)
7377 } else {
74- match self.tcx.def_kind(sig_id ) {
78+ match self.tcx.def_kind(def_id ) {
7579 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,
7781 _ => span_bug!(span, "unexpected DefKind for delegation item"),
7882 }
7983 }
@@ -107,12 +111,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
107111 span: Span,
108112 ) -> Result<DefId, ErrorGuaranteed> {
109113 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+ )
116125 })
117126 }
118127
@@ -122,7 +131,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
122131 predicates: &[],
123132 has_where_clause_predicates: false,
124133 where_clause_span: span,
125- span: span ,
134+ span,
126135 })
127136 }
128137
@@ -222,12 +231,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
222231 }));
223232
224233 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)
231235 }
232236
233237 fn lower_delegation_body(
@@ -236,19 +240,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
236240 param_count: usize,
237241 span: Span,
238242 ) -> 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- );
247243 let block = delegation.body.as_deref();
248244
249245 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 );
252248
253249 for idx in 0..param_count {
254250 let (param, pat_node_id) = this.generate_param(span);
@@ -264,55 +260,49 @@ impl<'hir> LoweringContext<'_, 'hir> {
264260 };
265261 self_resolver.visit_block(block);
266262 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)
272264 } else {
273265 let pat_hir_id = this.lower_node_id(pat_node_id);
274266 this.generate_arg(pat_hir_id, span)
275267 };
276268 args.push(arg);
277269 }
278270
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);
281272 (this.arena.alloc_from_iter(parameters), final_expr)
282273 })
283274 }
284275
285- fn generate_call(
276+ // Generates fully qualified call for the resulting body.
277+ fn finalize_body_lowering(
286278 &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,
289282 ) -> 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+ );
295291
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));
301295
302296 let block = self.arena.alloc(hir::Block {
303297 stmts: &[],
304- expr: Some(expr ),
298+ expr: Some(call ),
305299 hir_id: self.next_id(),
306300 rules: hir::BlockCheckMode::DefaultBlock,
307- span: path.span() ,
301+ span,
308302 targeted_by_break: false,
309303 });
310304
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)
316306 }
317307
318308 fn generate_delegation_error(
@@ -333,11 +323,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
333323 let header = self.generate_header_error();
334324 let sig = hir::FnSig { decl, header, span };
335325
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)));
341327 DelegationResults { generics, body_id, sig }
342328 }
343329
@@ -349,6 +335,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
349335 abi: abi::Abi::Rust,
350336 }
351337 }
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+ }
352343}
353344
354345struct SelfResolver<'a> {
0 commit comments