1use std::iter;
40use std::ops::ControlFlow;
41
42use ast::visit::Visitor;
43use hir::def::{DefKind, Res};
44use hir::{BodyId, HirId};
45use rustc_abi::ExternAbi;
46use rustc_ast as ast;
47use rustc_ast::node_id::NodeMap;
48use rustc_ast::*;
49use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
50use rustc_hir::attrs::{AttributeKind, InlineAttr};
51use rustc_hir::def_id::{DefId, LocalDefId};
52use rustc_hir::{self as hir, FnDeclFlags};
53use rustc_middle::span_bug;
54use rustc_middle::ty::{Asyncness, PerOwnerResolverData, TyCtxt};
55use rustc_span::symbol::kw;
56use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
57
58use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
59use crate::diagnostics::{
60 CycleInDelegationSignatureResolution, DelegationAttemptedBlockWithDefsDeletion,
61 DelegationBlockSpecifiedWhenNoParams, UnresolvedDelegationCallee,
62};
63use crate::{
64 AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
65 index_crate,
66};
67
68mod generics;
69
70pub(crate) struct DelegationResults<'hir> {
71 pub body_id: hir::BodyId,
72 pub sig: hir::FnSig<'hir>,
73 pub ident: Ident,
74 pub generics: &'hir hir::Generics<'hir>,
75}
76
77struct AttrAdditionInfo {
78 pub equals: fn(&hir::Attribute) -> bool,
79 pub kind: AttrAdditionKind,
80}
81
82enum AttrAdditionKind {
83 Default { factory: fn(Span) -> hir::Attribute },
84 Inherit { factory: fn(Span, &hir::Attribute) -> hir::Attribute },
85}
86
87#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ParamInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "ParamInfo",
"param_count", &self.param_count, "c_variadic", &self.c_variadic,
"splatted", &&self.splatted)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ParamInfo {
#[inline]
fn clone(&self) -> ParamInfo {
let _: ::core::clone::AssertParamIsClone<usize>;
let _: ::core::clone::AssertParamIsClone<bool>;
let _: ::core::clone::AssertParamIsClone<Option<u16>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ParamInfo { }Copy, #[automatically_derived]
impl ::core::cmp::Eq for ParamInfo {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<bool>;
let _: ::core::cmp::AssertParamIsEq<Option<u16>>;
}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for ParamInfo {
#[inline]
fn eq(&self, other: &ParamInfo) -> bool {
self.c_variadic == other.c_variadic &&
self.param_count == other.param_count &&
self.splatted == other.splatted
}
}PartialEq)]
89struct ParamInfo {
90 pub param_count: usize,
92
93 pub c_variadic: bool,
95
96 pub splatted: Option<u16>,
98}
99
100const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
101
102static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
103 AttrAdditionInfo {
104 equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
hir::Attribute::Parsed(AttributeKind::MustUse { .. }) => true,
_ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),
105 kind: AttrAdditionKind::Inherit {
106 factory: |span, original_attr| {
107 let reason = match original_attr {
108 hir::Attribute::Parsed(AttributeKind::MustUse { reason, .. }) => *reason,
109 _ => None,
110 };
111
112 hir::Attribute::Parsed(AttributeKind::MustUse { span, reason })
113 },
114 },
115 },
116 AttrAdditionInfo {
117 equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
hir::Attribute::Parsed(AttributeKind::Inline(..)) => true,
_ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))),
118 kind: AttrAdditionKind::Default {
119 factory: |span| hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span)),
120 },
121 },
122];
123
124pub(crate) fn delegations_resolutions(
125 tcx: TyCtxt<'_>,
126 _: (),
127) -> FxIndexMap<LocalDefId, Result<DefId, ErrorGuaranteed>> {
128 let krate = tcx.hir_crate(());
129
130 let (resolver, ast_crate) = &*krate.delayed_resolver.borrow();
131
132 let ast_index = index_crate(resolver, ast_crate);
135
136 let mut result = FxIndexMap::<LocalDefId, Result<DefId, ErrorGuaranteed>>::default();
137
138 for &def_id in &krate.delayed_ids {
139 let delegation = ast_index[def_id].delegation().expect("processing delegations");
140 let span = delegation.last_segment_span();
141
142 if let Some(info) = tcx.resolutions(()).delegation_infos.get(&def_id) {
143 let res = info.resolution_id.map(|id| check_for_cycles(tcx, id, span).map(|_| id));
144 result.insert(def_id, res.flatten());
145 } else {
146 tcx.dcx().span_delayed_bug(
147 span,
148 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("delegation resolution record was not found for {0:?}",
def_id))
})format!("delegation resolution record was not found for {def_id:?}"),
149 );
150 }
151 }
152
153 result
154}
155
156fn check_for_cycles(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
157 let mut visited: FxHashSet<DefId> = Default::default();
158
159 loop {
160 visited.insert(def_id);
161
162 if let Some(local_id) = def_id.as_local()
166 && let Some(info) = tcx.resolutions(()).delegation_infos.get(&local_id)
167 && let Ok(id) = info.resolution_id
168 {
169 def_id = id;
170 if visited.contains(&def_id) {
171 return Err(match visited.len() {
172 1 => tcx.dcx().emit_err(UnresolvedDelegationCallee { span }),
173 _ => tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
174 });
175 }
176 } else {
177 return Ok(());
178 }
179 }
180}
181
182impl<'hir> LoweringContext<'_, 'hir> {
183 fn is_method(&self, def_id: DefId, span: Span) -> bool {
184 match self.tcx.def_kind(def_id) {
185 DefKind::Fn => false,
186 DefKind::AssocFn => self.tcx.associated_item(def_id).is_method(),
187 _ => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("unexpected DefKind for delegation item"))span_bug!(span, "unexpected DefKind for delegation item"),
188 }
189 }
190
191 pub(crate) fn lower_delegation(
192 &mut self,
193 delegation: &Delegation,
194 item_id: NodeId,
195 ) -> DelegationResults<'hir> {
196 let span = self.lower_span(delegation.last_segment_span());
197
198 let sig_id = self.tcx.delegations_resolutions(()).get(&self.owner.def_id).copied();
199
200 let Some(Ok(sig_id)) = sig_id else {
203 self.dcx().span_delayed_bug(
204 span,
205 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("LoweringContext: the delegation {0:?} is unresolved",
item_id))
})format!("LoweringContext: the delegation {:?} is unresolved", item_id),
206 );
207
208 return self.generate_delegation_error(span, delegation);
209 };
210
211 self.add_attrs_if_needed(span, sig_id);
212
213 let is_method = self.is_method(sig_id, span);
214
215 let param_info = self.param_info(sig_id);
216
217 if !self.check_block_soundness(delegation, sig_id, is_method, param_info.param_count) {
218 return self.generate_delegation_error(span, delegation);
219 }
220
221 let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);
222
223 let (body_id, call_expr_id, unused_target_expr) = self.lower_delegation_body(
224 delegation,
225 sig_id,
226 param_info.param_count,
227 &mut generics,
228 span,
229 );
230
231 let decl = self.lower_delegation_decl(
232 delegation.source,
233 sig_id,
234 param_info,
235 span,
236 &generics,
237 delegation.id,
238 call_expr_id,
239 unused_target_expr,
240 );
241
242 let sig = self.lower_delegation_sig(sig_id, decl, span);
243 let ident = self.lower_ident(delegation.ident);
244
245 let generics = self.arena.alloc(hir::Generics {
246 has_where_clause_predicates: false,
247 params: self.arena.alloc_from_iter(generics.all_params()),
248 predicates: self.arena.alloc_from_iter(generics.all_predicates()),
249 span,
250 where_clause_span: span,
251 });
252
253 DelegationResults { body_id, sig, ident, generics }
254 }
255
256 fn check_block_soundness(
257 &self,
258 delegation: &Delegation,
259 sig_id: DefId,
260 is_method: bool,
261 param_count: usize,
262 ) -> bool {
263 let Some(block) = delegation.body.as_ref() else { return true };
264 let should_generate_block = self.should_generate_block(delegation, sig_id, is_method);
265
266 if param_count == 0 && should_generate_block {
269 self.dcx().emit_err(DelegationBlockSpecifiedWhenNoParams { span: block.span });
270 return false;
271 }
272
273 struct DefinitionsFinder<'a> {
274 all_owners: &'a NodeMap<PerOwnerResolverData<'a>>,
275 nested_def_ids: &'a NodeMap<LocalDefId>,
277 }
278
279 impl<'a> ast::visit::Visitor<'a> for DefinitionsFinder<'a> {
280 type Result = ControlFlow<()>;
281
282 fn visit_id(&mut self, id: NodeId) -> Self::Result {
283 match self.all_owners.contains_key(&id) || self.nested_def_ids.contains_key(&id) {
298 true => ControlFlow::Break(()),
299 false => ControlFlow::Continue(()),
300 }
301 }
302 }
303
304 let mut collector = DefinitionsFinder {
305 all_owners: &self.resolver.owners,
306 nested_def_ids: &self.owner.node_id_to_def_id,
307 };
308
309 let contains_defs = collector.visit_block(block).is_break();
310
311 if !should_generate_block && contains_defs {
314 self.dcx().emit_err(DelegationAttemptedBlockWithDefsDeletion { span: block.span });
315 return false;
316 }
317
318 true
319 }
320
321 fn should_generate_block(
322 &self,
323 delegation: &Delegation,
324 sig_id: DefId,
325 is_method: bool,
326 ) -> bool {
327 is_method
328 || #[allow(non_exhaustive_omitted_patterns)] match self.tcx.def_kind(sig_id) {
DefKind::Fn => true,
_ => false,
}matches!(self.tcx.def_kind(sig_id), DefKind::Fn)
329 || #[allow(non_exhaustive_omitted_patterns)] match delegation.source {
DelegationSource::Single => true,
_ => false,
}matches!(delegation.source, DelegationSource::Single)
330 }
331
332 fn add_attrs_if_needed(&mut self, span: Span, sig_id: DefId) {
333 let new_attrs =
334 self.create_new_attrs(ATTRS_ADDITIONS, span, sig_id, self.attrs.get(&PARENT_ID));
335
336 if new_attrs.is_empty() {
337 return;
338 }
339
340 let new_arena_allocated_attrs = match self.attrs.get(&PARENT_ID) {
341 Some(existing_attrs) => self.arena.alloc_from_iter(
342 existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()),
343 ),
344 None => self.arena.alloc_from_iter(new_attrs.into_iter()),
345 };
346
347 self.attrs.insert(PARENT_ID, new_arena_allocated_attrs);
348 }
349
350 fn create_new_attrs(
351 &self,
352 candidate_additions: &[AttrAdditionInfo],
353 span: Span,
354 sig_id: DefId,
355 existing_attrs: Option<&&[hir::Attribute]>,
356 ) -> Vec<hir::Attribute> {
357 candidate_additions
358 .iter()
359 .filter_map(|addition_info| {
360 if let Some(existing_attrs) = existing_attrs
361 && existing_attrs
362 .iter()
363 .any(|existing_attr| (addition_info.equals)(existing_attr))
364 {
365 return None;
366 }
367
368 match addition_info.kind {
369 AttrAdditionKind::Default { factory } => Some(factory(span)),
370 AttrAdditionKind::Inherit { factory, .. } =>
371 {
372 #[allow(deprecated)]
373 self.tcx
374 .get_all_attrs(sig_id)
375 .iter()
376 .find_map(|a| (addition_info.equals)(a).then(|| factory(span, a)))
377 }
378 }
379 })
380 .collect::<Vec<_>>()
381 }
382
383 fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
384 self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
385 }
386
387 fn param_info(&self, def_id: DefId) -> ParamInfo {
389 let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();
390
391 ParamInfo {
393 param_count: sig.inputs().len() + usize::from(sig.c_variadic()),
394 c_variadic: sig.c_variadic(),
395 splatted: None,
396 }
397 }
398
399 fn lower_delegation_decl(
400 &mut self,
401 source: DelegationSource,
402 sig_id: DefId,
403 param_info: ParamInfo,
404 span: Span,
405 generics: &GenericsGenerationResults<'hir>,
406 call_path_node_id: NodeId,
407 call_expr_id: HirId,
408 unused_target_expr: bool,
409 ) -> &'hir hir::FnDecl<'hir> {
410 let ParamInfo { param_count, c_variadic, splatted } = param_info;
411
412 let decl_param_count = param_count - c_variadic as usize;
415 let inputs = self.arena.alloc_from_iter((0..decl_param_count).map(|arg| hir::Ty {
416 hir_id: self.next_id(),
417 kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
418 sig_id,
419 hir::InferDelegationSig::Input(arg),
420 )),
421 span,
422 }));
423
424 let output = self.arena.alloc(hir::Ty {
425 hir_id: self.next_id(),
426 kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
427 sig_id,
428 hir::InferDelegationSig::Output(self.arena.alloc(hir::DelegationInfo {
429 call_expr_id,
430 call_path_res: self.get_resolution_id(call_path_node_id),
431 child_args_segment_id: generics.child.args_segment_id,
432 parent_args_segment_id: generics.parent.args_segment_id,
433 self_ty_id: generics.self_ty_id,
434 propagate_self_ty: generics.propagate_self_ty,
435 group_id: {
436 let id = match source {
437 DelegationSource::Single => None,
438 DelegationSource::List(expn_id) => Some(expn_id),
439 DelegationSource::Glob => {
440 Some(self.tcx.expn_that_defined(self.owner.def_id).expect_local())
441 }
442 };
443
444 id.map(|id| (id, unused_target_expr))
445 },
446 })),
447 )),
448 span,
449 });
450
451 self.arena.alloc(hir::FnDecl {
452 inputs,
453 output: hir::FnRetTy::Return(output),
454 fn_decl_kind: FnDeclFlags::default()
455 .set_lifetime_elision_allowed(true)
456 .set_c_variadic(c_variadic)
457 .set_splatted(splatted, inputs.len())
458 .unwrap(),
459 })
460 }
461
462 fn lower_delegation_sig(
463 &mut self,
464 sig_id: DefId,
465 decl: &'hir hir::FnDecl<'hir>,
466 span: Span,
467 ) -> hir::FnSig<'hir> {
468 let sig = self.tcx.fn_sig(sig_id).skip_binder().skip_binder();
469 let asyncness = match self.tcx.asyncness(sig_id) {
470 Asyncness::Yes => hir::IsAsync::Async(span),
471 Asyncness::No => hir::IsAsync::NotAsync,
472 };
473
474 let header = hir::FnHeader {
475 safety: if self.tcx.codegen_fn_attrs(sig_id).safe_target_features {
476 hir::HeaderSafety::SafeTargetFeatures
477 } else {
478 hir::HeaderSafety::Normal(sig.safety())
479 },
480 constness: self.tcx.constness(sig_id),
481 asyncness,
482 abi: sig.abi(),
483 };
484
485 hir::FnSig { decl, header, span }
486 }
487
488 fn generate_param(
489 &mut self,
490 is_method: bool,
491 idx: usize,
492 span: Span,
493 ) -> (hir::Param<'hir>, NodeId) {
494 let pat_node_id = self.next_node_id();
495 let pat_id = self.lower_node_id(pat_node_id);
496 let name = if is_method && idx == 0 {
498 kw::SelfLower
499 } else {
500 Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("arg{0}", idx))
})format!("arg{idx}"))
501 };
502 let ident = Ident::with_dummy_span(name);
503 let pat = self.arena.alloc(hir::Pat {
504 hir_id: pat_id,
505 kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
506 span,
507 default_binding_modes: false,
508 });
509
510 (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
511 }
512
513 fn generate_arg(
514 &mut self,
515 is_method: bool,
516 idx: usize,
517 param_id: HirId,
518 span: Span,
519 ) -> hir::Expr<'hir> {
520 let name = if is_method && idx == 0 {
522 kw::SelfLower
523 } else {
524 Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("arg{0}", idx))
})format!("arg{idx}"))
525 };
526
527 let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
528 ident: Ident::with_dummy_span(name),
529 hir_id: self.next_id(),
530 res: Res::Local(param_id),
531 args: None,
532 infer_args: false,
533 }));
534
535 let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });
536 self.mk_expr(hir::ExprKind::Path(hir::QPath::Resolved(None, path)), span)
537 }
538
539 fn lower_delegation_body(
540 &mut self,
541 delegation: &Delegation,
542 sig_id: DefId,
543 param_count: usize,
544 generics: &mut GenericsGenerationResults<'hir>,
545 span: Span,
546 ) -> (BodyId, HirId, bool) {
547 let block = delegation.body.as_deref();
548 let mut call_expr_id = HirId::INVALID;
549 let mut unused_target_expr = false;
550
551 let block_id = self.lower_body(|this| {
552 let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);
553 let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
554 let mut stmts: &[hir::Stmt<'hir>] = &[];
555
556 let is_method = this.is_method(sig_id, span);
557 let should_generate_block = this.should_generate_block(delegation, sig_id, is_method);
558
559 unused_target_expr = block.is_some() && (param_count == 0 || !should_generate_block);
563
564 for idx in 0..param_count {
565 let (param, pat_node_id) = this.generate_param(is_method, idx, span);
566 parameters.push(param);
567
568 let generate_arg =
569 |this: &mut Self| this.generate_arg(is_method, idx, param.pat.hir_id, span);
570
571 let arg = if let Some(block) = block
572 && idx == 0
573 && should_generate_block
574 {
575 let mut self_resolver = SelfResolver {
576 ctxt: this,
577 path_id: delegation.id,
578 self_param_id: pat_node_id,
579 };
580 self_resolver.visit_block(block);
581 this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
583
584 let block = this.lower_block_noalloc(HirId::INVALID, block, false);
588
589 stmts = block.stmts;
590
591 if let Some(&expr) = block.expr { expr } else { generate_arg(this) }
597 } else {
598 generate_arg(this)
599 };
600
601 args.push(arg);
602 }
603
604 let (final_expr, hir_id) =
605 this.finalize_body_lowering(delegation, stmts, args, generics, span);
606
607 call_expr_id = hir_id;
608
609 (this.arena.alloc_from_iter(parameters), final_expr)
610 });
611
612 if true {
match (&call_expr_id, &HirId::INVALID) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_ne!(call_expr_id, HirId::INVALID);
613
614 (block_id, call_expr_id, unused_target_expr)
615 }
616
617 fn finalize_body_lowering(
618 &mut self,
619 delegation: &Delegation,
620 stmts: &'hir [hir::Stmt<'hir>],
621 args: Vec<hir::Expr<'hir>>,
622 generics: &mut GenericsGenerationResults<'hir>,
623 span: Span,
624 ) -> (hir::Expr<'hir>, HirId) {
625 let path = self.lower_qpath(
626 delegation.id,
627 &delegation.qself,
628 &delegation.path,
629 ParamMode::Optional,
630 AllowReturnTypeNotation::No,
631 ImplTraitContext::Disallowed(ImplTraitPosition::Path),
632 None,
633 );
634
635 let new_path = match path {
636 hir::QPath::Resolved(ty, path) => {
637 let mut new_path = path.clone();
638 let len = new_path.segments.len();
639
640 new_path.segments = self.arena.alloc_from_iter(
641 new_path.segments.iter().enumerate().map(|(idx, segment)| {
642 if idx + 2 == len {
643 self.process_segment(span, segment, &mut generics.parent)
644 } else if idx + 1 == len {
645 self.process_segment(span, segment, &mut generics.child)
646 } else {
647 segment.clone()
648 }
649 }),
650 );
651
652 hir::QPath::Resolved(ty, self.arena.alloc(new_path))
653 }
654 hir::QPath::TypeRelative(ty, segment) => {
655 let segment = self.process_segment(span, segment, &mut generics.child);
656
657 hir::QPath::TypeRelative(ty, self.arena.alloc(segment))
658 }
659 };
660
661 generics.self_ty_id = match new_path {
662 hir::QPath::Resolved(ty, _) => ty,
663 hir::QPath::TypeRelative(ty, _) => Some(ty),
664 }
665 .map(|ty| ty.hir_id);
666
667 let callee_path = self.arena.alloc(self.mk_expr(hir::ExprKind::Path(new_path), span));
668 let args = self.arena.alloc_from_iter(args);
669 let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(callee_path, args), span));
670
671 let block = self.arena.alloc(hir::Block {
672 stmts,
673 expr: Some(call),
674 hir_id: self.next_id(),
675 rules: hir::BlockCheckMode::DefaultBlock,
676 span,
677 targeted_by_break: false,
678 });
679
680 (self.mk_expr(hir::ExprKind::Block(block, None), span), call.hir_id)
681 }
682
683 fn process_segment(
684 &mut self,
685 span: Span,
686 segment: &hir::PathSegment<'hir>,
687 result: &mut GenericsGenerationResult<'hir>,
688 ) -> hir::PathSegment<'hir> {
689 let details = result.generics.args_propagation_details();
690
691 let generics = result.generics.into_hir_generics(self, span);
694 let segment = if details.should_propagate {
695 let args = generics.into_generic_args(self, span);
696
697 let args = if args.is_empty() { None } else { Some(args) };
699
700 hir::PathSegment { args, ..segment.clone() }
701 } else {
702 segment.clone()
703 };
704
705 if details.use_args_in_sig_inheritance {
706 result.args_segment_id = Some(segment.hir_id);
707 }
708
709 segment
710 }
711
712 fn generate_delegation_error(
713 &mut self,
714 span: Span,
715 delegation: &Delegation,
716 ) -> DelegationResults<'hir> {
717 let decl = self.arena.alloc(hir::FnDecl::dummy(span));
718
719 let header = self.generate_header_error();
720 let sig = hir::FnSig { decl, header, span };
721
722 let ident = self.lower_ident(delegation.ident);
723
724 let body_id = self.lower_body(|this| {
725 let path = this.lower_qpath(
726 delegation.id,
727 &delegation.qself,
728 &delegation.path,
729 ParamMode::Optional,
730 AllowReturnTypeNotation::No,
731 ImplTraitContext::Disallowed(ImplTraitPosition::Path),
732 None,
733 );
734
735 let callee_path = this.arena.alloc(this.mk_expr(hir::ExprKind::Path(path), span));
736 let args = if let Some(block) = delegation.body.as_ref() {
737 this.arena.alloc_slice(&[this.lower_block_expr(block)])
738 } else {
739 &mut []
740 };
741
742 let call = this.arena.alloc(this.mk_expr(hir::ExprKind::Call(callee_path, args), span));
743
744 let block = this.arena.alloc(hir::Block {
745 stmts: &[],
746 expr: Some(call),
747 hir_id: this.next_id(),
748 rules: hir::BlockCheckMode::DefaultBlock,
749 span,
750 targeted_by_break: false,
751 });
752
753 (&[], this.mk_expr(hir::ExprKind::Block(block, None), span))
754 });
755
756 let generics = hir::Generics::empty();
757 DelegationResults { ident, generics, body_id, sig }
758 }
759
760 fn generate_header_error(&self) -> hir::FnHeader {
761 hir::FnHeader {
762 safety: hir::Safety::Safe.into(),
763 constness: hir::Constness::NotConst,
764 asyncness: hir::IsAsync::NotAsync,
765 abi: ExternAbi::Rust,
766 }
767 }
768
769 #[inline]
770 fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'hir> {
771 hir::Expr { hir_id: self.next_id(), kind, span }
772 }
773}
774
775struct SelfResolver<'a, 'b, 'hir> {
776 ctxt: &'a mut LoweringContext<'b, 'hir>,
777 path_id: NodeId,
778 self_param_id: NodeId,
779}
780
781impl SelfResolver<'_, '_, '_> {
782 fn try_replace_id(&mut self, id: NodeId) {
783 if let Some(res) = self.ctxt.get_partial_res(id)
784 && let Some(Res::Local(sig_id)) = res.full_res()
785 && sig_id == self.path_id
786 {
787 self.ctxt.partial_res_overrides.insert(id, self.self_param_id);
788 }
789 }
790}
791
792impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
793 fn visit_id(&mut self, id: NodeId) {
794 self.try_replace_id(id);
795 }
796}