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;
40
41use ast::visit::Visitor;
42use hir::def::{DefKind, Res};
43use hir::{BodyId, HirId};
44use rustc_abi::ExternAbi;
45use rustc_ast as ast;
46use rustc_ast::*;
47use rustc_data_structures::fx::FxHashSet;
48use rustc_errors::ErrorGuaranteed;
49use rustc_hir::attrs::{AttributeKind, InlineAttr};
50use rustc_hir::def_id::DefId;
51use rustc_hir::{self as hir, FnDeclFlags};
52use rustc_middle::span_bug;
53use rustc_middle::ty::Asyncness;
54use rustc_span::symbol::kw;
55use rustc_span::{Ident, Span, Symbol};
56use smallvec::SmallVec;
57
58use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
59use crate::errors::{CycleInDelegationSignatureResolution, UnresolvedDelegationCallee};
60use crate::{
61    AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
62    ResolverAstLoweringExt,
63};
64
65mod generics;
66
67pub(crate) struct DelegationResults<'hir> {
68    pub body_id: hir::BodyId,
69    pub sig: hir::FnSig<'hir>,
70    pub ident: Ident,
71    pub generics: &'hir hir::Generics<'hir>,
72}
73
74struct AttrAdditionInfo {
75    pub equals: fn(&hir::Attribute) -> bool,
76    pub kind: AttrAdditionKind,
77}
78
79enum AttrAdditionKind {
80    Default { factory: fn(Span) -> hir::Attribute },
81    Inherit { factory: fn(Span, &hir::Attribute) -> hir::Attribute },
82}
83
84const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
85
86static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
87    AttrAdditionInfo {
88        equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
    hir::Attribute::Parsed(AttributeKind::MustUse { .. }) => true,
    _ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),
89        kind: AttrAdditionKind::Inherit {
90            factory: |span, original_attr| {
91                let reason = match original_attr {
92                    hir::Attribute::Parsed(AttributeKind::MustUse { reason, .. }) => *reason,
93                    _ => None,
94                };
95
96                hir::Attribute::Parsed(AttributeKind::MustUse { span, reason })
97            },
98        },
99    },
100    AttrAdditionInfo {
101        equals: |a| #[allow(non_exhaustive_omitted_patterns)] match a {
    hir::Attribute::Parsed(AttributeKind::Inline(..)) => true,
    _ => false,
}matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))),
102        kind: AttrAdditionKind::Default {
103            factory: |span| hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span)),
104        },
105    },
106];
107
108impl<'hir> LoweringContext<'_, 'hir> {
109    fn is_method(&self, def_id: DefId, span: Span) -> bool {
110        match self.tcx.def_kind(def_id) {
111            DefKind::Fn => false,
112            DefKind::AssocFn => self.tcx.associated_item(def_id).is_method(),
113            _ => ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("unexpected DefKind for delegation item"))span_bug!(span, "unexpected DefKind for delegation item"),
114        }
115    }
116
117    pub(crate) fn lower_delegation(
118        &mut self,
119        delegation: &Delegation,
120        item_id: NodeId,
121    ) -> DelegationResults<'hir> {
122        let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
123
124        // Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
125        let sig_id = if let Some(delegation_info) = self.resolver.delegation_info(self.owner.def_id)
126        {
127            self.get_sig_id(delegation_info.resolution_id, span)
128        } else {
129            self.dcx().span_delayed_bug(
130                span,
131                ::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),
132            );
133
134            return self.generate_delegation_error(span, delegation);
135        };
136
137        match sig_id {
138            Ok(sig_id) => {
139                self.add_attrs_if_needed(span, sig_id);
140
141                let is_method = self.is_method(sig_id, span);
142
143                let (param_count, c_variadic) = self.param_count(sig_id);
144
145                let mut generics = self.uplift_delegation_generics(delegation, sig_id, is_method);
146
147                let (body_id, call_expr_id) = self.lower_delegation_body(
148                    delegation,
149                    is_method,
150                    param_count,
151                    &mut generics,
152                    span,
153                );
154
155                let decl = self.lower_delegation_decl(
156                    sig_id,
157                    param_count,
158                    c_variadic,
159                    span,
160                    &generics,
161                    delegation.id,
162                    call_expr_id,
163                );
164
165                let sig = self.lower_delegation_sig(sig_id, decl, span);
166                let ident = self.lower_ident(delegation.ident);
167
168                let generics = self.arena.alloc(hir::Generics {
169                    has_where_clause_predicates: false,
170                    params: self.arena.alloc_from_iter(generics.all_params()),
171                    predicates: self.arena.alloc_from_iter(generics.all_predicates()),
172                    span,
173                    where_clause_span: span,
174                });
175
176                DelegationResults { body_id, sig, ident, generics }
177            }
178            Err(_) => self.generate_delegation_error(span, delegation),
179        }
180    }
181
182    fn add_attrs_if_needed(&mut self, span: Span, sig_id: DefId) {
183        let new_attrs =
184            self.create_new_attrs(ATTRS_ADDITIONS, span, sig_id, self.attrs.get(&PARENT_ID));
185
186        if new_attrs.is_empty() {
187            return;
188        }
189
190        let new_arena_allocated_attrs = match self.attrs.get(&PARENT_ID) {
191            Some(existing_attrs) => self.arena.alloc_from_iter(
192                existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()),
193            ),
194            None => self.arena.alloc_from_iter(new_attrs.into_iter()),
195        };
196
197        self.attrs.insert(PARENT_ID, new_arena_allocated_attrs);
198    }
199
200    fn create_new_attrs(
201        &self,
202        candidate_additions: &[AttrAdditionInfo],
203        span: Span,
204        sig_id: DefId,
205        existing_attrs: Option<&&[hir::Attribute]>,
206    ) -> Vec<hir::Attribute> {
207        candidate_additions
208            .iter()
209            .filter_map(|addition_info| {
210                if let Some(existing_attrs) = existing_attrs
211                    && existing_attrs
212                        .iter()
213                        .any(|existing_attr| (addition_info.equals)(existing_attr))
214                {
215                    return None;
216                }
217
218                match addition_info.kind {
219                    AttrAdditionKind::Default { factory } => Some(factory(span)),
220                    AttrAdditionKind::Inherit { factory, .. } =>
221                    {
222                        #[allow(deprecated)]
223                        self.tcx
224                            .get_all_attrs(sig_id)
225                            .iter()
226                            .find_map(|a| (addition_info.equals)(a).then(|| factory(span, a)))
227                    }
228                }
229            })
230            .collect::<Vec<_>>()
231    }
232
233    fn get_sig_id(&self, mut def_id: DefId, span: Span) -> Result<DefId, ErrorGuaranteed> {
234        let mut visited: FxHashSet<DefId> = Default::default();
235        let mut path: SmallVec<[DefId; 1]> = Default::default();
236
237        loop {
238            visited.insert(def_id);
239
240            path.push(def_id);
241
242            // If def_id is in local crate and it corresponds to another delegation
243            // it means that we refer to another delegation as a callee, so in order to obtain
244            // a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
245            if let Some(local_id) = def_id.as_local()
246                && let Some(delegation_info) = self.resolver.delegation_info(local_id)
247            {
248                def_id = delegation_info.resolution_id;
249                if visited.contains(&def_id) {
250                    // We encountered a cycle in the resolution, or delegation callee refers to non-existent
251                    // entity, in this case emit an error.
252                    return Err(match visited.len() {
253                        1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),
254                        _ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
255                    });
256                }
257            } else {
258                return Ok(path[0]);
259            }
260        }
261    }
262
263    fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
264        self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
265    }
266
267    // Function parameter count, including C variadic `...` if present.
268    fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
269        let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();
270        (sig.inputs().len() + usize::from(sig.c_variadic()), sig.c_variadic())
271    }
272
273    fn lower_delegation_decl(
274        &mut self,
275        sig_id: DefId,
276        param_count: usize,
277        c_variadic: bool,
278        span: Span,
279        generics: &GenericsGenerationResults<'hir>,
280        call_path_node_id: NodeId,
281        call_expr_id: HirId,
282    ) -> &'hir hir::FnDecl<'hir> {
283        // The last parameter in C variadic functions is skipped in the signature,
284        // like during regular lowering.
285        let decl_param_count = param_count - c_variadic as usize;
286        let inputs = self.arena.alloc_from_iter((0..decl_param_count).map(|arg| hir::Ty {
287            hir_id: self.next_id(),
288            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
289                sig_id,
290                hir::InferDelegationSig::Input(arg),
291            )),
292            span,
293        }));
294
295        let output = self.arena.alloc(hir::Ty {
296            hir_id: self.next_id(),
297            kind: hir::TyKind::InferDelegation(hir::InferDelegation::Sig(
298                sig_id,
299                hir::InferDelegationSig::Output(self.arena.alloc(hir::DelegationInfo {
300                    call_expr_id,
301                    call_path_res: self.get_resolution_id(call_path_node_id),
302                    child_args_segment_id: generics.child.args_segment_id,
303                    parent_args_segment_id: generics.parent.args_segment_id,
304                    self_ty_id: generics.self_ty_id,
305                    propagate_self_ty: generics.propagate_self_ty,
306                })),
307            )),
308            span,
309        });
310
311        self.arena.alloc(hir::FnDecl {
312            inputs,
313            output: hir::FnRetTy::Return(output),
314            fn_decl_kind: FnDeclFlags::default()
315                .set_lifetime_elision_allowed(true)
316                .set_c_variadic(c_variadic),
317        })
318    }
319
320    fn lower_delegation_sig(
321        &mut self,
322        sig_id: DefId,
323        decl: &'hir hir::FnDecl<'hir>,
324        span: Span,
325    ) -> hir::FnSig<'hir> {
326        let sig = self.tcx.fn_sig(sig_id).skip_binder().skip_binder();
327        let asyncness = match self.tcx.asyncness(sig_id) {
328            Asyncness::Yes => hir::IsAsync::Async(span),
329            Asyncness::No => hir::IsAsync::NotAsync,
330        };
331
332        let header = hir::FnHeader {
333            safety: if self.tcx.codegen_fn_attrs(sig_id).safe_target_features {
334                hir::HeaderSafety::SafeTargetFeatures
335            } else {
336                hir::HeaderSafety::Normal(sig.safety())
337            },
338            constness: self.tcx.constness(sig_id),
339            asyncness,
340            abi: sig.abi(),
341        };
342
343        hir::FnSig { decl, header, span }
344    }
345
346    fn generate_param(
347        &mut self,
348        is_method: bool,
349        idx: usize,
350        span: Span,
351    ) -> (hir::Param<'hir>, NodeId) {
352        let pat_node_id = self.next_node_id();
353        let pat_id = self.lower_node_id(pat_node_id);
354        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
355        let name = if is_method && idx == 0 {
356            kw::SelfLower
357        } else {
358            Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", idx))
    })format!("arg{idx}"))
359        };
360        let ident = Ident::with_dummy_span(name);
361        let pat = self.arena.alloc(hir::Pat {
362            hir_id: pat_id,
363            kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
364            span,
365            default_binding_modes: false,
366        });
367
368        (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
369    }
370
371    fn generate_arg(
372        &mut self,
373        is_method: bool,
374        idx: usize,
375        param_id: HirId,
376        span: Span,
377    ) -> hir::Expr<'hir> {
378        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
379        let name = if is_method && idx == 0 {
380            kw::SelfLower
381        } else {
382            Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("arg{0}", idx))
    })format!("arg{idx}"))
383        };
384
385        let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
386            ident: Ident::with_dummy_span(name),
387            hir_id: self.next_id(),
388            res: Res::Local(param_id),
389            args: None,
390            infer_args: false,
391        }));
392
393        let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });
394        self.mk_expr(hir::ExprKind::Path(hir::QPath::Resolved(None, path)), span)
395    }
396
397    fn lower_delegation_body(
398        &mut self,
399        delegation: &Delegation,
400        is_method: bool,
401        param_count: usize,
402        generics: &mut GenericsGenerationResults<'hir>,
403        span: Span,
404    ) -> (BodyId, HirId) {
405        let block = delegation.body.as_deref();
406        let mut call_expr_id = HirId::INVALID;
407
408        let block_id = self.lower_body(|this| {
409            let mut parameters: Vec<hir::Param<'_>> = Vec::with_capacity(param_count);
410            let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
411
412            for idx in 0..param_count {
413                let (param, pat_node_id) = this.generate_param(is_method, idx, span);
414                parameters.push(param);
415
416                let arg = if let Some(block) = block
417                    && idx == 0
418                {
419                    let mut self_resolver = SelfResolver {
420                        ctxt: this,
421                        path_id: delegation.id,
422                        self_param_id: pat_node_id,
423                    };
424                    self_resolver.visit_block(block);
425                    // Target expr needs to lower `self` path.
426                    this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
427                    this.lower_target_expr(&block)
428                } else {
429                    this.generate_arg(is_method, idx, param.pat.hir_id, span)
430                };
431                args.push(arg);
432            }
433
434            // If we have no params in signature function but user still wrote some code in
435            // delegation body, then add this code as first arg, eventually an error will be shown,
436            // also nested delegations may need to access information about this code (#154332),
437            // so it is better to leave this code as opposed to bodies of extern functions,
438            // which are completely erased from existence.
439            if param_count == 0
440                && let Some(block) = block
441            {
442                args.push(this.lower_target_expr(&block));
443            }
444
445            let (final_expr, hir_id) =
446                this.finalize_body_lowering(delegation, args, generics, span);
447
448            call_expr_id = hir_id;
449
450            (this.arena.alloc_from_iter(parameters), final_expr)
451        });
452
453        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);
454
455        (block_id, call_expr_id)
456    }
457
458    // FIXME(fn_delegation): Alternatives for target expression lowering:
459    // https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.
460    fn lower_target_expr(&mut self, block: &Block) -> hir::Expr<'hir> {
461        if let [stmt] = block.stmts.as_slice()
462            && let StmtKind::Expr(expr) = &stmt.kind
463        {
464            return self.lower_expr_mut(expr);
465        }
466
467        let block = self.lower_block(block, false);
468        self.mk_expr(hir::ExprKind::Block(block, None), block.span)
469    }
470
471    fn finalize_body_lowering(
472        &mut self,
473        delegation: &Delegation,
474        args: Vec<hir::Expr<'hir>>,
475        generics: &mut GenericsGenerationResults<'hir>,
476        span: Span,
477    ) -> (hir::Expr<'hir>, HirId) {
478        let path = self.lower_qpath(
479            delegation.id,
480            &delegation.qself,
481            &delegation.path,
482            ParamMode::Optional,
483            AllowReturnTypeNotation::No,
484            ImplTraitContext::Disallowed(ImplTraitPosition::Path),
485            None,
486        );
487
488        let new_path = match path {
489            hir::QPath::Resolved(ty, path) => {
490                let mut new_path = path.clone();
491                let len = new_path.segments.len();
492
493                new_path.segments = self.arena.alloc_from_iter(
494                    new_path.segments.iter().enumerate().map(|(idx, segment)| {
495                        if idx + 2 == len {
496                            self.process_segment(span, segment, &mut generics.parent)
497                        } else if idx + 1 == len {
498                            self.process_segment(span, segment, &mut generics.child)
499                        } else {
500                            segment.clone()
501                        }
502                    }),
503                );
504
505                hir::QPath::Resolved(ty, self.arena.alloc(new_path))
506            }
507            hir::QPath::TypeRelative(ty, segment) => {
508                let segment = self.process_segment(span, segment, &mut generics.child);
509
510                hir::QPath::TypeRelative(ty, self.arena.alloc(segment))
511            }
512        };
513
514        generics.self_ty_id = match new_path {
515            hir::QPath::Resolved(ty, _) => ty,
516            hir::QPath::TypeRelative(ty, _) => Some(ty),
517        }
518        .map(|ty| ty.hir_id);
519
520        let callee_path = self.arena.alloc(self.mk_expr(hir::ExprKind::Path(new_path), span));
521        let args = self.arena.alloc_from_iter(args);
522        let call = self.arena.alloc(self.mk_expr(hir::ExprKind::Call(callee_path, args), span));
523
524        let block = self.arena.alloc(hir::Block {
525            stmts: &[],
526            expr: Some(call),
527            hir_id: self.next_id(),
528            rules: hir::BlockCheckMode::DefaultBlock,
529            span,
530            targeted_by_break: false,
531        });
532
533        (self.mk_expr(hir::ExprKind::Block(block, None), span), call.hir_id)
534    }
535
536    fn process_segment(
537        &mut self,
538        span: Span,
539        segment: &hir::PathSegment<'hir>,
540        result: &mut GenericsGenerationResult<'hir>,
541    ) -> hir::PathSegment<'hir> {
542        let details = result.generics.args_propagation_details();
543
544        // Always uplift generic params, because if they are not empty then they
545        // should be generated in delegation.
546        let generics = result.generics.into_hir_generics(self, span);
547        let segment = if details.should_propagate {
548            let args = generics.into_generic_args(self, span);
549
550            // Needed for better error messages (`trait-impl-wrong-args-count.rs` test).
551            let args = if args.is_empty() { None } else { Some(args) };
552
553            hir::PathSegment { args, ..segment.clone() }
554        } else {
555            segment.clone()
556        };
557
558        if details.use_args_in_sig_inheritance {
559            result.args_segment_id = Some(segment.hir_id);
560        }
561
562        segment
563    }
564
565    fn generate_delegation_error(
566        &mut self,
567        span: Span,
568        delegation: &Delegation,
569    ) -> DelegationResults<'hir> {
570        let decl = self.arena.alloc(hir::FnDecl::dummy(span));
571
572        let header = self.generate_header_error();
573        let sig = hir::FnSig { decl, header, span };
574
575        let ident = self.lower_ident(delegation.ident);
576
577        let body_id = self.lower_body(|this| {
578            let path = this.lower_qpath(
579                delegation.id,
580                &delegation.qself,
581                &delegation.path,
582                ParamMode::Optional,
583                AllowReturnTypeNotation::No,
584                ImplTraitContext::Disallowed(ImplTraitPosition::Path),
585                None,
586            );
587
588            let callee_path = this.arena.alloc(this.mk_expr(hir::ExprKind::Path(path), span));
589            let args = if let Some(block) = delegation.body.as_ref() {
590                this.arena.alloc_slice(&[this.lower_target_expr(block)])
591            } else {
592                &mut []
593            };
594
595            let call = this.arena.alloc(this.mk_expr(hir::ExprKind::Call(callee_path, args), span));
596
597            let block = this.arena.alloc(hir::Block {
598                stmts: &[],
599                expr: Some(call),
600                hir_id: this.next_id(),
601                rules: hir::BlockCheckMode::DefaultBlock,
602                span,
603                targeted_by_break: false,
604            });
605
606            (&[], this.mk_expr(hir::ExprKind::Block(block, None), span))
607        });
608
609        let generics = hir::Generics::empty();
610        DelegationResults { ident, generics, body_id, sig }
611    }
612
613    fn generate_header_error(&self) -> hir::FnHeader {
614        hir::FnHeader {
615            safety: hir::Safety::Safe.into(),
616            constness: hir::Constness::NotConst,
617            asyncness: hir::IsAsync::NotAsync,
618            abi: ExternAbi::Rust,
619        }
620    }
621
622    #[inline]
623    fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'hir> {
624        hir::Expr { hir_id: self.next_id(), kind, span }
625    }
626}
627
628struct SelfResolver<'a, 'b, 'hir> {
629    ctxt: &'a mut LoweringContext<'b, 'hir>,
630    path_id: NodeId,
631    self_param_id: NodeId,
632}
633
634impl SelfResolver<'_, '_, '_> {
635    fn try_replace_id(&mut self, id: NodeId) {
636        if let Some(res) = self.ctxt.get_partial_res(id)
637            && let Some(Res::Local(sig_id)) = res.full_res()
638            && sig_id == self.path_id
639        {
640            self.ctxt.partial_res_overrides.insert(id, self.self_param_id);
641        }
642    }
643}
644
645impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
646    fn visit_id(&mut self, id: NodeId) {
647        self.try_replace_id(id);
648    }
649}