Skip to main content

rustc_ast_lowering/
delegation.rs

1//! This module implements expansion of delegation items with early resolved paths.
2//! It includes a delegation to a free functions:
3//!
4//! ```ignore (illustrative)
5//! reuse module::name { target_expr_template }
6//! ```
7//!
8//! And delegation to a trait methods:
9//!
10//! ```ignore (illustrative)
11//! reuse <Type as Trait>::name { target_expr_template }
12//! ```
13//!
14//! After expansion for both cases we get:
15//!
16//! ```ignore (illustrative)
17//! fn name(
18//!     arg0: InferDelegation(sig_id, Input(0)),
19//!     arg1: InferDelegation(sig_id, Input(1)),
20//!     ...,
21//!     argN: InferDelegation(sig_id, Input(N)),
22//! ) -> InferDelegation(sig_id, Output) {
23//!     callee_path(target_expr_template(arg0), arg1, ..., argN)
24//! }
25//! ```
26//!
27//! Where `callee_path` is a path in delegation item e.g. `<Type as Trait>::name`.
28//! `sig_id` is a id of item from which the signature is inherited. It may be a delegation
29//! item id (`item_id`) in case of impl trait or path resolution id (`path_id`) otherwise.
30//!
31//! Since we do not have a proper way to obtain function type information by path resolution
32//! in AST, we mark each function parameter type as `InferDelegation` and inherit it during
33//! HIR ty lowering.
34//!
35//! Similarly generics, predicates and header are set to the "default" values.
36//! In case of discrepancy with callee function the `UnsupportedDelegation` error will
37//! also be emitted during HIR ty lowering.
38
39use 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;
50use rustc_hir::attrs::{AttributeKind, InlineAttr};
51use rustc_hir::{self as hir, FnDeclFlags};
52use rustc_middle::span_bug;
53use rustc_middle::ty::{Asyncness, PerOwnerResolverData};
54use rustc_span::def_id::{DefId, LocalDefId};
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};
66
67mod generics;
68
69pub(crate) struct DelegationResults<'hir> {
70    pub body_id: hir::BodyId,
71    pub sig: hir::FnSig<'hir>,
72    pub ident: Ident,
73    pub generics: &'hir hir::Generics<'hir>,
74}
75
76struct AttrAdditionInfo {
77    pub equals: fn(&hir::Attribute) -> bool,
78    pub kind: AttrAdditionKind,
79}
80
81enum AttrAdditionKind {
82    Default { factory: fn(Span) -> hir::Attribute },
83    Inherit { factory: fn(Span, &hir::Attribute) -> hir::Attribute },
84}
85
86/// Summary info about function parameters.
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)]
88struct ParamInfo {
89    /// The number of function parameters, including any C variadic `...` parameter.
90    pub param_count: usize,
91
92    /// Whether the function arguments end in a C variadic `...` parameter.
93    pub c_variadic: bool,
94
95    /// The index of the splatted parameter, if any.
96    pub splatted: Option<u16>,
97}
98
99const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
100
101static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
102    AttrAdditionInfo {
103        equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
    hir::Attribute::Parsed(AttributeKind::MustUse { .. }) => true,
    _ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),
104        kind: AttrAdditionKind::Inherit {
105            factory: |span, original_attr| {
106                let reason = match original_attr {
107                    hir::Attribute::Parsed(AttributeKind::MustUse { reason, .. }) => *reason,
108                    _ => None,
109                };
110
111                hir::Attribute::Parsed(AttributeKind::MustUse { span, reason })
112            },
113        },
114    },
115    AttrAdditionInfo {
116        equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
    hir::Attribute::Parsed(AttributeKind::Inline(..)) => true,
    _ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))),
117        kind: AttrAdditionKind::Default {
118            factory: |span| hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span)),
119        },
120    },
121];
122
123impl<'hir> LoweringContext<'_, 'hir> {
124    fn is_method(&self, def_id: DefId, span: Span) -> bool {
125        match self.tcx.def_kind(def_id) {
126            DefKind::Fn => false,
127            DefKind::AssocFn => self.tcx.associated_item(def_id).is_method(),
128            _ => ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("unexpected DefKind for delegation item"))span_bug!(span, "unexpected DefKind for delegation item"),
129        }
130    }
131
132    fn check_for_cycles(&self, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
133        let mut visited: FxHashSet<DefId> = Default::default();
134
135        loop {
136            visited.insert(def_id);
137
138            // If def_id is in local crate and it corresponds to another delegation
139            // it means that we refer to another delegation as a callee, so in order to obtain
140            // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
141            if let Some(local_id) = def_id.as_local()
142                && let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&local_id)
143                && let Ok(id) = info.resolution_id
144            {
145                def_id = id;
146                if visited.contains(&def_id) {
147                    return Err(match visited.len() {
148                        1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),
149                        _ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
150                    });
151                }
152            } else {
153                return Ok(());
154            }
155        }
156    }
157
158    pub(crate) fn lower_delegation(
159        &mut self,
160        delegation: &Delegation,
161        item_id: NodeId,
162    ) -> DelegationResults<'hir> {
163        let span = self.lower_span(delegation.last_segment_span());
164
165        let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&self.owner.def_id) else {
166            self.dcx().span_delayed_bug(
167                span,
168                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("delegation resolution record was not found for {0:?}",
                self.owner.def_id))
    })format!("delegation resolution record was not found for {:?}", self.owner.def_id),
169            );
170
171            return self.generate_delegation_error(span, delegation);
172        };
173
174        let sig_id = info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id));
175
176        // Delegation can be missing from the `delegations_resolutions` table
177        // in illegal places such as function bodies in extern blocks (see #151356).
178        let Ok(sig_id) = sig_id else {
179            self.dcx().span_delayed_bug(
180                span,
181                ::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),
182            );
183
184            return self.generate_delegation_error(span, delegation);
185        };
186
187        self.add_attrs_if_needed(span, sig_id);
188
189        let is_method = self.is_method(sig_id, span);
190
191        let param_info = self.param_info(sig_id);
192
193        if !self.check_block_soundness(delegation, sig_id, is_method, param_info.param_count) {
194            return self.generate_delegation_error(span, delegation);
195        }
196
197        let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);
198
199        let (body_id, call_expr_id, unused_target_expr) = self.lower_delegation_body(
200            delegation,
201            sig_id,
202            param_info.param_count,
203            &mut generics,
204            span,
205        );
206
207        let decl = self.lower_delegation_decl(
208            delegation.source,
209            sig_id,
210            param_info,
211            span,
212            &generics,
213            delegation.id,
214            call_expr_id,
215            unused_target_expr,
216        );
217
218        let sig = self.lower_delegation_sig(sig_id, decl, span);
219        let ident = self.lower_ident(delegation.ident);
220
221        let generics = self.arena.alloc(hir::Generics {
222            has_where_clause_predicates: false,
223            params: self.arena.alloc_from_iter(generics.all_params()),
224            predicates: self.arena.alloc_from_iter(generics.all_predicates()),
225            span,
226            where_clause_span: span,
227        });
228
229        DelegationResults { body_id, sig, ident, generics }
230    }
231
232    fn check_block_soundness(
233        &self,
234        delegation: &Delegation,
235        sig_id: DefId,
236        is_method: bool,
237        param_count: usize,
238    ) -> bool {
239        let Some(block) = delegation.body.as_ref() else { return true };
240        let should_generate_block = self.should_generate_block(delegation, sig_id, is_method);
241
242        // Report an error if user has explicitly specified delegation's target expression
243        // in a single delegation when reused function has no params.
244        if param_count == 0 && should_generate_block {
245            self.dcx().emit_err(DelegationBlockSpecifiedWhenNoParams { span: block.span });
246            return false;
247        }
248
249        struct DefinitionsFinder<'a> {
250            all_owners: &'a NodeMap<PerOwnerResolverData<'a>>,
251            // `self.owner.node_id_to_def_id`
252            nested_def_ids: &'a NodeMap<LocalDefId>,
253        }
254
255        impl<'a> ast::visit::Visitor<'a> for DefinitionsFinder<'a> {
256            type Result = ControlFlow<()>;
257
258            fn visit_id(&mut self, id: NodeId) -> Self::Result {
259                /*
260                    (from `tests\ui\delegation\target-expr-removal-defs-inside.rs`):
261                    ```rust
262                        reuse impl Trait for S1 {
263                            some::path::<{ fn foo() {} }>::xd();
264                            fn foo() {}
265                            self.0
266                        }
267                    ```
268
269                    Constant from unresolved path will be in `nested_owners`,
270                    `fn foo() {}` will not be in `nested_owners` but will be in `owners`,
271                    both have `LocalDefId`, so we check those two maps.
272                */
273                match self.all_owners.contains_key(&id) || self.nested_def_ids.contains_key(&id) {
274                    true => ControlFlow::Break(()),
275                    false => ControlFlow::Continue(()),
276                }
277            }
278        }
279
280        let mut collector = DefinitionsFinder {
281            all_owners: &self.resolver.owners,
282            nested_def_ids: &self.owner.node_id_to_def_id,
283        };
284
285        let contains_defs = collector.visit_block(block).is_break();
286
287        // If there are definitions inside and we can't delete target expression, so report an error.
288        // FIXME(fn_delegation): support deletion of target expression with defs inside.
289        if !should_generate_block && contains_defs {
290            self.dcx().emit_err(DelegationAttemptedBlockWithDefsDeletion { span: block.span });
291            return false;
292        }
293
294        true
295    }
296
297    fn should_generate_block(
298        &self,
299        delegation: &Delegation,
300        sig_id: DefId,
301        is_method: bool,
302    ) -> bool {
303        is_method
304            || #[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)
305            || #[allow(non_exhaustive_omitted_patterns)] match delegation.source {
    DelegationSource::Single => true,
    _ => false,
}matches!(delegation.source, DelegationSource::Single)
306    }
307
308    fn add_attrs_if_needed(&mut self, span: Span, sig_id: DefId) {
309        let new_attrs =
310            self.create_new_attrs(ATTRS_ADDITIONS, span, sig_id, self.attrs.get(&PARENT_ID));
311
312        if new_attrs.is_empty() {
313            return;
314        }
315
316        let new_arena_allocated_attrs = match self.attrs.get(&PARENT_ID) {
317            Some(existing_attrs) => self.arena.alloc_from_iter(
318                existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()),
319            ),
320            None => self.arena.alloc_from_iter(new_attrs.into_iter()),
321        };
322
323        self.attrs.insert(PARENT_ID, new_arena_allocated_attrs);
324    }
325
326    fn create_new_attrs(
327        &self,
328        candidate_additions: &[AttrAdditionInfo],
329        span: Span,
330        sig_id: DefId,
331        existing_attrs: Option<&&[hir::Attribute]>,
332    ) -> Vec<hir::Attribute> {
333        candidate_additions
334            .iter()
335            .filter_map(|addition_info| {
336                if let Some(existing_attrs) = existing_attrs
337                    && existing_attrs
338                        .iter()
339                        .any(|existing_attr| (addition_info.equals)(existing_attr))
340                {
341                    return None;
342                }
343
344                match addition_info.kind {
345                    AttrAdditionKind::Default { factory } => Some(factory(span)),
346                    AttrAdditionKind::Inherit { factory, .. } =>
347                    {
348                        #[allow(deprecated)]
349                        self.tcx
350                            .get_all_attrs(sig_id)
351                            .iter()
352                            .find_map(|a| (addition_info.equals)(a).then(|| factory(span, a)))
353                    }
354                }
355            })
356            .collect::<Vec<_>>()
357    }
358
359    fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
360        self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
361    }
362
363    /// Returns function parameter info, including C variadic `...` and `#[splat]` if present.
364    fn param_info(&self, def_id: DefId) -> ParamInfo {
365        let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();
366
367        // FIXME(splat): use `sig.splatted()` once FnSig has it
368        ParamInfo {
369            param_count: sig.inputs().len() + usize::from(sig.c_variadic()),
370            c_variadic: sig.c_variadic(),
371            splatted: None,
372        }
373    }
374
375    fn lower_delegation_decl(
376        &mut self,
377        source: DelegationSource,
378        sig_id: DefId,
379        param_info: ParamInfo,
380        span: Span,
381        generics: &GenericsGenerationResults<'hir>,
382        call_path_node_id: NodeId,
383        call_expr_id: HirId,
384        unused_target_expr: bool,
385    ) -> &'hir hir::FnDecl<'hir> {
386        let ParamInfo { param_count, c_variadic, splatted } = param_info;
387
388        // The last parameter in C variadic functions is skipped in the signature,
389        // like during regular lowering.
390        let decl_param_count = param_count - c_variadic as usize;
391        let inputs = self.arena.alloc_from_iter((0..decl_param_count).map(|arg| hir::Ty {
392            hir_id: self.next_id(),
393            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
394                sig_id,
395                hir::InferDelegationSig::Input(arg),
396            )),
397            span,
398        }));
399
400        let output = self.arena.alloc(hir::Ty {
401            hir_id: self.next_id(),
402            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
403                sig_id,
404                hir::InferDelegationSig::Output(self.arena.alloc(hir::DelegationInfo {
405                    call_expr_id,
406                    call_path_res: self.get_resolution_id(call_path_node_id),
407                    child_args_segment_id: generics.child.args_segment_id,
408                    parent_args_segment_id: generics.parent.args_segment_id,
409                    self_ty_id: generics.self_ty_id,
410                    propagate_self_ty: generics.propagate_self_ty,
411                    group_id: {
412                        let id = match source {
413                            DelegationSource::Single => None,
414                            DelegationSource::List(expn_id) => Some(expn_id),
415                            DelegationSource::Glob => {
416                                Some(self.tcx.expn_that_defined(self.owner.def_id).expect_local())
417                            }
418                        };
419
420                        id.map(|id| (id, unused_target_expr))
421                    },
422                })),
423            )),
424            span,
425        });
426
427        self.arena.alloc(hir::FnDecl {
428            inputs,
429            output: hir::FnRetTy::Return(output),
430            fn_decl_kind: FnDeclFlags::default()
431                .set_lifetime_elision_allowed(true)
432                .set_c_variadic(c_variadic)
433                .set_splatted(splatted, inputs.len())
434                .unwrap(),
435        })
436    }
437
438    fn lower_delegation_sig(
439        &mut self,
440        sig_id: DefId,
441        decl: &'hir hir::FnDecl<'hir>,
442        span: Span,
443    ) -> hir::FnSig<'hir> {
444        let sig = self.tcx.fn_sig(sig_id).skip_binder().skip_binder();
445        let asyncness = match self.tcx.asyncness(sig_id) {
446            Asyncness::Yes => hir::IsAsync::Async(span),
447            Asyncness::No => hir::IsAsync::NotAsync,
448        };
449
450        let header = hir::FnHeader {
451            safety: if self.tcx.codegen_fn_attrs(sig_id).safe_target_features {
452                hir::HeaderSafety::SafeTargetFeatures
453            } else {
454                hir::HeaderSafety::Normal(sig.safety())
455            },
456            constness: self.tcx.constness(sig_id),
457            asyncness,
458            abi: sig.abi(),
459        };
460
461        hir::FnSig { decl, header, span }
462    }
463
464    fn generate_param(
465        &mut self,
466        is_method: bool,
467        idx: usize,
468        span: Span,
469    ) -> (hir::Param<'hir>, NodeId) {
470        let pat_node_id = self.next_node_id();
471        let pat_id = self.lower_node_id(pat_node_id);
472        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
473        let name = if is_method && idx == 0 {
474            kw::SelfLower
475        } else {
476            Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", idx))
    })format!("arg{idx}"))
477        };
478        let ident = Ident::with_dummy_span(name);
479        let pat = self.arena.alloc(hir::Pat {
480            hir_id: pat_id,
481            kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
482            span,
483            default_binding_modes: false,
484        });
485
486        (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
487    }
488
489    fn generate_arg(
490        &mut self,
491        is_method: bool,
492        idx: usize,
493        param_id: HirId,
494        span: Span,
495    ) -> hir::Expr<'hir> {
496        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
497        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
503        let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
504            ident: Ident::with_dummy_span(name),
505            hir_id: self.next_id(),
506            res: Res::Local(param_id),
507            args: None,
508            infer_args: false,
509        }));
510
511        let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });
512        self.mk_expr(hir::ExprKind::Path(hir::QPath::Resolved(None, path)), span)
513    }
514
515    fn lower_delegation_body(
516        &mut self,
517        delegation: &Delegation,
518        sig_id: DefId,
519        param_count: usize,
520        generics: &mut GenericsGenerationResults<'hir>,
521        span: Span,
522    ) -> (BodyId, HirId, bool) {
523        let block = delegation.body.as_deref();
524        let mut call_expr_id = HirId::INVALID;
525        let mut unused_target_expr = false;
526
527        let block_id = self.lower_body(|this| {
528            let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);
529            let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
530            let mut stmts: &[hir::Stmt<'hir>] = &[];
531
532            let is_method = this.is_method(sig_id, span);
533            let should_generate_block = this.should_generate_block(delegation, sig_id, is_method);
534
535            // Consider non-specified target expression as generated,
536            // as we do not want to emit error when target expression is
537            // not specified.
538            unused_target_expr = block.is_some() && (param_count == 0 || !should_generate_block);
539
540            for idx in 0..param_count {
541                let (param, pat_node_id) = this.generate_param(is_method, idx, span);
542                parameters.push(param);
543
544                let generate_arg =
545                    |this: &mut Self| this.generate_arg(is_method, idx, param.pat.hir_id, span);
546
547                let arg = if let Some(block) = block
548                    && idx == 0
549                    && should_generate_block
550                {
551                    let mut self_resolver = SelfResolver {
552                        ctxt: this,
553                        path_id: delegation.id,
554                        self_param_id: pat_node_id,
555                    };
556                    self_resolver.visit_block(block);
557                    // Target expr needs to lower `self` path.
558                    this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
559
560                    // Lower with `HirId::INVALID` as we will use only expr and stmts.
561                    // FIXME(fn_delegation): Alternatives for target expression lowering:
562                    // https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.
563                    let block = this.lower_block_noalloc(HirId::INVALID, block, false);
564
565                    stmts = block.stmts;
566
567                    // The behavior of the delegation's target expression differs from the
568                    // behavior of the usual block, where if there is no final expression
569                    // the `()` is returned. In case of the similar situation in delegation
570                    // (no final expression) we propagate first argument instead of replacing
571                    // it with `()`.
572                    if let Some(&expr) = block.expr { expr } else { generate_arg(this) }
573                } else {
574                    generate_arg(this)
575                };
576
577                args.push(arg);
578            }
579
580            let (final_expr, hir_id) =
581                this.finalize_body_lowering(delegation, stmts, args, generics, span);
582
583            call_expr_id = hir_id;
584
585            (this.arena.alloc_from_iter(parameters), final_expr)
586        });
587
588        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);
589
590        (block_id, call_expr_id, unused_target_expr)
591    }
592
593    fn finalize_body_lowering(
594        &mut self,
595        delegation: &Delegation,
596        stmts: &'hir [hir::Stmt<'hir>],
597        args: Vec<hir::Expr<'hir>>,
598        generics: &mut GenericsGenerationResults<'hir>,
599        span: Span,
600    ) -> (hir::Expr<'hir>, HirId) {
601        let path = self.lower_qpath(
602            delegation.id,
603            &delegation.qself,
604            &delegation.path,
605            ParamMode::Optional,
606            AllowReturnTypeNotation::No,
607            ImplTraitContext::Disallowed(ImplTraitPosition::Path),
608            None,
609        );
610
611        let new_path = match path {
612            hir::QPath::Resolved(ty, path) => {
613                let mut new_path = path.clone();
614                let len = new_path.segments.len();
615
616                new_path.segments = self.arena.alloc_from_iter(
617                    new_path.segments.iter().enumerate().map(|(idx, segment)| {
618                        if idx + 2 == len {
619                            self.process_segment(span, segment, &mut generics.parent)
620                        } else if idx + 1 == len {
621                            self.process_segment(span, segment, &mut generics.child)
622                        } else {
623                            segment.clone()
624                        }
625                    }),
626                );
627
628                hir::QPath::Resolved(ty, self.arena.alloc(new_path))
629            }
630            hir::QPath::TypeRelative(ty, segment) => {
631                let segment = self.process_segment(span, segment, &mut generics.child);
632
633                hir::QPath::TypeRelative(ty, self.arena.alloc(segment))
634            }
635        };
636
637        generics.self_ty_id = match new_path {
638            hir::QPath::Resolved(ty, _) => ty,
639            hir::QPath::TypeRelative(ty, _) => Some(ty),
640        }
641        .map(|ty| ty.hir_id);
642
643        let callee_path = self.arena.alloc(self.mk_expr(hir::ExprKind::Path(new_path), span));
644        let args = self.arena.alloc_from_iter(args);
645        let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(callee_path, args), span));
646
647        let block = self.arena.alloc(hir::Block {
648            stmts,
649            expr: Some(call),
650            hir_id: self.next_id(),
651            rules: hir::BlockCheckMode::DefaultBlock,
652            span,
653            targeted_by_break: false,
654        });
655
656        (self.mk_expr(hir::ExprKind::Block(block, None), span), call.hir_id)
657    }
658
659    fn process_segment(
660        &mut self,
661        span: Span,
662        segment: &hir::PathSegment<'hir>,
663        result: &mut GenericsGenerationResult<'hir>,
664    ) -> hir::PathSegment<'hir> {
665        let details = result.generics.args_propagation_details();
666
667        // Always uplift generic params, because if they are not empty then they
668        // should be generated in delegation.
669        let generics = result.generics.into_hir_generics(self, span);
670        let segment = if details.should_propagate {
671            let args = generics.into_generic_args(self, span);
672
673            // Needed for better error messages (`trait-impl-wrong-args-count.rs` test).
674            let args = if args.is_empty() { None } else { Some(args) };
675
676            hir::PathSegment { args, ..segment.clone() }
677        } else {
678            segment.clone()
679        };
680
681        if details.use_args_in_sig_inheritance {
682            result.args_segment_id = Some(segment.hir_id);
683        }
684
685        segment
686    }
687
688    fn generate_delegation_error(
689        &mut self,
690        span: Span,
691        delegation: &Delegation,
692    ) -> DelegationResults<'hir> {
693        let decl = self.arena.alloc(hir::FnDecl::dummy(span));
694
695        let header = self.generate_header_error();
696        let sig = hir::FnSig { decl, header, span };
697
698        let ident = self.lower_ident(delegation.ident);
699
700        let body_id = self.lower_body(|this| {
701            let path = this.lower_qpath(
702                delegation.id,
703                &delegation.qself,
704                &delegation.path,
705                ParamMode::Optional,
706                AllowReturnTypeNotation::No,
707                ImplTraitContext::Disallowed(ImplTraitPosition::Path),
708                None,
709            );
710
711            let callee_path = this.arena.alloc(this.mk_expr(hir::ExprKind::Path(path), span));
712            let args = if let Some(block) = delegation.body.as_ref() {
713                this.arena.alloc_slice(&[this.lower_block_expr(block)])
714            } else {
715                &mut []
716            };
717
718            let call = this.arena.alloc(this.mk_expr(hir::ExprKind::Call(callee_path, args), span));
719
720            let block = this.arena.alloc(hir::Block {
721                stmts: &[],
722                expr: Some(call),
723                hir_id: this.next_id(),
724                rules: hir::BlockCheckMode::DefaultBlock,
725                span,
726                targeted_by_break: false,
727            });
728
729            (&[], this.mk_expr(hir::ExprKind::Block(block, None), span))
730        });
731
732        let generics = hir::Generics::empty();
733        DelegationResults { ident, generics, body_id, sig }
734    }
735
736    fn generate_header_error(&self) -> hir::FnHeader {
737        hir::FnHeader {
738            safety: hir::Safety::Safe.into(),
739            constness: hir::Constness::NotConst,
740            asyncness: hir::IsAsync::NotAsync,
741            abi: ExternAbi::Rust,
742        }
743    }
744
745    #[inline]
746    fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'hir> {
747        hir::Expr { hir_id: self.next_id(), kind, span }
748    }
749}
750
751struct SelfResolver<'a, 'b, 'hir> {
752    ctxt: &'a mut LoweringContext<'b, 'hir>,
753    path_id: NodeId,
754    self_param_id: NodeId,
755}
756
757impl SelfResolver<'_, '_, '_> {
758    fn try_replace_id(&mut self, id: NodeId) {
759        if let Some(res) = self.ctxt.get_partial_res(id)
760            && let Some(Res::Local(sig_id)) = res.full_res()
761            && sig_id == self.path_id
762        {
763            self.ctxt.partial_res_overrides.insert(id, self.self_param_id);
764        }
765    }
766}
767
768impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
769    fn visit_id(&mut self, id: NodeId) {
770        self.try_replace_id(id);
771    }
772}