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, 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 mut err = if !data.inputs.is_empty() {
272                            self.dcx().create_err(BadReturnTypeNotation::Inputs {
273                                span: data.inputs_span,
274                            })
275                        } else if let FnRetTy::Ty(ty) = &data.output {
276                            self.dcx().create_err(BadReturnTypeNotation::Output {
277                                span: data.inputs_span.shrink_to_hi().to(ty.span),
278                            })
279                        } else {
280                            self.dcx().create_err(BadReturnTypeNotation::NeedsDots {
281                                span: data.inputs_span,
282                            })
283                        };
284                        if !self.tcx.features().return_type_notation()
285                            && self.tcx.sess.is_nightly_build()
286                        {
287                            add_feature_diagnostics(
288                                &mut err,
289                                &self.tcx.sess,
290                                sym::return_type_notation,
291                            );
292                        }
293                        err.emit();
294                        (
295                            GenericArgsCtor {
296                                args: Default::default(),
297                                constraints: &[],
298                                parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
299                                span: path_span,
300                            },
301                            false,
302                        )
303                    }
304                    GenericArgsMode::ParenSugar | GenericArgsMode::Silence => self
305                        .lower_parenthesized_parameter_data(
306                            data,
307                            itctx,
308                            bound_modifier_allowed_features,
309                        ),
310                    GenericArgsMode::Err => {
311                        // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
312                        let sub = if !data.inputs.is_empty() {
313                            // Start of the span to the 1st character of 1st argument
314                            let open_param = data.inputs_span.shrink_to_lo().to(data
315                                .inputs
316                                .first()
317                                .unwrap()
318                                .span
319                                .shrink_to_lo());
320                            // Last character position of last argument to the end of the span
321                            let close_param = data
322                                .inputs
323                                .last()
324                                .unwrap()
325                                .span
326                                .shrink_to_hi()
327                                .to(data.inputs_span.shrink_to_hi());
328
329                            Some(UseAngleBrackets { open_param, close_param })
330                        } else {
331                            None
332                        };
333                        self.dcx().emit_err(GenericTypeWithParentheses { span: data.span, sub });
334                        (
335                            self.lower_angle_bracketed_parameter_data(
336                                &data.as_angle_bracketed_args(),
337                                param_mode,
338                                itctx,
339                            )
340                            .0,
341                            false,
342                        )
343                    }
344                },
345                GenericArgs::ParenthesizedElided(span) => {
346                    match generic_args_mode {
347                        GenericArgsMode::ReturnTypeNotation | GenericArgsMode::Silence => {
348                            // Ok
349                        }
350                        GenericArgsMode::ParenSugar | GenericArgsMode::Err => {
351                            self.dcx().emit_err(BadReturnTypeNotation::Position { span: *span });
352                        }
353                    }
354                    (
355                        GenericArgsCtor {
356                            args: Default::default(),
357                            constraints: &[],
358                            parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
359                            span: *span,
360                        },
361                        false,
362                    )
363                }
364            }
365        } else {
366            (
367                GenericArgsCtor {
368                    args: Default::default(),
369                    constraints: &[],
370                    parenthesized: hir::GenericArgsParentheses::No,
371                    span: path_span.shrink_to_hi(),
372                },
373                param_mode == ParamMode::Optional,
374            )
375        };
376
377        let has_lifetimes =
378            generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
379
380        // FIXME(return_type_notation): Is this correct? I think so.
381        if generic_args.parenthesized != hir::GenericArgsParentheses::ParenSugar && !has_lifetimes {
382            self.maybe_insert_elided_lifetimes_in_path(
383                path_span,
384                segment.id,
385                segment.ident.span,
386                &mut generic_args,
387            );
388        }
389
390        let res = self.expect_full_res(segment.id);
391        let hir_id = self.lower_node_id(segment.id);
392        debug!(
393            "lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
394            segment.ident, segment.id, hir_id,
395        );
396
397        hir::PathSegment {
398            ident: self.lower_ident(segment.ident),
399            hir_id,
400            res: self.lower_res(res),
401            infer_args,
402            args: if generic_args.is_empty() && generic_args.span.is_empty() {
403                None
404            } else {
405                Some(generic_args.into_generic_args(self))
406            },
407        }
408    }
409
410    fn maybe_insert_elided_lifetimes_in_path(
411        &mut self,
412        path_span: Span,
413        segment_id: NodeId,
414        segment_ident_span: Span,
415        generic_args: &mut GenericArgsCtor<'hir>,
416    ) {
417        let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
418            Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
419            None => return,
420            Some(res) => {
421                span_bug!(path_span, "expected an elided lifetime to insert. found {res:?}")
422            }
423        };
424        let expected_lifetimes = end.as_usize() - start.as_usize();
425        debug!(expected_lifetimes);
426
427        // Note: these spans are used for diagnostics when they can't be inferred.
428        // See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
429        let elided_lifetime_span = if generic_args.span.is_empty() {
430            // If there are no brackets, use the identifier span.
431            // HACK: we use find_ancestor_inside to properly suggest elided spans in paths
432            // originating from macros, since the segment's span might be from a macro arg.
433            segment_ident_span.find_ancestor_inside(path_span).unwrap_or(path_span)
434        } else if generic_args.is_empty() {
435            // If there are brackets, but not generic arguments, then use the opening bracket
436            generic_args.span.with_hi(generic_args.span.lo() + BytePos(1))
437        } else {
438            // Else use an empty span right after the opening bracket.
439            generic_args.span.with_lo(generic_args.span.lo() + BytePos(1)).shrink_to_lo()
440        };
441
442        generic_args.args.insert_many(
443            0,
444            (start.as_u32()..end.as_u32()).map(|i| {
445                let id = NodeId::from_u32(i);
446                let l = self.lower_lifetime(&Lifetime {
447                    id,
448                    ident: Ident::new(kw::Empty, elided_lifetime_span),
449                });
450                GenericArg::Lifetime(l)
451            }),
452        );
453    }
454
455    pub(crate) fn lower_angle_bracketed_parameter_data(
456        &mut self,
457        data: &AngleBracketedArgs,
458        param_mode: ParamMode,
459        itctx: ImplTraitContext,
460    ) -> (GenericArgsCtor<'hir>, bool) {
461        let has_non_lt_args = data.args.iter().any(|arg| match arg {
462            AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_))
463            | AngleBracketedArg::Constraint(_) => false,
464            AngleBracketedArg::Arg(ast::GenericArg::Type(_) | ast::GenericArg::Const(_)) => true,
465        });
466        let args = data
467            .args
468            .iter()
469            .filter_map(|arg| match arg {
470                AngleBracketedArg::Arg(arg) => Some(self.lower_generic_arg(arg, itctx)),
471                AngleBracketedArg::Constraint(_) => None,
472            })
473            .collect();
474        let constraints =
475            self.arena.alloc_from_iter(data.args.iter().filter_map(|arg| match arg {
476                AngleBracketedArg::Constraint(c) => {
477                    Some(self.lower_assoc_item_constraint(c, itctx))
478                }
479                AngleBracketedArg::Arg(_) => None,
480            }));
481        let ctor = GenericArgsCtor {
482            args,
483            constraints,
484            parenthesized: hir::GenericArgsParentheses::No,
485            span: data.span,
486        };
487        (ctor, !has_non_lt_args && param_mode == ParamMode::Optional)
488    }
489
490    fn lower_parenthesized_parameter_data(
491        &mut self,
492        data: &ParenthesizedArgs,
493        itctx: ImplTraitContext,
494        bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
495    ) -> (GenericArgsCtor<'hir>, bool) {
496        // Switch to `PassThrough` mode for anonymous lifetimes; this
497        // means that we permit things like `&Ref<T>`, where `Ref` has
498        // a hidden lifetime parameter. This is needed for backwards
499        // compatibility, even in contexts like an impl header where
500        // we generally don't permit such things (see #51008).
501        let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
502        let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| {
503            self.lower_ty_direct(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
504        }));
505        let output_ty = match output {
506            // Only allow `impl Trait` in return position. i.e.:
507            // ```rust
508            // fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
509            // //      disallowed --^^^^^^^^^^        allowed --^^^^^^^^^^
510            // ```
511            FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
512                if self.tcx.features().impl_trait_in_fn_trait_return() {
513                    self.lower_ty(ty, itctx)
514                } else {
515                    self.lower_ty(
516                        ty,
517                        ImplTraitContext::FeatureGated(
518                            ImplTraitPosition::FnTraitReturn,
519                            sym::impl_trait_in_fn_trait_return,
520                        ),
521                    )
522                }
523            }
524            FnRetTy::Ty(ty) => {
525                self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))
526            }
527            FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
528        };
529        let args = smallvec![GenericArg::Type(
530            self.arena.alloc(self.ty_tup(*inputs_span, inputs)).try_as_ambig_ty().unwrap()
531        )];
532
533        // If we have a bound like `async Fn() -> T`, make sure that we mark the
534        // `Output = T` associated type bound with the right feature gates.
535        let mut output_span = output_ty.span;
536        if let Some(bound_modifier_allowed_features) = bound_modifier_allowed_features {
537            output_span = self.mark_span_with_reason(
538                DesugaringKind::BoundModifier,
539                output_span,
540                Some(bound_modifier_allowed_features),
541            );
542        }
543        let constraint = self.assoc_ty_binding(sym::Output, output_span, output_ty);
544
545        (
546            GenericArgsCtor {
547                args,
548                constraints: arena_vec![self; constraint],
549                parenthesized: hir::GenericArgsParentheses::ParenSugar,
550                span: data.inputs_span,
551            },
552            false,
553        )
554    }
555
556    /// An associated type binding (i.e., associated type equality constraint).
557    pub(crate) fn assoc_ty_binding(
558        &mut self,
559        assoc_ty_name: rustc_span::Symbol,
560        span: Span,
561        ty: &'hir hir::Ty<'hir>,
562    ) -> hir::AssocItemConstraint<'hir> {
563        let ident = Ident::with_dummy_span(assoc_ty_name);
564        let kind = hir::AssocItemConstraintKind::Equality { term: ty.into() };
565        let args = arena_vec![self;];
566        let constraints = arena_vec![self;];
567        let gen_args = self.arena.alloc(hir::GenericArgs {
568            args,
569            constraints,
570            parenthesized: hir::GenericArgsParentheses::No,
571            span_ext: DUMMY_SP,
572        });
573        hir::AssocItemConstraint {
574            hir_id: self.next_id(),
575            gen_args,
576            span: self.lower_span(span),
577            ident,
578            kind,
579        }
580    }
581
582    /// When a bound is annotated with `async`, it signals to lowering that the trait
583    /// that the bound refers to should be mapped to the "async" flavor of the trait.
584    ///
585    /// This only needs to be done until we unify `AsyncFn` and `Fn` traits into one
586    /// that is generic over `async`ness, if that's ever possible, or modify the
587    /// lowering of `async Fn()` bounds to desugar to another trait like `LendingFn`.
588    fn map_trait_to_async_trait(&self, def_id: DefId) -> Option<DefId> {
589        let lang_items = self.tcx.lang_items();
590        if Some(def_id) == lang_items.fn_trait() {
591            lang_items.async_fn_trait()
592        } else if Some(def_id) == lang_items.fn_mut_trait() {
593            lang_items.async_fn_mut_trait()
594        } else if Some(def_id) == lang_items.fn_once_trait() {
595            lang_items.async_fn_once_trait()
596        } else {
597            None
598        }
599    }
600}