2025-02-03 06:44:41 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{self as ast, *};
|
2020-01-06 06:28:43 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::GenericArg;
|
|
|
|
use rustc_hir::def::{DefKind, PartialRes, Res};
|
2024-01-26 17:00:28 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2023-12-14 23:31:36 -05:00
|
|
|
use rustc_middle::span_bug;
|
2024-08-26 14:51:26 -04:00
|
|
|
use rustc_session::parse::add_feature_diagnostics;
|
2025-03-25 08:21:28 +11:00
|
|
|
use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
|
2022-11-25 17:39:38 +03:00
|
|
|
use smallvec::{SmallVec, smallvec};
|
2024-04-29 16:24:06 +10:00
|
|
|
use tracing::{debug, instrument};
|
2020-01-06 06:28:43 +01:00
|
|
|
|
2024-01-26 17:33:42 +00:00
|
|
|
use super::errors::{
|
2024-06-28 14:10:32 -04:00
|
|
|
AsyncBoundNotOnTrait, AsyncBoundOnlyForFnTraits, BadReturnTypeNotation,
|
2025-02-28 20:05:43 +00:00
|
|
|
GenericTypeWithParentheses, RTNSuggestion, UseAngleBrackets,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2024-01-26 17:33:42 +00:00
|
|
|
use super::{
|
2024-08-26 15:03:30 -04:00
|
|
|
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
|
|
|
|
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
|
|
|
|
2020-01-06 06:28:43 +01:00
|
|
|
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn lower_qpath(
|
2020-01-06 06:28:43 +01:00
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
2022-09-08 10:52:51 +10:00
|
|
|
qself: &Option<ptr::P<QSelf>>,
|
2020-01-06 06:28:43 +01:00
|
|
|
p: &Path,
|
|
|
|
param_mode: ParamMode,
|
2024-08-26 14:51:26 -04:00
|
|
|
allow_return_type_notation: AllowReturnTypeNotation,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2024-01-26 17:00:28 +00:00
|
|
|
// modifiers of the impl/bound if this is a trait path
|
|
|
|
modifiers: Option<ast::TraitBoundModifiers>,
|
2020-01-06 06:28:43 +01:00
|
|
|
) -> hir::QPath<'hir> {
|
|
|
|
let qself_position = qself.as_ref().map(|q| q.position);
|
2024-10-27 06:21:24 +01:00
|
|
|
let qself = qself
|
|
|
|
.as_ref()
|
|
|
|
// Reject cases like `<impl Trait>::Assoc` and `<impl Trait as Trait>::Assoc`.
|
|
|
|
.map(|q| self.lower_ty(&q.ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path)));
|
2020-01-06 06:28:43 +01:00
|
|
|
|
|
|
|
let partial_res =
|
|
|
|
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
|
2022-10-10 19:21:35 +04:00
|
|
|
let base_res = partial_res.base_res();
|
|
|
|
let unresolved_segments = partial_res.unresolved_segments();
|
2020-01-06 06:28:43 +01:00
|
|
|
|
2024-01-26 17:00:28 +00:00
|
|
|
let mut res = self.lower_res(base_res);
|
|
|
|
|
|
|
|
// When we have an `async` kw on a bound, map the trait it resolves to.
|
|
|
|
if let Some(TraitBoundModifiers { asyncness: BoundAsyncness::Async(_), .. }) = modifiers {
|
2024-01-26 17:33:42 +00:00
|
|
|
match res {
|
|
|
|
Res::Def(DefKind::Trait, def_id) => {
|
2024-07-23 19:10:22 -04:00
|
|
|
if let Some(async_def_id) = self.map_trait_to_async_trait(def_id) {
|
2024-01-26 17:33:42 +00:00
|
|
|
res = Res::Def(DefKind::Trait, async_def_id);
|
|
|
|
} else {
|
|
|
|
self.dcx().emit_err(AsyncBoundOnlyForFnTraits { span: p.span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Res::Err => {
|
|
|
|
// No additional error.
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// This error isn't actually emitted AFAICT, but it's best to keep
|
|
|
|
// it around in case the resolver doesn't always check the defkind
|
|
|
|
// of an item or something.
|
|
|
|
self.dcx().emit_err(AsyncBoundNotOnTrait { span: p.span, descr: res.descr() });
|
2024-01-26 17:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:10:22 -04:00
|
|
|
// Ungate the `async_fn_traits` feature in the path if the trait is
|
|
|
|
// named via either `async Fn*()` or `AsyncFn*()`.
|
|
|
|
let bound_modifier_allowed_features = if let Res::Def(DefKind::Trait, async_def_id) = res
|
|
|
|
&& self.tcx.async_fn_trait_kind_from_def_id(async_def_id).is_some()
|
|
|
|
{
|
2025-02-03 06:44:41 +03:00
|
|
|
Some(Arc::clone(&self.allow_async_fn_traits))
|
2024-07-23 19:10:22 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-10-27 06:21:24 +01:00
|
|
|
// Only permit `impl Trait` in the final segment. E.g., we permit `Option<impl Trait>`,
|
|
|
|
// `option::Option<T>::Xyz<impl Trait>` and reject `option::Option<impl Trait>::Xyz`.
|
|
|
|
let itctx = |i| {
|
|
|
|
if i + 1 == p.segments.len() {
|
|
|
|
itctx
|
|
|
|
} else {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Path)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-13 19:14:18 +03:00
|
|
|
let path_span_lo = p.span.shrink_to_lo();
|
2022-10-10 19:21:35 +04:00
|
|
|
let proj_start = p.segments.len() - unresolved_segments;
|
2020-01-06 06:28:43 +01:00
|
|
|
let path = self.arena.alloc(hir::Path {
|
2024-01-26 17:00:28 +00:00
|
|
|
res,
|
2020-01-06 06:28:43 +01:00
|
|
|
segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map(
|
|
|
|
|(i, segment)| {
|
|
|
|
let param_mode = match (qself_position, param_mode) {
|
|
|
|
(Some(j), ParamMode::Optional) if i < j => {
|
|
|
|
// This segment is part of the trait path in a
|
|
|
|
// qualified path - one of `a`, `b` or `Trait`
|
|
|
|
// in `<X as a::b::Trait>::T::U::method`.
|
|
|
|
ParamMode::Explicit
|
|
|
|
}
|
|
|
|
_ => param_mode,
|
|
|
|
};
|
|
|
|
|
2024-08-26 13:59:21 -04:00
|
|
|
let generic_args_mode = match base_res {
|
2020-01-06 06:28:43 +01:00
|
|
|
// `a::b::Trait(Args)`
|
|
|
|
Res::Def(DefKind::Trait, _) if i + 1 == proj_start => {
|
2024-08-26 13:59:21 -04:00
|
|
|
GenericArgsMode::ParenSugar
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
// `a::b::Trait(Args)::TraitItem`
|
2020-03-03 12:29:07 -06:00
|
|
|
Res::Def(DefKind::AssocFn, _)
|
2020-01-06 06:28:43 +01:00
|
|
|
| Res::Def(DefKind::AssocConst, _)
|
|
|
|
| Res::Def(DefKind::AssocTy, _)
|
|
|
|
if i + 2 == proj_start =>
|
|
|
|
{
|
2024-08-26 13:59:21 -04:00
|
|
|
GenericArgsMode::ParenSugar
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
2024-08-26 14:51:26 -04:00
|
|
|
Res::Def(DefKind::AssocFn, _) if i + 1 == proj_start => {
|
|
|
|
match allow_return_type_notation {
|
|
|
|
AllowReturnTypeNotation::Yes => GenericArgsMode::ReturnTypeNotation,
|
|
|
|
AllowReturnTypeNotation::No => GenericArgsMode::Err,
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 06:28:43 +01:00
|
|
|
// Avoid duplicated errors.
|
2024-08-26 16:31:06 -04:00
|
|
|
Res::Err => GenericArgsMode::Silence,
|
2020-01-06 06:28:43 +01:00
|
|
|
// An error
|
2024-08-26 13:59:21 -04:00
|
|
|
_ => GenericArgsMode::Err,
|
2020-01-06 06:28:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.lower_path_segment(
|
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
2024-08-26 13:59:21 -04:00
|
|
|
generic_args_mode,
|
2024-10-27 06:21:24 +01:00
|
|
|
itctx(i),
|
2024-01-26 17:00:28 +00:00
|
|
|
bound_modifier_allowed_features.clone(),
|
2020-01-06 06:28:43 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)),
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(
|
|
|
|
p.segments[..proj_start]
|
|
|
|
.last()
|
|
|
|
.map_or(path_span_lo, |segment| path_span_lo.to(segment.span())),
|
|
|
|
),
|
2020-01-06 06:28:43 +01:00
|
|
|
});
|
|
|
|
|
2024-01-26 17:00:28 +00:00
|
|
|
if let Some(bound_modifier_allowed_features) = bound_modifier_allowed_features {
|
|
|
|
path.span = self.mark_span_with_reason(
|
|
|
|
DesugaringKind::BoundModifier,
|
|
|
|
path.span,
|
|
|
|
Some(bound_modifier_allowed_features),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-06 06:28:43 +01:00
|
|
|
// Simple case, either no projections, or only fully-qualified.
|
|
|
|
// E.g., `std::mem::size_of` or `<I as Iterator>::Item`.
|
2022-10-10 19:21:35 +04:00
|
|
|
if unresolved_segments == 0 {
|
2020-01-06 06:28:43 +01:00
|
|
|
return hir::QPath::Resolved(qself, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the innermost type that we're projecting from.
|
|
|
|
let mut ty = if path.segments.is_empty() {
|
|
|
|
// If the base path is empty that means there exists a
|
|
|
|
// syntactical `Self`, e.g., `&i32` in `<&i32>::clone`.
|
|
|
|
qself.expect("missing QSelf for <T>::...")
|
|
|
|
} else {
|
|
|
|
// Otherwise, the base path is an implicit `Self` type path,
|
|
|
|
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
|
|
|
|
// `<I as Iterator>::Item::default`.
|
|
|
|
let new_id = self.next_id();
|
2021-03-13 19:14:18 +03:00
|
|
|
self.arena.alloc(self.ty_path(new_id, path.span, hir::QPath::Resolved(qself, path)))
|
2020-01-06 06:28:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Anything after the base path are associated "extensions",
|
|
|
|
// out of which all but the last one are associated types,
|
|
|
|
// e.g., for `std::vec::Vec::<T>::IntoIter::Item::clone`:
|
|
|
|
// * base path is `std::vec::Vec<T>`
|
|
|
|
// * "extensions" are `IntoIter`, `Item` and `clone`
|
|
|
|
// * type nodes are:
|
|
|
|
// 1. `std::vec::Vec<T>` (created above)
|
|
|
|
// 2. `<std::vec::Vec<T>>::IntoIter`
|
|
|
|
// 3. `<<std::vec::Vec<T>>::IntoIter>::Item`
|
|
|
|
// * final path is `<<<std::vec::Vec<T>>::IntoIter>::Item>::clone`
|
|
|
|
for (i, segment) in p.segments.iter().enumerate().skip(proj_start) {
|
2024-08-26 14:51:26 -04:00
|
|
|
// If this is a type-dependent `T::method(..)`.
|
|
|
|
let generic_args_mode = if i + 1 == p.segments.len()
|
|
|
|
&& matches!(allow_return_type_notation, AllowReturnTypeNotation::Yes)
|
|
|
|
{
|
|
|
|
GenericArgsMode::ReturnTypeNotation
|
|
|
|
} else {
|
|
|
|
GenericArgsMode::Err
|
|
|
|
};
|
|
|
|
|
2021-03-13 19:14:18 +03:00
|
|
|
let hir_segment = self.arena.alloc(self.lower_path_segment(
|
2020-01-06 06:28:43 +01:00
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
2024-08-26 14:51:26 -04:00
|
|
|
generic_args_mode,
|
2024-10-27 06:21:24 +01:00
|
|
|
itctx(i),
|
2023-07-25 05:58:53 +00:00
|
|
|
None,
|
2020-01-06 06:28:43 +01:00
|
|
|
));
|
2021-03-13 19:14:18 +03:00
|
|
|
let qpath = hir::QPath::TypeRelative(ty, hir_segment);
|
2020-01-06 06:28:43 +01:00
|
|
|
|
|
|
|
// It's finished, return the extension of the right node type.
|
|
|
|
if i == p.segments.len() - 1 {
|
|
|
|
return qpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the associated extension in another type node.
|
|
|
|
let new_id = self.next_id();
|
2021-03-13 19:14:18 +03:00
|
|
|
ty = self.arena.alloc(self.ty_path(new_id, path_span_lo.to(segment.span()), qpath));
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should've returned in the for loop above.
|
2020-03-21 02:38:48 +01:00
|
|
|
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().span_bug(
|
2020-01-06 06:28:43 +01:00
|
|
|
p.span,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
format!(
|
2020-03-21 02:38:48 +01:00
|
|
|
"lower_qpath: no final extension segment in {}..{}",
|
|
|
|
proj_start,
|
|
|
|
p.segments.len()
|
|
|
|
),
|
|
|
|
);
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 17:39:38 +03:00
|
|
|
pub(crate) fn lower_use_path(
|
2020-01-06 06:28:43 +01:00
|
|
|
&mut self,
|
2022-11-25 17:39:38 +03:00
|
|
|
res: SmallVec<[Res; 3]>,
|
2020-01-06 06:28:43 +01:00
|
|
|
p: &Path,
|
|
|
|
param_mode: ParamMode,
|
2022-11-25 17:39:38 +03:00
|
|
|
) -> &'hir hir::UsePath<'hir> {
|
2024-01-30 22:03:16 +03:00
|
|
|
assert!((1..=3).contains(&res.len()));
|
2022-11-25 17:39:38 +03:00
|
|
|
self.arena.alloc(hir::UsePath {
|
2020-01-06 06:28:43 +01:00
|
|
|
res,
|
|
|
|
segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| {
|
|
|
|
self.lower_path_segment(
|
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
2024-08-26 13:59:21 -04:00
|
|
|
GenericArgsMode::Err,
|
2024-02-07 19:27:44 +00:00
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
|
2023-07-25 05:58:53 +00:00
|
|
|
None,
|
2020-01-06 06:28:43 +01:00
|
|
|
)
|
|
|
|
})),
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(p.span),
|
2020-01-06 06:28:43 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn lower_path_segment(
|
2020-01-06 06:28:43 +01:00
|
|
|
&mut self,
|
|
|
|
path_span: Span,
|
|
|
|
segment: &PathSegment,
|
|
|
|
param_mode: ParamMode,
|
2024-08-26 13:59:21 -04:00
|
|
|
generic_args_mode: GenericArgsMode,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2024-01-26 17:00:28 +00:00
|
|
|
// Additional features ungated with a bound modifier like `async`.
|
|
|
|
// This is passed down to the implicit associated type binding in
|
|
|
|
// parenthesized bounds.
|
2025-02-03 06:44:41 +03:00
|
|
|
bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
|
2020-01-06 06:28:43 +01:00
|
|
|
) -> hir::PathSegment<'hir> {
|
2023-07-25 05:58:53 +00:00
|
|
|
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment);
|
2022-11-22 15:37:54 +00:00
|
|
|
let (mut generic_args, infer_args) = if let Some(generic_args) = segment.args.as_deref() {
|
|
|
|
match generic_args {
|
|
|
|
GenericArgs::AngleBracketed(data) => {
|
2020-01-06 06:28:43 +01:00
|
|
|
self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
|
|
|
|
}
|
2024-08-26 13:59:21 -04:00
|
|
|
GenericArgs::Parenthesized(data) => match generic_args_mode {
|
2024-08-26 14:51:26 -04:00
|
|
|
GenericArgsMode::ReturnTypeNotation => {
|
2025-02-28 20:05:43 +00:00
|
|
|
let err = match (&data.inputs[..], &data.output) {
|
|
|
|
([_, ..], FnRetTy::Default(_)) => {
|
|
|
|
BadReturnTypeNotation::Inputs { span: data.inputs_span }
|
|
|
|
}
|
|
|
|
([], FnRetTy::Default(_)) => {
|
|
|
|
BadReturnTypeNotation::NeedsDots { span: data.inputs_span }
|
|
|
|
}
|
|
|
|
// The case `T: Trait<method(..) -> Ret>` is handled in the parser.
|
|
|
|
(_, FnRetTy::Ty(ty)) => {
|
|
|
|
let span = data.inputs_span.shrink_to_hi().to(ty.span);
|
|
|
|
BadReturnTypeNotation::Output {
|
|
|
|
span,
|
|
|
|
suggestion: RTNSuggestion {
|
|
|
|
output: span,
|
|
|
|
input: data.inputs_span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-08-26 14:51:26 -04:00
|
|
|
};
|
2025-02-28 20:05:43 +00:00
|
|
|
let mut err = self.dcx().create_err(err);
|
2024-10-09 09:01:57 +02:00
|
|
|
if !self.tcx.features().return_type_notation()
|
2024-08-26 14:51:26 -04:00
|
|
|
&& self.tcx.sess.is_nightly_build()
|
|
|
|
{
|
|
|
|
add_feature_diagnostics(
|
|
|
|
&mut err,
|
|
|
|
&self.tcx.sess,
|
|
|
|
sym::return_type_notation,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
(
|
|
|
|
GenericArgsCtor {
|
|
|
|
args: Default::default(),
|
|
|
|
constraints: &[],
|
|
|
|
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
|
|
|
|
span: path_span,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
2024-08-26 16:31:06 -04:00
|
|
|
GenericArgsMode::ParenSugar | GenericArgsMode::Silence => self
|
|
|
|
.lower_parenthesized_parameter_data(
|
|
|
|
data,
|
|
|
|
itctx,
|
|
|
|
bound_modifier_allowed_features,
|
|
|
|
),
|
2024-08-26 13:59:21 -04:00
|
|
|
GenericArgsMode::Err => {
|
2022-06-15 02:50:34 +01:00
|
|
|
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
|
2022-08-16 22:28:51 +02:00
|
|
|
let sub = if !data.inputs.is_empty() {
|
2022-06-15 02:50:34 +01:00
|
|
|
// Start of the span to the 1st character of 1st argument
|
|
|
|
let open_param = data.inputs_span.shrink_to_lo().to(data
|
|
|
|
.inputs
|
|
|
|
.first()
|
|
|
|
.unwrap()
|
|
|
|
.span
|
|
|
|
.shrink_to_lo());
|
|
|
|
// Last character position of last argument to the end of the span
|
|
|
|
let close_param = data
|
|
|
|
.inputs
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.span
|
|
|
|
.shrink_to_hi()
|
|
|
|
.to(data.inputs_span.shrink_to_hi());
|
2022-08-16 22:28:51 +02:00
|
|
|
|
|
|
|
Some(UseAngleBrackets { open_param, close_param })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().emit_err(GenericTypeWithParentheses { span: data.span, sub });
|
2020-01-06 06:28:43 +01:00
|
|
|
(
|
|
|
|
self.lower_angle_bracketed_parameter_data(
|
|
|
|
&data.as_angle_bracketed_args(),
|
|
|
|
param_mode,
|
|
|
|
itctx,
|
|
|
|
)
|
|
|
|
.0,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2024-06-28 14:10:32 -04:00
|
|
|
GenericArgs::ParenthesizedElided(span) => {
|
2024-08-26 14:51:26 -04:00
|
|
|
match generic_args_mode {
|
2024-08-26 16:31:06 -04:00
|
|
|
GenericArgsMode::ReturnTypeNotation | GenericArgsMode::Silence => {
|
2024-08-26 14:51:26 -04:00
|
|
|
// Ok
|
|
|
|
}
|
|
|
|
GenericArgsMode::ParenSugar | GenericArgsMode::Err => {
|
|
|
|
self.dcx().emit_err(BadReturnTypeNotation::Position { span: *span });
|
|
|
|
}
|
|
|
|
}
|
2024-06-28 14:10:32 -04:00
|
|
|
(
|
|
|
|
GenericArgsCtor {
|
|
|
|
args: Default::default(),
|
|
|
|
constraints: &[],
|
|
|
|
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
|
|
|
|
span: *span,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
2024-06-28 11:49:16 -04:00
|
|
|
}
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-12 11:36:07 +02:00
|
|
|
(
|
|
|
|
GenericArgsCtor {
|
|
|
|
args: Default::default(),
|
2024-05-27 23:53:46 +02:00
|
|
|
constraints: &[],
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses::No,
|
2021-05-12 11:36:07 +02:00
|
|
|
span: path_span.shrink_to_hi(),
|
|
|
|
},
|
|
|
|
param_mode == ParamMode::Optional,
|
|
|
|
)
|
2020-01-06 06:28:43 +01:00
|
|
|
};
|
|
|
|
|
2020-10-26 20:02:06 -04:00
|
|
|
let has_lifetimes =
|
|
|
|
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
|
2023-03-16 22:00:08 +00:00
|
|
|
|
|
|
|
// FIXME(return_type_notation): Is this correct? I think so.
|
|
|
|
if generic_args.parenthesized != hir::GenericArgsParentheses::ParenSugar && !has_lifetimes {
|
2022-04-07 20:54:13 +02:00
|
|
|
self.maybe_insert_elided_lifetimes_in_path(
|
|
|
|
path_span,
|
|
|
|
segment.id,
|
|
|
|
segment.ident.span,
|
|
|
|
&mut generic_args,
|
|
|
|
);
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let res = self.expect_full_res(segment.id);
|
2022-08-30 16:54:42 +10:00
|
|
|
let hir_id = self.lower_node_id(segment.id);
|
2020-01-06 06:28:43 +01:00
|
|
|
debug!(
|
|
|
|
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
|
2022-08-30 16:54:42 +10:00
|
|
|
segment.ident, segment.id, hir_id,
|
2020-01-06 06:28:43 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
hir::PathSegment {
|
2021-08-21 00:29:08 +03:00
|
|
|
ident: self.lower_ident(segment.ident),
|
2022-08-30 16:54:42 +10:00
|
|
|
hir_id,
|
2022-08-30 15:10:28 +10:00
|
|
|
res: self.lower_res(res),
|
2020-01-06 06:28:43 +01:00
|
|
|
infer_args,
|
2021-05-12 11:36:07 +02:00
|
|
|
args: if generic_args.is_empty() && generic_args.span.is_empty() {
|
2020-01-06 06:28:43 +01:00
|
|
|
None
|
|
|
|
} else {
|
2021-08-21 00:29:08 +03:00
|
|
|
Some(generic_args.into_generic_args(self))
|
2020-01-06 06:28:43 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 20:54:13 +02:00
|
|
|
fn maybe_insert_elided_lifetimes_in_path(
|
|
|
|
&mut self,
|
|
|
|
path_span: Span,
|
|
|
|
segment_id: NodeId,
|
|
|
|
segment_ident_span: Span,
|
|
|
|
generic_args: &mut GenericArgsCtor<'hir>,
|
|
|
|
) {
|
|
|
|
let (start, end) = match self.resolver.get_lifetime_res(segment_id) {
|
|
|
|
Some(LifetimeRes::ElidedAnchor { start, end }) => (start, end),
|
|
|
|
None => return,
|
2023-12-14 23:31:36 -05:00
|
|
|
Some(res) => {
|
|
|
|
span_bug!(path_span, "expected an elided lifetime to insert. found {res:?}")
|
|
|
|
}
|
2022-04-07 20:54:13 +02:00
|
|
|
};
|
|
|
|
let expected_lifetimes = end.as_usize() - start.as_usize();
|
|
|
|
debug!(expected_lifetimes);
|
|
|
|
|
|
|
|
// Note: these spans are used for diagnostics when they can't be inferred.
|
|
|
|
// See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
|
|
|
|
let elided_lifetime_span = if generic_args.span.is_empty() {
|
|
|
|
// If there are no brackets, use the identifier span.
|
|
|
|
// HACK: we use find_ancestor_inside to properly suggest elided spans in paths
|
|
|
|
// originating from macros, since the segment's span might be from a macro arg.
|
|
|
|
segment_ident_span.find_ancestor_inside(path_span).unwrap_or(path_span)
|
|
|
|
} else if generic_args.is_empty() {
|
|
|
|
// If there are brackets, but not generic arguments, then use the opening bracket
|
|
|
|
generic_args.span.with_hi(generic_args.span.lo() + BytePos(1))
|
|
|
|
} else {
|
|
|
|
// Else use an empty span right after the opening bracket.
|
|
|
|
generic_args.span.with_lo(generic_args.span.lo() + BytePos(1)).shrink_to_lo()
|
|
|
|
};
|
|
|
|
|
|
|
|
generic_args.args.insert_many(
|
|
|
|
0,
|
|
|
|
(start.as_u32()..end.as_u32()).map(|i| {
|
|
|
|
let id = NodeId::from_u32(i);
|
2025-03-25 08:21:28 +11:00
|
|
|
let l = self.lower_lifetime_anon_in_path(id, elided_lifetime_span);
|
2022-04-07 20:54:13 +02:00
|
|
|
GenericArg::Lifetime(l)
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:24:54 +01:00
|
|
|
pub(crate) fn lower_angle_bracketed_parameter_data(
|
2020-01-06 06:28:43 +01:00
|
|
|
&mut self,
|
|
|
|
data: &AngleBracketedArgs,
|
|
|
|
param_mode: ParamMode,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2020-01-06 06:28:43 +01:00
|
|
|
) -> (GenericArgsCtor<'hir>, bool) {
|
2020-03-22 04:40:05 +01:00
|
|
|
let has_non_lt_args = data.args.iter().any(|arg| match arg {
|
2020-03-27 07:39:10 +01:00
|
|
|
AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_))
|
|
|
|
| AngleBracketedArg::Constraint(_) => false,
|
|
|
|
AngleBracketedArg::Arg(ast::GenericArg::Type(_) | ast::GenericArg::Const(_)) => true,
|
2020-01-06 06:28:43 +01:00
|
|
|
});
|
2020-03-22 04:40:05 +01:00
|
|
|
let args = data
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| match arg {
|
2022-05-31 16:46:15 -03:00
|
|
|
AngleBracketedArg::Arg(arg) => Some(self.lower_generic_arg(arg, itctx)),
|
2020-03-22 04:40:05 +01:00
|
|
|
AngleBracketedArg::Constraint(_) => None,
|
|
|
|
})
|
|
|
|
.collect();
|
2024-05-27 23:53:46 +02:00
|
|
|
let constraints =
|
|
|
|
self.arena.alloc_from_iter(data.args.iter().filter_map(|arg| match arg {
|
|
|
|
AngleBracketedArg::Constraint(c) => {
|
|
|
|
Some(self.lower_assoc_item_constraint(c, itctx))
|
|
|
|
}
|
|
|
|
AngleBracketedArg::Arg(_) => None,
|
|
|
|
}));
|
2023-03-16 22:00:08 +00:00
|
|
|
let ctor = GenericArgsCtor {
|
|
|
|
args,
|
2024-05-27 23:53:46 +02:00
|
|
|
constraints,
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses::No,
|
|
|
|
span: data.span,
|
|
|
|
};
|
2020-03-22 04:40:05 +01:00
|
|
|
(ctor, !has_non_lt_args && param_mode == ParamMode::Optional)
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_parenthesized_parameter_data(
|
|
|
|
&mut self,
|
|
|
|
data: &ParenthesizedArgs,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2025-02-03 06:44:41 +03:00
|
|
|
bound_modifier_allowed_features: Option<Arc<[Symbol]>>,
|
2020-01-06 06:28:43 +01:00
|
|
|
) -> (GenericArgsCtor<'hir>, bool) {
|
|
|
|
// Switch to `PassThrough` mode for anonymous lifetimes; this
|
|
|
|
// means that we permit things like `&Ref<T>`, where `Ref` has
|
|
|
|
// a hidden lifetime parameter. This is needed for backwards
|
|
|
|
// compatibility, even in contexts like an impl header where
|
|
|
|
// we generally don't permit such things (see #51008).
|
2022-05-11 22:49:39 +02:00
|
|
|
let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
|
|
|
|
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| {
|
2024-02-07 19:27:44 +00:00
|
|
|
self.lower_ty_direct(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
|
2022-05-11 22:49:39 +02:00
|
|
|
}));
|
|
|
|
let output_ty = match output {
|
2022-02-02 15:09:44 +03:00
|
|
|
// Only allow `impl Trait` in return position. i.e.:
|
|
|
|
// ```rust
|
|
|
|
// fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
|
|
|
|
// // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
|
|
|
|
// ```
|
2024-03-06 18:44:55 +00:00
|
|
|
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
|
2024-10-09 09:01:57 +02:00
|
|
|
if self.tcx.features().impl_trait_in_fn_trait_return() {
|
2023-11-21 20:07:32 +01:00
|
|
|
self.lower_ty(ty, itctx)
|
2022-12-06 17:53:50 -08:00
|
|
|
} else {
|
|
|
|
self.lower_ty(
|
2023-11-21 20:07:32 +01:00
|
|
|
ty,
|
2024-02-07 19:27:44 +00:00
|
|
|
ImplTraitContext::FeatureGated(
|
2022-12-06 17:53:50 -08:00
|
|
|
ImplTraitPosition::FnTraitReturn,
|
|
|
|
sym::impl_trait_in_fn_trait_return,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2022-02-02 15:09:44 +03:00
|
|
|
}
|
2022-09-14 17:39:52 -03:00
|
|
|
FnRetTy::Ty(ty) => {
|
2024-02-07 19:27:44 +00:00
|
|
|
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))
|
2022-09-14 17:39:52 -03:00
|
|
|
}
|
2022-05-11 22:49:39 +02:00
|
|
|
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
|
|
|
|
};
|
2025-01-11 19:12:36 +00:00
|
|
|
let args = smallvec![GenericArg::Type(
|
|
|
|
self.arena.alloc(self.ty_tup(*inputs_span, inputs)).try_as_ambig_ty().unwrap()
|
|
|
|
)];
|
2024-01-26 17:00:28 +00:00
|
|
|
|
|
|
|
// If we have a bound like `async Fn() -> T`, make sure that we mark the
|
|
|
|
// `Output = T` associated type bound with the right feature gates.
|
|
|
|
let mut output_span = output_ty.span;
|
|
|
|
if let Some(bound_modifier_allowed_features) = bound_modifier_allowed_features {
|
|
|
|
output_span = self.mark_span_with_reason(
|
|
|
|
DesugaringKind::BoundModifier,
|
|
|
|
output_span,
|
|
|
|
Some(bound_modifier_allowed_features),
|
|
|
|
);
|
|
|
|
}
|
2024-05-27 23:53:46 +02:00
|
|
|
let constraint = self.assoc_ty_binding(sym::Output, output_span, output_ty);
|
2024-01-26 17:00:28 +00:00
|
|
|
|
2022-05-11 22:49:39 +02:00
|
|
|
(
|
|
|
|
GenericArgsCtor {
|
|
|
|
args,
|
2024-05-27 23:53:46 +02:00
|
|
|
constraints: arena_vec![self; constraint],
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses::ParenSugar,
|
2022-05-11 22:49:39 +02:00
|
|
|
span: data.inputs_span,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|
2020-01-06 06:48:51 +01:00
|
|
|
|
2024-05-27 23:53:46 +02:00
|
|
|
/// An associated type binding (i.e., associated type equality constraint).
|
2023-11-29 12:07:43 -08:00
|
|
|
pub(crate) fn assoc_ty_binding(
|
2020-01-06 06:48:51 +01:00
|
|
|
&mut self,
|
2023-12-04 13:43:38 -08:00
|
|
|
assoc_ty_name: rustc_span::Symbol,
|
2020-01-06 06:48:51 +01:00
|
|
|
span: Span,
|
|
|
|
ty: &'hir hir::Ty<'hir>,
|
2024-05-27 23:53:46 +02:00
|
|
|
) -> hir::AssocItemConstraint<'hir> {
|
2023-12-04 13:43:38 -08:00
|
|
|
let ident = Ident::with_dummy_span(assoc_ty_name);
|
2024-05-27 23:53:46 +02:00
|
|
|
let kind = hir::AssocItemConstraintKind::Equality { term: ty.into() };
|
2020-11-30 09:24:54 +01:00
|
|
|
let args = arena_vec![self;];
|
2024-05-27 23:53:46 +02:00
|
|
|
let constraints = arena_vec![self;];
|
2021-05-12 11:36:07 +02:00
|
|
|
let gen_args = self.arena.alloc(hir::GenericArgs {
|
|
|
|
args,
|
2024-05-27 23:53:46 +02:00
|
|
|
constraints,
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses::No,
|
2021-05-12 11:36:07 +02:00
|
|
|
span_ext: DUMMY_SP,
|
|
|
|
});
|
2024-05-27 23:53:46 +02:00
|
|
|
hir::AssocItemConstraint {
|
2021-08-21 00:29:08 +03:00
|
|
|
hir_id: self.next_id(),
|
|
|
|
gen_args,
|
|
|
|
span: self.lower_span(span),
|
|
|
|
ident,
|
|
|
|
kind,
|
|
|
|
}
|
2020-01-06 06:48:51 +01:00
|
|
|
}
|
2024-01-26 17:00:28 +00:00
|
|
|
|
|
|
|
/// When a bound is annotated with `async`, it signals to lowering that the trait
|
|
|
|
/// that the bound refers to should be mapped to the "async" flavor of the trait.
|
|
|
|
///
|
|
|
|
/// This only needs to be done until we unify `AsyncFn` and `Fn` traits into one
|
|
|
|
/// that is generic over `async`ness, if that's ever possible, or modify the
|
|
|
|
/// lowering of `async Fn()` bounds to desugar to another trait like `LendingFn`.
|
2024-07-23 19:10:22 -04:00
|
|
|
fn map_trait_to_async_trait(&self, def_id: DefId) -> Option<DefId> {
|
2024-01-26 17:00:28 +00:00
|
|
|
let lang_items = self.tcx.lang_items();
|
|
|
|
if Some(def_id) == lang_items.fn_trait() {
|
2024-07-23 19:10:22 -04:00
|
|
|
lang_items.async_fn_trait()
|
2024-01-26 17:00:28 +00:00
|
|
|
} else if Some(def_id) == lang_items.fn_mut_trait() {
|
2024-07-23 19:10:22 -04:00
|
|
|
lang_items.async_fn_mut_trait()
|
2024-01-26 17:00:28 +00:00
|
|
|
} else if Some(def_id) == lang_items.fn_once_trait() {
|
2024-07-23 19:10:22 -04:00
|
|
|
lang_items.async_fn_once_trait()
|
2024-01-26 17:00:28 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 06:28:43 +01:00
|
|
|
}
|