rustc_ast_lowering/
path.rs

1use std::sync::Arc;
2
3use rustc_ast::{self as ast, *};
4use rustc_hir as hir;
5use rustc_hir::GenericArg;
6use rustc_hir::def::{DefKind, PartialRes, Res};
7use rustc_hir::def_id::DefId;
8use rustc_middle::span_bug;
9use rustc_session::parse::add_feature_diagnostics;
10use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
11use smallvec::{SmallVec, smallvec};
12use tracing::{debug, instrument};
13
14use super::errors::{
15    AsyncBoundNotOnTrait, AsyncBoundOnlyForFnTraits, BadReturnTypeNotation,
16    GenericTypeWithParentheses, RTNSuggestion, UseAngleBrackets,
17};
18use super::{
19    AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
20    LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
21};
22
23impl<'a, 'hir> LoweringContext<'a, 'hir> {
24    #[instrument(level = "trace", skip(self))]
25    pub(crate) fn lower_qpath(
26        &mut self,
27        id: NodeId,
28        qself: &Option<ptr::P<QSelf>>,
29        p: &Path,
30        param_mode: ParamMode,
31        allow_return_type_notation: AllowReturnTypeNotation,
32        itctx: ImplTraitContext,
33        // modifiers of the impl/bound if this is a trait path
34        modifiers: Option<ast::TraitBoundModifiers>,
35    ) -> hir::QPath<'hir> {
36        let qself_position = qself.as_ref().map(|q| q.position);
37        let qself = qself
38            .as_ref()
39            // Reject cases like `<impl Trait>::Assoc` and `<impl Trait as Trait>::Assoc`.
40            .map(|q| self.lower_ty(&q.ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path)));
41
42        let partial_res =
43            self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
44        let base_res = partial_res.base_res();
45        let unresolved_segments = partial_res.unresolved_segments();
46
47        let mut res = self.lower_res(base_res);
48
49        // When we have an `async` kw on a bound, map the trait it resolves to.
50        if let Some(TraitBoundModifiers { asyncness: BoundAsyncness::Async(_), .. }) = modifiers {
51            match res {
52                Res::Def(DefKind::Trait, def_id) => {
53                    if let Some(async_def_id) = self.map_trait_to_async_trait(def_id) {
54                        res = Res::Def(DefKind::Trait, async_def_id);
55                    } else {
56                        self.dcx().emit_err(AsyncBoundOnlyForFnTraits { span: p.span });
57                    }
58                }
59                Res::Err => {
60                    // No additional error.
61                }
62                _ => {
63                    // This error isn't actually emitted AFAICT, but it's best to keep
64                    // it around in case the resolver doesn't always check the defkind
65                    // of an item or something.
66                    self.dcx().emit_err(AsyncBoundNotOnTrait { span: p.span, descr: res.descr() });
67                }
68            }
69        }
70
71        // Ungate the `async_fn_traits` feature in the path if the trait is
72        // named via either `async Fn*()` or `AsyncFn*()`.
73        let bound_modifier_allowed_features = if let Res::Def(DefKind::Trait, async_def_id) = res
74            && self.tcx.async_fn_trait_kind_from_def_id(async_def_id).is_some()
75        {
76            Some(Arc::clone(&self.allow_async_fn_traits))
77        } else {
78            None
79        };
80
81        // Only permit `impl Trait` in the final segment. E.g., we permit `Option<impl Trait>`,
82        // `option::Option<T>::Xyz<impl Trait>` and reject `option::Option<impl Trait>::Xyz`.
83        let itctx = |i| {
84            if i + 1 == p.segments.len() {
85                itctx
86            } else {
87                ImplTraitContext::Disallowed(ImplTraitPosition::Path)
88            }
89        };
90
91        let path_span_lo = p.span.shrink_to_lo();
92        let proj_start = p.segments.len() - unresolved_segments;
93        let path = self.arena.alloc(hir::Path {
94            res,
95            segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map(
96                |(i, segment)| {
97                    let param_mode = match (qself_position, param_mode) {
98                        (Some(j), ParamMode::Optional) if i < j => {
99                            // This segment is part of the trait path in a
100                            // qualified path - one of `a`, `b` or `Trait`
101                            // in `<X as a::b::Trait>::T::U::method`.
102                            ParamMode::Explicit
103                        }
104                        _ => param_mode,
105                    };
106
107                    let generic_args_mode = match base_res {
108                        // `a::b::Trait(Args)`
109                        Res::Def(DefKind::Trait, _) if i + 1 == proj_start => {
110                            GenericArgsMode::ParenSugar
111                        }
112                        // `a::b::Trait(Args)::TraitItem`
113                        Res::Def(DefKind::AssocFn, _)
114                        | Res::Def(DefKind::AssocConst, _)
115                        | Res::Def(DefKind::AssocTy, _)
116                            if i + 2 == proj_start =>
117                        {
118                            GenericArgsMode::ParenSugar
119                        }
120                        Res::Def(DefKind::AssocFn, _) if i + 1 == proj_start => {
121                            match allow_return_type_notation {
122                                AllowReturnTypeNotation::Yes => GenericArgsMode::ReturnTypeNotation,
123                                AllowReturnTypeNotation::No => GenericArgsMode::Err,
124                            }
125                        }
126                        // Avoid duplicated errors.
127                        Res::Err => GenericArgsMode::Silence,
128                        // An error
129                        _ => GenericArgsMode::Err,
130                    };
131
132                    self.lower_path_segment(
133                        p.span,
134                        segment,
135                        param_mode,
136                        generic_args_mode,
137                        itctx(i),
138                        bound_modifier_allowed_features.clone(),
139                    )
140                },
141            )),
142            span: self.lower_span(
143                p.segments[..proj_start]
144                    .last()
145                    .map_or(path_span_lo, |segment| path_span_lo.to(segment.span())),
146            ),
147        });
148
149        if let Some(bound_modifier_allowed_features) = bound_modifier_allowed_features {
150            path.span = self.mark_span_with_reason(
151                DesugaringKind::BoundModifier,
152                path.span,
153                Some(bound_modifier_allowed_features),
154            );
155        }
156
157        // Simple case, either no projections, or only fully-qualified.
158        // E.g., `std::mem::size_of` or `<I as Iterator>::Item`.
159        if unresolved_segments == 0 {
160            return hir::QPath::Resolved(qself, path);
161        }
162
163        // Create the innermost type that we're projecting from.
164        let mut ty = if path.segments.is_empty() {
165            // If the base path is empty that means there exists a
166            // syntactical `Self`, e.g., `&i32` in `<&i32>::clone`.
167            qself.expect("missing QSelf for <T>::...")
168        } else {
169            // Otherwise, the base path is an implicit `Self` type path,
170            // e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
171            // `<I as Iterator>::Item::default`.
172            let new_id = self.next_id();
173            self.arena.alloc(self.ty_path(new_id, path.span, hir::QPath::Resolved(qself, path)))
174        };
175
176        // Anything after the base path are associated "extensions",
177        // out of which all but the last one are associated types,
178        // e.g., for `std::vec::Vec::<T>::IntoIter::Item::clone`:
179        // * base path is `std::vec::Vec<T>`
180        // * "extensions" are `IntoIter`, `Item` and `clone`
181        // * type nodes are:
182        //   1. `std::vec::Vec<T>` (created above)
183        //   2. `<std::vec::Vec<T>>::IntoIter`
184        //   3. `<<std::vec::Vec<T>>::IntoIter>::Item`
185        // * final path is `<<<std::vec::Vec<T>>::IntoIter>::Item>::clone`
186        for (i, segment) in p.segments.iter().enumerate().skip(proj_start) {
187            // If this is a type-dependent `T::method(..)`.
188            let generic_args_mode = if i + 1 == p.segments.len()
189                && matches!(allow_return_type_notation, AllowReturnTypeNotation::Yes)
190            {
191                GenericArgsMode::ReturnTypeNotation
192            } else {
193                GenericArgsMode::Err
194            };
195
196            let hir_segment = self.arena.alloc(self.lower_path_segment(
197                p.span,
198                segment,
199                param_mode,
200                generic_args_mode,
201                itctx(i),
202                None,
203            ));
204            let qpath = hir::QPath::TypeRelative(ty, hir_segment);
205
206            // It's finished, return the extension of the right node type.
207            if i == p.segments.len() - 1 {
208                return qpath;
209            }
210
211            // Wrap the associated extension in another type node.
212            let new_id = self.next_id();
213            ty = self.arena.alloc(self.ty_path(new_id, path_span_lo.to(segment.span()), qpath));
214        }
215
216        // We should've returned in the for loop above.
217
218        self.dcx().span_bug(
219            p.span,
220            format!(
221                "lower_qpath: no final extension segment in {}..{}",
222                proj_start,
223                p.segments.len()
224            ),
225        );
226    }
227
228    pub(crate) fn lower_use_path(
229        &mut self,
230        res: SmallVec<[Res; 3]>,
231        p: &Path,
232        param_mode: ParamMode,
233    ) -> &'hir hir::UsePath<'hir> {
234        assert!((1..=3).contains(&res.len()));
235        self.arena.alloc(hir::UsePath {
236            res,
237            segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| {
238                self.lower_path_segment(
239                    p.span,
240                    segment,
241                    param_mode,
242                    GenericArgsMode::Err,
243                    ImplTraitContext::Disallowed(ImplTraitPosition::Path),
244                    None,
245                )
246            })),
247            span: self.lower_span(p.span),
248        })
249    }
250
251    pub(crate) fn lower_path_segment(
252        &mut self,
253        path_span: Span,
254        segment: &PathSegment,
255        param_mode: ParamMode,
256        generic_args_mode: GenericArgsMode,
257        itctx: ImplTraitContext,
258        // Additional features ungated with a bound modifier like `async`.
259        // This is passed down to the implicit associated type binding in
260        // parenthesized bounds.
261        bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
262    ) -> hir::PathSegment<'hir> {
263        debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment);
264        let (mut generic_args, infer_args) = if let Some(generic_args) = segment.args.as_deref() {
265            match generic_args {
266                GenericArgs::AngleBracketed(data) => {
267                    self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
268                }
269                GenericArgs::Parenthesized(data) => match generic_args_mode {
270                    GenericArgsMode::ReturnTypeNotation => {
271                        let err = match (&data.inputs[..], &data.output) {
272                            ([_, ..], FnRetTy::Default(_)) => {
273                                BadReturnTypeNotation::Inputs { span: data.inputs_span }
274                            }
275                            ([], FnRetTy::Default(_)) => {
276                                BadReturnTypeNotation::NeedsDots { span: data.inputs_span }
277                            }
278                            // The case `T: Trait<method(..) -> Ret>` is handled in the parser.
279                            (_, FnRetTy::Ty(ty)) => {
280                                let span = data.inputs_span.shrink_to_hi().to(ty.span);
281                                BadReturnTypeNotation::Output {
282                                    span,
283                                    suggestion: RTNSuggestion {
284                                        output: span,
285                                        input: data.inputs_span,
286                                    },
287                                }
288                            }
289                        };
290                        let mut err = self.dcx().create_err(err);
291                        if !self.tcx.features().return_type_notation()
292                            && self.tcx.sess.is_nightly_build()
293                        {
294                            add_feature_diagnostics(
295                                &mut err,
296                                &self.tcx.sess,
297                                sym::return_type_notation,
298                            );
299                        }
300                        err.emit();
301                        (
302                            GenericArgsCtor {
303                                args: Default::default(),
304                                constraints: &[],
305                                parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
306                                span: path_span,
307                            },
308                            false,
309                        )
310                    }
311                    GenericArgsMode::ParenSugar | GenericArgsMode::Silence => self
312                        .lower_parenthesized_parameter_data(
313                            data,
314                            itctx,
315                            bound_modifier_allowed_features,
316                        ),
317                    GenericArgsMode::Err => {
318                        // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
319                        let sub = if !data.inputs.is_empty() {
320                            // Start of the span to the 1st character of 1st argument
321                            let open_param = data.inputs_span.shrink_to_lo().to(data
322                                .inputs
323                                .first()
324                                .unwrap()
325                                .span
326                                .shrink_to_lo());
327                            // Last character position of last argument to the end of the span
328                            let close_param = data
329                                .inputs
330                                .last()
331                                .unwrap()
332                                .span
333                                .shrink_to_hi()
334                                .to(data.inputs_span.shrink_to_hi());
335
336                            Some(UseAngleBrackets { open_param, close_param })
337                        } else {
338                            None
339                        };
340                        self.dcx().emit_err(GenericTypeWithParentheses { span: data.span, sub });
341                        (
342                            self.lower_angle_bracketed_parameter_data(
343                                &data.as_angle_bracketed_args(),
344                                param_mode,
345                                itctx,
346                            )
347                            .0,
348                            false,
349                        )
350                    }
351                },
352                GenericArgs::ParenthesizedElided(span) => {
353                    match generic_args_mode {
354                        GenericArgsMode::ReturnTypeNotation | GenericArgsMode::Silence => {
355                            // Ok
356                        }
357                        GenericArgsMode::ParenSugar | GenericArgsMode::Err => {
358                            self.dcx().emit_err(BadReturnTypeNotation::Position { span: *span });
359                        }
360                    }
361                    (
362                        GenericArgsCtor {
363                            args: Default::default(),
364                            constraints: &[],
365                            parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
366                            span: *span,
367                        },
368                        false,
369                    )
370                }
371            }
372        } else {
373            (
374                GenericArgsCtor {
375                    args: Default::default(),
376                    constraints: &[],
377                    parenthesized: hir::GenericArgsParentheses::No,
378                    span: path_span.shrink_to_hi(),
379                },
380                param_mode == ParamMode::Optional,
381            )
382        };
383
384        let has_lifetimes =
385            generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
386
387        // FIXME(return_type_notation): Is this correct? I think so.
388        if generic_args.parenthesized != hir::GenericArgsParentheses::ParenSugar && !has_lifetimes {
389            self.maybe_insert_elided_lifetimes_in_path(
390                path_span,
391                segment.id,
392                segment.ident.span,
393                &mut generic_args,
394            );
395        }
396
397        let res = self.expect_full_res(segment.id);
398        let hir_id = self.lower_node_id(segment.id);
399        debug!(
400            "lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
401            segment.ident, segment.id, hir_id,
402        );
403
404        hir::PathSegment {
405            ident: self.lower_ident(segment.ident),
406            hir_id,
407            res: self.lower_res(res),
408            infer_args,
409            args: if generic_args.is_empty() && generic_args.span.is_empty() {
410                None
411            } else {
412                Some(generic_args.into_generic_args(self))
413            },
414        }
415    }
416
417    fn maybe_insert_elided_lifetimes_in_path(
418        &mut self,
419        path_span: Span,
420        segment_id: NodeId,
421        segment_ident_span: Span,
422        generic_args: &mut GenericArgsCtor<'hir>,
423    ) {
424        let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
425            Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
426            None => return,
427            Some(res) => {
428                span_bug!(path_span, "expected an elided lifetime to insert. found {res:?}")
429            }
430        };
431        let expected_lifetimes = end.as_usize() - start.as_usize();
432        debug!(expected_lifetimes);
433
434        // Note: these spans are used for diagnostics when they can't be inferred.
435        // See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
436        let elided_lifetime_span = if generic_args.span.is_empty() {
437            // If there are no brackets, use the identifier span.
438            // HACK: we use find_ancestor_inside to properly suggest elided spans in paths
439            // originating from macros, since the segment's span might be from a macro arg.
440            segment_ident_span.find_ancestor_inside(path_span).unwrap_or(path_span)
441        } else if generic_args.is_empty() {
442            // If there are brackets, but not generic arguments, then use the opening bracket
443            generic_args.span.with_hi(generic_args.span.lo() + BytePos(1))
444        } else {
445            // Else use an empty span right after the opening bracket.
446            generic_args.span.with_lo(generic_args.span.lo() + BytePos(1)).shrink_to_lo()
447        };
448
449        generic_args.args.insert_many(
450            0,
451            (start.as_u32()..end.as_u32()).map(|i| {
452                let id = NodeId::from_u32(i);
453                let l = self.lower_lifetime(&Lifetime {
454                    id,
455                    ident: Ident::new(kw::Empty, elided_lifetime_span),
456                });
457                GenericArg::Lifetime(l)
458            }),
459        );
460    }
461
462    pub(crate) fn lower_angle_bracketed_parameter_data(
463        &mut self,
464        data: &AngleBracketedArgs,
465        param_mode: ParamMode,
466        itctx: ImplTraitContext,
467    ) -> (GenericArgsCtor<'hir>, bool) {
468        let has_non_lt_args = data.args.iter().any(|arg| match arg {
469            AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_))
470            | AngleBracketedArg::Constraint(_) => false,
471            AngleBracketedArg::Arg(ast::GenericArg::Type(_) | ast::GenericArg::Const(_)) => true,
472        });
473        let args = data
474            .args
475            .iter()
476            .filter_map(|arg| match arg {
477                AngleBracketedArg::Arg(arg) => Some(self.lower_generic_arg(arg, itctx)),
478                AngleBracketedArg::Constraint(_) => None,
479            })
480            .collect();
481        let constraints =
482            self.arena.alloc_from_iter(data.args.iter().filter_map(|arg| match arg {
483                AngleBracketedArg::Constraint(c) => {
484                    Some(self.lower_assoc_item_constraint(c, itctx))
485                }
486                AngleBracketedArg::Arg(_) => None,
487            }));
488        let ctor = GenericArgsCtor {
489            args,
490            constraints,
491            parenthesized: hir::GenericArgsParentheses::No,
492            span: data.span,
493        };
494        (ctor, !has_non_lt_args && param_mode == ParamMode::Optional)
495    }
496
497    fn lower_parenthesized_parameter_data(
498        &mut self,
499        data: &ParenthesizedArgs,
500        itctx: ImplTraitContext,
501        bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
502    ) -> (GenericArgsCtor<'hir>, bool) {
503        // Switch to `PassThrough` mode for anonymous lifetimes; this
504        // means that we permit things like `&Ref<T>`, where `Ref` has
505        // a hidden lifetime parameter. This is needed for backwards
506        // compatibility, even in contexts like an impl header where
507        // we generally don't permit such things (see #51008).
508        let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
509        let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| {
510            self.lower_ty_direct(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
511        }));
512        let output_ty = match output {
513            // Only allow `impl Trait` in return position. i.e.:
514            // ```rust
515            // fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
516            // //      disallowed --^^^^^^^^^^        allowed --^^^^^^^^^^
517            // ```
518            FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
519                if self.tcx.features().impl_trait_in_fn_trait_return() {
520                    self.lower_ty(ty, itctx)
521                } else {
522                    self.lower_ty(
523                        ty,
524                        ImplTraitContext::FeatureGated(
525                            ImplTraitPosition::FnTraitReturn,
526                            sym::impl_trait_in_fn_trait_return,
527                        ),
528                    )
529                }
530            }
531            FnRetTy::Ty(ty) => {
532                self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))
533            }
534            FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
535        };
536        let args = smallvec![GenericArg::Type(
537            self.arena.alloc(self.ty_tup(*inputs_span, inputs)).try_as_ambig_ty().unwrap()
538        )];
539
540        // If we have a bound like `async Fn() -> T`, make sure that we mark the
541        // `Output = T` associated type bound with the right feature gates.
542        let mut output_span = output_ty.span;
543        if let Some(bound_modifier_allowed_features) = bound_modifier_allowed_features {
544            output_span = self.mark_span_with_reason(
545                DesugaringKind::BoundModifier,
546                output_span,
547                Some(bound_modifier_allowed_features),
548            );
549        }
550        let constraint = self.assoc_ty_binding(sym::Output, output_span, output_ty);
551
552        (
553            GenericArgsCtor {
554                args,
555                constraints: arena_vec![self; constraint],
556                parenthesized: hir::GenericArgsParentheses::ParenSugar,
557                span: data.inputs_span,
558            },
559            false,
560        )
561    }
562
563    /// An associated type binding (i.e., associated type equality constraint).
564    pub(crate) fn assoc_ty_binding(
565        &mut self,
566        assoc_ty_name: rustc_span::Symbol,
567        span: Span,
568        ty: &'hir hir::Ty<'hir>,
569    ) -> hir::AssocItemConstraint<'hir> {
570        let ident = Ident::with_dummy_span(assoc_ty_name);
571        let kind = hir::AssocItemConstraintKind::Equality { term: ty.into() };
572        let args = arena_vec![self;];
573        let constraints = arena_vec![self;];
574        let gen_args = self.arena.alloc(hir::GenericArgs {
575            args,
576            constraints,
577            parenthesized: hir::GenericArgsParentheses::No,
578            span_ext: DUMMY_SP,
579        });
580        hir::AssocItemConstraint {
581            hir_id: self.next_id(),
582            gen_args,
583            span: self.lower_span(span),
584            ident,
585            kind,
586        }
587    }
588
589    /// When a bound is annotated with `async`, it signals to lowering that the trait
590    /// that the bound refers to should be mapped to the "async" flavor of the trait.
591    ///
592    /// This only needs to be done until we unify `AsyncFn` and `Fn` traits into one
593    /// that is generic over `async`ness, if that's ever possible, or modify the
594    /// lowering of `async Fn()` bounds to desugar to another trait like `LendingFn`.
595    fn map_trait_to_async_trait(&self, def_id: DefId) -> Option<DefId> {
596        let lang_items = self.tcx.lang_items();
597        if Some(def_id) == lang_items.fn_trait() {
598            lang_items.async_fn_trait()
599        } else if Some(def_id) == lang_items.fn_mut_trait() {
600            lang_items.async_fn_mut_trait()
601        } else if Some(def_id) == lang_items.fn_once_trait() {
602            lang_items.async_fn_once_trait()
603        } else {
604            None
605        }
606    }
607}