mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Lowering for hir::Ty.
This commit is contained in:
parent
5865d563ea
commit
c737c07021
@ -128,7 +128,12 @@ macro_rules! arena_types {
|
||||
[] arm: rustc::hir::Arm<$tcx>,
|
||||
[] attribute: syntax::ast::Attribute,
|
||||
[] block: rustc::hir::Block<$tcx>,
|
||||
[] bare_fn_ty: rustc::hir::BareFnTy<$tcx>,
|
||||
[few] global_asm: rustc::hir::GlobalAsm,
|
||||
[] generic_arg: rustc::hir::GenericArg<$tcx>,
|
||||
[] generic_args: rustc::hir::GenericArgs<$tcx>,
|
||||
[] generic_bound: rustc::hir::GenericBound<$tcx>,
|
||||
[] generic_param: rustc::hir::GenericParam<$tcx>,
|
||||
[] expr: rustc::hir::Expr<$tcx>,
|
||||
[] field: rustc::hir::Field<$tcx>,
|
||||
[] field_pat: rustc::hir::FieldPat<$tcx>,
|
||||
@ -142,12 +147,15 @@ macro_rules! arena_types {
|
||||
[] pat: rustc::hir::Pat<$tcx>,
|
||||
[] path: rustc::hir::Path<$tcx>,
|
||||
[] path_segment: rustc::hir::PathSegment<$tcx>,
|
||||
[] poly_trait_ref: rustc::hir::PolyTraitRef<$tcx>,
|
||||
[] qpath: rustc::hir::QPath<$tcx>,
|
||||
[] stmt: rustc::hir::Stmt<$tcx>,
|
||||
[] struct_field: rustc::hir::StructField<$tcx>,
|
||||
[] trait_item_ref: rustc::hir::TraitItemRef,
|
||||
[] ty: rustc::hir::Ty<$tcx>,
|
||||
[] type_binding: rustc::hir::TypeBinding<$tcx>,
|
||||
[] variant: rustc::hir::Variant<$tcx>,
|
||||
[] where_predicate: rustc::hir::WherePredicate<$tcx>,
|
||||
], $tcx);
|
||||
)
|
||||
}
|
||||
|
@ -206,13 +206,13 @@ type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
|
||||
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
|
||||
/// and if so, what meaning it has.
|
||||
#[derive(Debug)]
|
||||
enum ImplTraitContext<'a> {
|
||||
enum ImplTraitContext<'b, 'a> {
|
||||
/// Treat `impl Trait` as shorthand for a new universal generic parameter.
|
||||
/// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
|
||||
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
|
||||
///
|
||||
/// Newly generated parameters should be inserted into the given `Vec`.
|
||||
Universal(&'a mut Vec<hir::GenericParam<'a>>),
|
||||
Universal(&'b mut Vec<hir::GenericParam<'a>>),
|
||||
|
||||
/// Treat `impl Trait` as shorthand for a new opaque type.
|
||||
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
|
||||
@ -237,13 +237,13 @@ enum ImplTraitPosition {
|
||||
Other,
|
||||
}
|
||||
|
||||
impl<'a> ImplTraitContext<'a> {
|
||||
impl<'b, 'a> ImplTraitContext<'b, 'a> {
|
||||
#[inline]
|
||||
fn disallowed() -> Self {
|
||||
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
|
||||
}
|
||||
|
||||
fn reborrow(&'b mut self) -> ImplTraitContext<'b> {
|
||||
fn reborrow(&'c mut self) -> ImplTraitContext<'c, 'a> {
|
||||
use self::ImplTraitContext::*;
|
||||
match self {
|
||||
Universal(params) => Universal(params),
|
||||
@ -741,7 +741,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
f: F,
|
||||
) -> (Vec<hir::GenericParam<'hir>>, T)
|
||||
where
|
||||
F: FnOnce(&mut LoweringContext<'_, '_>) -> (Vec<hir::GenericParam<'hir>>, T),
|
||||
F: FnOnce(&mut Self) -> (Vec<hir::GenericParam<'hir>>, T),
|
||||
{
|
||||
assert!(!self.is_collecting_in_band_lifetimes);
|
||||
assert!(self.lifetimes_to_define.is_empty());
|
||||
@ -796,8 +796,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
hir::GenericParam {
|
||||
hir_id: self.lower_node_id(node_id),
|
||||
name: hir_name,
|
||||
attrs: hir_vec![],
|
||||
bounds: hir_vec![],
|
||||
attrs: &[],
|
||||
bounds: &[],
|
||||
span,
|
||||
pure_wrt_drop: false,
|
||||
kind: hir::GenericParamKind::Lifetime { kind },
|
||||
@ -847,7 +847,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// for them.
|
||||
fn with_in_scope_lifetime_defs<T, F>(&mut self, params: &[GenericParam], f: F) -> T
|
||||
where
|
||||
F: FnOnce(&mut LoweringContext<'_, 'hir>) -> T,
|
||||
F: FnOnce(&mut Self) -> T,
|
||||
{
|
||||
let old_len = self.in_scope_lifetimes.len();
|
||||
let lt_def_names = params.iter().filter_map(|param| match param.kind {
|
||||
@ -876,7 +876,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
f: F,
|
||||
) -> (hir::Generics<'hir>, T)
|
||||
where
|
||||
F: FnOnce(&mut LoweringContext<'_, '_>, &mut Vec<hir::GenericParam<'hir>>) -> T,
|
||||
F: FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
|
||||
{
|
||||
let (in_band_defs, (mut lowered_generics, res)) =
|
||||
self.with_in_scope_lifetime_defs(&generics.params, |this| {
|
||||
@ -897,7 +897,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
});
|
||||
|
||||
let mut lowered_params: Vec<_> =
|
||||
lowered_generics.params.into_iter().chain(in_band_defs).collect();
|
||||
lowered_generics.params.into_iter().chain(in_band_defs.into_iter()).collect();
|
||||
|
||||
// FIXME(const_generics): the compiler doesn't always cope with
|
||||
// unsorted generic parameters at the moment, so we make sure
|
||||
@ -916,7 +916,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
fn with_dyn_type_scope<T, F>(&mut self, in_scope: bool, f: F) -> T
|
||||
where
|
||||
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
|
||||
F: FnOnce(&mut Self) -> T,
|
||||
{
|
||||
let was_in_dyn_type = self.is_in_dyn_type;
|
||||
self.is_in_dyn_type = in_scope;
|
||||
@ -1025,7 +1025,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn lower_assoc_ty_constraint(
|
||||
&mut self,
|
||||
constraint: &AssocTyConstraint,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::TypeBinding<'hir> {
|
||||
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
|
||||
|
||||
@ -1122,7 +1122,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn lower_generic_arg(
|
||||
&mut self,
|
||||
arg: &ast::GenericArg,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::GenericArg<'hir> {
|
||||
match arg {
|
||||
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(<)),
|
||||
@ -1178,8 +1178,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
}
|
||||
}
|
||||
|
||||
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_>) -> P<hir::Ty<'hir>> {
|
||||
P(self.lower_ty_direct(t, itctx))
|
||||
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_, 'hir>) -> &'hir hir::Ty<'hir> {
|
||||
self.arena.alloc(self.lower_ty_direct(t, itctx))
|
||||
}
|
||||
|
||||
fn lower_path_ty(
|
||||
@ -1188,7 +1188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
qself: &Option<QSelf>,
|
||||
path: &Path,
|
||||
param_mode: ParamMode,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::Ty<'hir> {
|
||||
let id = self.lower_node_id(t.id);
|
||||
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
|
||||
@ -1203,11 +1203,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
hir::Ty { hir_id: self.next_id(), kind, span }
|
||||
}
|
||||
|
||||
fn ty_tup(&mut self, span: Span, tys: HirVec<hir::Ty<'hir>>) -> hir::Ty<'hir> {
|
||||
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
|
||||
self.ty(span, hir::TyKind::Tup(tys))
|
||||
}
|
||||
|
||||
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_>) -> hir::Ty<'hir> {
|
||||
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_, 'hir>) -> hir::Ty<'hir> {
|
||||
let kind = match t.kind {
|
||||
TyKind::Infer => hir::TyKind::Infer,
|
||||
TyKind::Err => hir::TyKind::Err,
|
||||
@ -1223,23 +1223,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
}
|
||||
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(&f.generic_params, |this| {
|
||||
this.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
|
||||
hir::TyKind::BareFn(P(hir::BareFnTy {
|
||||
generic_params: this.lower_generic_params(
|
||||
&f.generic_params,
|
||||
&NodeMap::default(),
|
||||
ImplTraitContext::disallowed(),
|
||||
),
|
||||
unsafety: f.unsafety,
|
||||
abi: this.lower_extern(f.ext),
|
||||
decl: this.lower_fn_decl(&f.decl, None, false, None),
|
||||
param_names: this.lower_fn_params_to_names(&f.decl),
|
||||
}))
|
||||
hir::TyKind::BareFn(
|
||||
this.arena.alloc(hir::BareFnTy {
|
||||
generic_params: this.arena.alloc_from_iter(
|
||||
this.lower_generic_params(
|
||||
&f.generic_params,
|
||||
&NodeMap::default(),
|
||||
ImplTraitContext::disallowed(),
|
||||
)
|
||||
.into_iter(),
|
||||
),
|
||||
unsafety: f.unsafety,
|
||||
abi: this.lower_extern(f.ext),
|
||||
decl: this.lower_fn_decl(&f.decl, None, false, None),
|
||||
param_names: this.arena.alloc_from_iter(
|
||||
this.lower_fn_params_to_names(&f.decl).into_iter(),
|
||||
),
|
||||
}),
|
||||
)
|
||||
})
|
||||
}),
|
||||
TyKind::Never => hir::TyKind::Never,
|
||||
TyKind::Tup(ref tys) => hir::TyKind::Tup(
|
||||
tys.iter().map(|ty| self.lower_ty_direct(ty, itctx.reborrow())).collect(),
|
||||
),
|
||||
TyKind::Tup(ref tys) => {
|
||||
hir::TyKind::Tup(self.arena.alloc_from_iter(
|
||||
tys.iter().map(|ty| self.lower_ty_direct(ty, itctx.reborrow())),
|
||||
))
|
||||
}
|
||||
TyKind::Paren(ref ty) => {
|
||||
return self.lower_ty_direct(ty, itctx);
|
||||
}
|
||||
@ -1251,11 +1260,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
let res = self.lower_res(res);
|
||||
hir::TyKind::Path(hir::QPath::Resolved(
|
||||
None,
|
||||
P(hir::Path {
|
||||
self.arena.alloc(hir::Path {
|
||||
res,
|
||||
segments: hir_vec![hir::PathSegment::from_ident(Ident::with_dummy_span(
|
||||
kw::SelfUpper
|
||||
))],
|
||||
segments: arena_vec![self; hir::PathSegment::from_ident(
|
||||
Ident::with_dummy_span(kw::SelfUpper)
|
||||
)],
|
||||
span: t.span,
|
||||
}),
|
||||
))
|
||||
@ -1267,21 +1276,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
TyKind::TraitObject(ref bounds, kind) => {
|
||||
let mut lifetime_bound = None;
|
||||
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
||||
let bounds = bounds
|
||||
.iter()
|
||||
.filter_map(|bound| match *bound {
|
||||
GenericBound::Trait(ref ty, TraitBoundModifier::None) => {
|
||||
Some(this.lower_poly_trait_ref(ty, itctx.reborrow()))
|
||||
}
|
||||
GenericBound::Trait(_, TraitBoundModifier::Maybe) => None,
|
||||
GenericBound::Outlives(ref lifetime) => {
|
||||
if lifetime_bound.is_none() {
|
||||
lifetime_bound = Some(this.lower_lifetime(lifetime));
|
||||
let bounds =
|
||||
this.arena.alloc_from_iter(bounds.iter().filter_map(
|
||||
|bound| match *bound {
|
||||
GenericBound::Trait(ref ty, TraitBoundModifier::None) => {
|
||||
Some(this.lower_poly_trait_ref(ty, itctx.reborrow()))
|
||||
}
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
GenericBound::Trait(_, TraitBoundModifier::Maybe) => None,
|
||||
GenericBound::Outlives(ref lifetime) => {
|
||||
if lifetime_bound.is_none() {
|
||||
lifetime_bound = Some(this.lower_lifetime(lifetime));
|
||||
}
|
||||
None
|
||||
}
|
||||
},
|
||||
));
|
||||
let lifetime_bound =
|
||||
lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
|
||||
(bounds, lifetime_bound)
|
||||
@ -1314,7 +1323,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
hir_id: self.lower_node_id(def_node_id),
|
||||
name: ParamName::Plain(ident),
|
||||
pure_wrt_drop: false,
|
||||
attrs: hir_vec![],
|
||||
attrs: &[],
|
||||
bounds: hir_bounds,
|
||||
span,
|
||||
kind: hir::GenericParamKind::Type {
|
||||
@ -1325,10 +1334,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
hir::TyKind::Path(hir::QPath::Resolved(
|
||||
None,
|
||||
P(hir::Path {
|
||||
self.arena.alloc(hir::Path {
|
||||
span,
|
||||
res: Res::Def(DefKind::TyParam, DefId::local(def_index)),
|
||||
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
||||
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
|
||||
}),
|
||||
))
|
||||
}
|
||||
@ -1376,7 +1385,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
span: Span,
|
||||
fn_def_id: Option<DefId>,
|
||||
opaque_ty_node_id: NodeId,
|
||||
lower_bounds: impl FnOnce(&mut LoweringContext<'_, '_>) -> hir::GenericBounds<'hir>,
|
||||
lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
|
||||
) -> hir::TyKind<'hir> {
|
||||
debug!(
|
||||
"lower_opaque_impl_trait(fn_def_id={:?}, opaque_ty_node_id={:?}, span={:?})",
|
||||
@ -1411,7 +1420,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
let opaque_ty_item = hir::OpaqueTy {
|
||||
generics: hir::Generics {
|
||||
params: lifetime_defs,
|
||||
where_clause: hir::WhereClause { predicates: hir_vec![], span },
|
||||
where_clause: hir::WhereClause { predicates: &[], span },
|
||||
span,
|
||||
},
|
||||
bounds: hir_bounds,
|
||||
@ -1461,8 +1470,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
&mut self,
|
||||
opaque_ty_id: NodeId,
|
||||
parent_index: DefIndex,
|
||||
bounds: &hir::GenericBounds<'hir>,
|
||||
) -> (HirVec<hir::GenericArg<'hir>>, HirVec<hir::GenericParam<'hir>>) {
|
||||
bounds: hir::GenericBounds<'hir>,
|
||||
) -> (&'hir [hir::GenericArg<'hir>], HirVec<hir::GenericParam<'hir>>) {
|
||||
debug!(
|
||||
"lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \
|
||||
parent_index={:?}, \
|
||||
@ -1603,8 +1612,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
name,
|
||||
span: lifetime.span,
|
||||
pure_wrt_drop: false,
|
||||
attrs: hir_vec![],
|
||||
bounds: hir_vec![],
|
||||
attrs: &[],
|
||||
bounds: &[],
|
||||
kind: hir::GenericParamKind::Lifetime { kind },
|
||||
});
|
||||
}
|
||||
@ -1626,10 +1635,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
hir::intravisit::walk_param_bound(&mut lifetime_collector, &bound);
|
||||
}
|
||||
|
||||
(
|
||||
lifetime_collector.output_lifetimes.into(),
|
||||
lifetime_collector.output_lifetime_params.into(),
|
||||
)
|
||||
let ImplTraitLifetimeCollector { output_lifetimes, output_lifetime_params, .. } =
|
||||
lifetime_collector;
|
||||
|
||||
(self.arena.alloc_from_iter(output_lifetimes), output_lifetime_params.into())
|
||||
}
|
||||
|
||||
fn lower_qpath(
|
||||
@ -1638,7 +1647,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
qself: &Option<QSelf>,
|
||||
p: &Path,
|
||||
param_mode: ParamMode,
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::QPath<'hir> {
|
||||
let qself_position = qself.as_ref().map(|q| q.position);
|
||||
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx.reborrow()));
|
||||
@ -1647,12 +1656,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
|
||||
|
||||
let proj_start = p.segments.len() - partial_res.unresolved_segments();
|
||||
let path = P(hir::Path {
|
||||
let path = self.arena.alloc(hir::Path {
|
||||
res: self.lower_res(partial_res.base_res()),
|
||||
segments: p.segments[..proj_start]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, segment)| {
|
||||
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
|
||||
@ -1728,8 +1735,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
itctx.reborrow(),
|
||||
None,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
)),
|
||||
span: p.span,
|
||||
});
|
||||
|
||||
@ -1749,7 +1756,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
|
||||
// `<I as Iterator>::Item::default`.
|
||||
let new_id = self.next_id();
|
||||
P(self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path)))
|
||||
self.arena.alloc(self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path)))
|
||||
};
|
||||
|
||||
// Anything after the base path are associated "extensions",
|
||||
@ -1763,7 +1770,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// 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) {
|
||||
let segment = P(self.lower_path_segment(
|
||||
let segment = self.arena.alloc(self.lower_path_segment(
|
||||
p.span,
|
||||
segment,
|
||||
param_mode,
|
||||
@ -1781,7 +1788,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
// Wrap the associated extension in another type node.
|
||||
let new_id = self.next_id();
|
||||
ty = P(self.ty_path(new_id, p.span, qpath));
|
||||
ty = self.arena.alloc(self.ty_path(new_id, p.span, qpath));
|
||||
}
|
||||
|
||||
// We should've returned in the for loop above.
|
||||
@ -1802,21 +1809,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
) -> hir::Path<'hir> {
|
||||
hir::Path {
|
||||
res,
|
||||
segments: p
|
||||
.segments
|
||||
.iter()
|
||||
.map(|segment| {
|
||||
self.lower_path_segment(
|
||||
p.span,
|
||||
segment,
|
||||
param_mode,
|
||||
0,
|
||||
ParenthesizedGenericArgs::Err,
|
||||
ImplTraitContext::disallowed(),
|
||||
explicit_owner,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
segments: self.arena.alloc_from_iter(p.segments.iter().map(|segment| {
|
||||
self.lower_path_segment(
|
||||
p.span,
|
||||
segment,
|
||||
param_mode,
|
||||
0,
|
||||
ParenthesizedGenericArgs::Err,
|
||||
ImplTraitContext::disallowed(),
|
||||
explicit_owner,
|
||||
)
|
||||
})),
|
||||
span: p.span,
|
||||
}
|
||||
}
|
||||
@ -1834,7 +1837,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
param_mode: ParamMode,
|
||||
expected_lifetimes: usize,
|
||||
parenthesized_generic_args: ParenthesizedGenericArgs,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
explicit_owner: Option<NodeId>,
|
||||
) -> hir::PathSegment<'hir> {
|
||||
let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args {
|
||||
@ -1967,20 +1970,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
segment.ident, segment.id, id,
|
||||
);
|
||||
|
||||
hir::PathSegment::new(
|
||||
segment.ident,
|
||||
Some(id),
|
||||
Some(self.lower_res(res)),
|
||||
generic_args,
|
||||
hir::PathSegment {
|
||||
ident: segment.ident,
|
||||
hir_id: Some(id),
|
||||
res: Some(self.lower_res(res)),
|
||||
infer_args,
|
||||
)
|
||||
args: if generic_args.is_empty() { None } else { Some(self.arena.alloc(generic_args)) },
|
||||
}
|
||||
}
|
||||
|
||||
fn lower_angle_bracketed_parameter_data(
|
||||
&mut self,
|
||||
data: &AngleBracketedArgs,
|
||||
param_mode: ParamMode,
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> (hir::GenericArgs<'hir>, bool) {
|
||||
let &AngleBracketedArgs { ref args, ref constraints, .. } = data;
|
||||
let has_non_lt_args = args.iter().any(|arg| match arg {
|
||||
@ -1991,10 +1994,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
(
|
||||
hir::GenericArgs {
|
||||
args: args.iter().map(|a| self.lower_generic_arg(a, itctx.reborrow())).collect(),
|
||||
bindings: constraints
|
||||
.iter()
|
||||
.map(|b| self.lower_assoc_ty_constraint(b, itctx.reborrow()))
|
||||
.collect(),
|
||||
bindings: self.arena.alloc_from_iter(
|
||||
constraints.iter().map(|b| self.lower_assoc_ty_constraint(b, itctx.reborrow())),
|
||||
),
|
||||
parenthesized: false,
|
||||
},
|
||||
!has_non_lt_args && param_mode == ParamMode::Optional,
|
||||
@ -2012,13 +2014,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// we generally don't permit such things (see #51008).
|
||||
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
|
||||
let &ParenthesizedArgs { ref inputs, ref output, span } = data;
|
||||
let inputs = inputs
|
||||
.iter()
|
||||
.map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed()))
|
||||
.collect();
|
||||
let inputs = this.arena.alloc_from_iter(
|
||||
inputs.iter().map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed())),
|
||||
);
|
||||
let output_ty = match output {
|
||||
FunctionRetTy::Ty(ty) => this.lower_ty(&ty, ImplTraitContext::disallowed()),
|
||||
FunctionRetTy::Default(_) => P(this.ty_tup(span, hir::HirVec::new())),
|
||||
FunctionRetTy::Default(_) => this.arena.alloc(this.ty_tup(span, &[])),
|
||||
};
|
||||
let args = hir_vec![GenericArg::Type(this.ty_tup(span, inputs))];
|
||||
let binding = hir::TypeBinding {
|
||||
@ -2027,7 +2028,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
span: output_ty.span,
|
||||
kind: hir::TypeBindingKind::Equality { ty: output_ty },
|
||||
};
|
||||
(hir::GenericArgs { args, bindings: hir_vec![binding], parenthesized: true }, false)
|
||||
(
|
||||
hir::GenericArgs { args, bindings: arena_vec![this; binding], parenthesized: true },
|
||||
false,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@ -2050,7 +2054,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
},
|
||||
)
|
||||
});
|
||||
let ty = ty.map(|ty| &*self.arena.alloc(ty.into_inner()));
|
||||
let init = l.init.as_ref().map(|e| self.lower_expr(e));
|
||||
(
|
||||
hir::Local {
|
||||
@ -2101,7 +2104,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam<'hir>>)>,
|
||||
impl_trait_return_allow: bool,
|
||||
make_ret_async: Option<NodeId>,
|
||||
) -> P<hir::FnDecl<'hir>> {
|
||||
) -> &'hir hir::FnDecl<'hir> {
|
||||
debug!(
|
||||
"lower_fn_decl(\
|
||||
fn_decl: {:?}, \
|
||||
@ -2131,16 +2134,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
if c_variadic {
|
||||
inputs = &inputs[..inputs.len() - 1];
|
||||
}
|
||||
inputs
|
||||
.iter()
|
||||
.map(|param| {
|
||||
if let Some((_, ibty)) = &mut in_band_ty_params {
|
||||
this.lower_ty_direct(¶m.ty, ImplTraitContext::Universal(ibty))
|
||||
} else {
|
||||
this.lower_ty_direct(¶m.ty, ImplTraitContext::disallowed())
|
||||
}
|
||||
})
|
||||
.collect::<HirVec<_>>()
|
||||
this.arena.alloc_from_iter(inputs.iter().map(|param| {
|
||||
if let Some((_, ibty)) = &mut in_band_ty_params {
|
||||
this.lower_ty_direct(¶m.ty, ImplTraitContext::Universal(ibty))
|
||||
} else {
|
||||
this.lower_ty_direct(¶m.ty, ImplTraitContext::disallowed())
|
||||
}
|
||||
}))
|
||||
});
|
||||
|
||||
let output = if let Some(ret_id) = make_ret_async {
|
||||
@ -2161,7 +2161,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
}
|
||||
};
|
||||
|
||||
P(hir::FnDecl {
|
||||
self.arena.alloc(hir::FnDecl {
|
||||
inputs,
|
||||
output,
|
||||
c_variadic,
|
||||
@ -2309,19 +2309,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
let generic_params = lifetime_params
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|(span, hir_name)| {
|
||||
this.lifetime_to_generic_param(span, hir_name, opaque_ty_def_index)
|
||||
this.lifetime_to_generic_param(*span, *hir_name, opaque_ty_def_index)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let opaque_ty_item = hir::OpaqueTy {
|
||||
generics: hir::Generics {
|
||||
params: generic_params,
|
||||
where_clause: hir::WhereClause { predicates: hir_vec![], span },
|
||||
where_clause: hir::WhereClause { predicates: &[], span },
|
||||
span,
|
||||
},
|
||||
bounds: hir_vec![future_bound],
|
||||
bounds: arena_vec![this; future_bound],
|
||||
impl_trait_fn: Some(fn_def_id),
|
||||
origin: hir::OpaqueTyOrigin::AsyncFn,
|
||||
};
|
||||
@ -2360,22 +2359,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
generic_args.extend(lifetime_params[input_lifetimes_count..].iter().map(|&(span, _)| {
|
||||
generic_args.extend(lifetime_params[input_lifetimes_count..].iter().map(|&(span, _)|
|
||||
// Output lifetime like `'_`.
|
||||
GenericArg::Lifetime(hir::Lifetime {
|
||||
hir_id: self.next_id(),
|
||||
span,
|
||||
name: hir::LifetimeName::Implicit,
|
||||
})
|
||||
}));
|
||||
})));
|
||||
let generic_args = self.arena.alloc_from_iter(generic_args);
|
||||
|
||||
// Create the `Foo<...>` reference itself. Note that the `type
|
||||
// Foo = impl Trait` is, internally, created as a child of the
|
||||
// async fn, so the *type parameters* are inherited. It's
|
||||
// only the lifetime parameters that we must supply.
|
||||
let opaque_ty_ref = hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, generic_args.into());
|
||||
let opaque_ty_ref = hir::TyKind::Def(hir::ItemId { id: opaque_ty_id }, generic_args);
|
||||
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
|
||||
hir::FunctionRetTy::Return(P(opaque_ty))
|
||||
hir::FunctionRetTy::Return(self.arena.alloc(opaque_ty))
|
||||
}
|
||||
|
||||
/// Transforms `-> T` into `Future<Output = T>`
|
||||
@ -2388,13 +2387,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// Compute the `T` in `Future<Output = T>` from the return type.
|
||||
let output_ty = match output {
|
||||
FunctionRetTy::Ty(ty) => self.lower_ty(ty, ImplTraitContext::OpaqueTy(Some(fn_def_id))),
|
||||
FunctionRetTy::Default(ret_ty_span) => P(self.ty_tup(*ret_ty_span, hir_vec![])),
|
||||
FunctionRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
|
||||
};
|
||||
|
||||
// "<Output = T>"
|
||||
let future_params = P(hir::GenericArgs {
|
||||
args: hir_vec![],
|
||||
bindings: hir_vec![hir::TypeBinding {
|
||||
let future_params = self.arena.alloc(hir::GenericArgs {
|
||||
args: HirVec::new(),
|
||||
bindings: arena_vec![self; hir::TypeBinding {
|
||||
ident: Ident::with_dummy_span(FN_OUTPUT_NAME),
|
||||
kind: hir::TypeBindingKind::Equality { ty: output_ty },
|
||||
hir_id: self.next_id(),
|
||||
@ -2404,13 +2403,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
});
|
||||
|
||||
// ::std::future::Future<future_params>
|
||||
let future_path =
|
||||
P(self.std_path(span, &[sym::future, sym::Future], Some(future_params), false));
|
||||
let future_path = self.arena.alloc(self.std_path(
|
||||
span,
|
||||
&[sym::future, sym::Future],
|
||||
Some(future_params),
|
||||
false,
|
||||
));
|
||||
|
||||
hir::GenericBound::Trait(
|
||||
hir::PolyTraitRef {
|
||||
trait_ref: hir::TraitRef { path: future_path, hir_ref_id: self.next_id() },
|
||||
bound_generic_params: hir_vec![],
|
||||
bound_generic_params: &[],
|
||||
span,
|
||||
},
|
||||
hir::TraitBoundModifier::None,
|
||||
@ -2420,7 +2423,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn lower_param_bound(
|
||||
&mut self,
|
||||
tpb: &GenericBound,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::GenericBound<'hir> {
|
||||
match *tpb {
|
||||
GenericBound::Trait(ref ty, modifier) => hir::GenericBound::Trait(
|
||||
@ -2472,8 +2475,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
&mut self,
|
||||
params: &[GenericParam],
|
||||
add_bounds: &NodeMap<Vec<GenericBound>>,
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
) -> hir::HirVec<hir::GenericParam<'hir>> {
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> HirVec<hir::GenericParam<'hir>> {
|
||||
params
|
||||
.iter()
|
||||
.map(|param| self.lower_generic_param(param, add_bounds, itctx.reborrow()))
|
||||
@ -2484,11 +2487,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
&mut self,
|
||||
param: &GenericParam,
|
||||
add_bounds: &NodeMap<Vec<GenericBound>>,
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::GenericParam<'hir> {
|
||||
let mut bounds = self
|
||||
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
|
||||
this.lower_param_bounds(¶m.bounds, itctx.reborrow())
|
||||
this.lower_param_bounds_mut(¶m.bounds, itctx.reborrow())
|
||||
});
|
||||
|
||||
let (name, kind) = match param.kind {
|
||||
@ -2524,8 +2527,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
GenericParamKind::Type { ref default, .. } => {
|
||||
let add_bounds = add_bounds.get(¶m.id).map_or(&[][..], |x| &x);
|
||||
if !add_bounds.is_empty() {
|
||||
let params = self.lower_param_bounds(add_bounds, itctx.reborrow()).into_iter();
|
||||
bounds = bounds.into_iter().chain(params).collect();
|
||||
let params = self.lower_param_bounds_mut(add_bounds, itctx.reborrow());
|
||||
bounds.extend(params);
|
||||
}
|
||||
|
||||
let kind = hir::GenericParamKind::Type {
|
||||
@ -2555,8 +2558,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
name,
|
||||
span: param.ident.span,
|
||||
pure_wrt_drop: attr::contains_name(¶m.attrs, sym::may_dangle),
|
||||
attrs: self.lower_attrs(¶m.attrs),
|
||||
bounds,
|
||||
attrs: self.lower_attrs_arena(¶m.attrs),
|
||||
bounds: self.arena.alloc_from_iter(bounds),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
@ -2564,7 +2567,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn lower_trait_ref(
|
||||
&mut self,
|
||||
p: &TraitRef,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::TraitRef<'hir> {
|
||||
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
|
||||
hir::QPath::Resolved(None, path) => path,
|
||||
@ -2576,7 +2579,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
fn lower_poly_trait_ref(
|
||||
&mut self,
|
||||
p: &PolyTraitRef,
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::PolyTraitRef<'hir> {
|
||||
let bound_generic_params = self.lower_generic_params(
|
||||
&p.bound_generic_params,
|
||||
@ -2587,18 +2590,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
this.lower_trait_ref(&p.trait_ref, itctx)
|
||||
});
|
||||
|
||||
hir::PolyTraitRef { bound_generic_params, trait_ref, span: p.span }
|
||||
hir::PolyTraitRef {
|
||||
bound_generic_params: self.arena.alloc_from_iter(bound_generic_params.into_iter()),
|
||||
trait_ref,
|
||||
span: p.span,
|
||||
}
|
||||
}
|
||||
|
||||
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext<'_>) -> hir::MutTy<'hir> {
|
||||
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext<'_, 'hir>) -> hir::MutTy<'hir> {
|
||||
hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
|
||||
}
|
||||
|
||||
fn lower_param_bounds(
|
||||
&mut self,
|
||||
bounds: &[GenericBound],
|
||||
mut itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::GenericBounds<'hir> {
|
||||
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
|
||||
}
|
||||
|
||||
fn lower_param_bounds_mut(
|
||||
&mut self,
|
||||
bounds: &[GenericBound],
|
||||
mut itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> Vec<hir::GenericBound<'hir>> {
|
||||
bounds.iter().map(|bound| self.lower_param_bound(bound, itctx.reborrow())).collect()
|
||||
}
|
||||
|
||||
@ -2836,10 +2851,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
}
|
||||
Some(res) => hir::PatKind::Path(hir::QPath::Resolved(
|
||||
None,
|
||||
P(hir::Path {
|
||||
self.arena.alloc(hir::Path {
|
||||
span: ident.span,
|
||||
res: self.lower_res(res),
|
||||
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
||||
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
|
||||
}),
|
||||
)),
|
||||
}
|
||||
@ -3035,7 +3050,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
subpats: &'hir [&'hir hir::Pat<'hir>],
|
||||
) -> &'hir hir::Pat<'hir> {
|
||||
let path = self.std_path(span, components, None, true);
|
||||
let qpath = hir::QPath::Resolved(None, P(path));
|
||||
let qpath = hir::QPath::Resolved(None, self.arena.alloc(path));
|
||||
let pt = if subpats.is_empty() {
|
||||
hir::PatKind::Path(qpath)
|
||||
} else {
|
||||
@ -3081,7 +3096,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
&mut self,
|
||||
span: Span,
|
||||
components: &[Symbol],
|
||||
params: Option<P<hir::GenericArgs<'hir>>>,
|
||||
params: Option<&'hir hir::GenericArgs<'hir>>,
|
||||
is_value: bool,
|
||||
) -> hir::Path<'hir> {
|
||||
let ns = if is_value { Namespace::ValueNS } else { Namespace::TypeNS };
|
||||
@ -3106,7 +3121,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
hir::Path {
|
||||
span,
|
||||
res: res.map_id(|_| panic!("unexpected `NodeId`")),
|
||||
segments: segments.into(),
|
||||
segments: self.arena.alloc_from_iter(segments),
|
||||
}
|
||||
}
|
||||
|
||||
@ -3122,7 +3137,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
match path.res {
|
||||
Res::Def(DefKind::Trait, _) | Res::Def(DefKind::TraitAlias, _) => {
|
||||
let principal = hir::PolyTraitRef {
|
||||
bound_generic_params: hir::HirVec::new(),
|
||||
bound_generic_params: &[],
|
||||
trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
|
||||
span,
|
||||
};
|
||||
@ -3130,7 +3145,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// The original ID is taken by the `PolyTraitRef`,
|
||||
// so the `Ty` itself needs a different one.
|
||||
hir_id = self.next_id();
|
||||
hir::TyKind::TraitObject(hir_vec![principal], self.elided_dyn_bound(span))
|
||||
hir::TyKind::TraitObject(
|
||||
arena_vec![self; principal],
|
||||
self.elided_dyn_bound(span),
|
||||
)
|
||||
}
|
||||
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
|
||||
use crate::hir;
|
||||
use crate::hir::def::Res;
|
||||
use crate::hir::ptr::P;
|
||||
|
||||
use rustc_data_structures::thin_vec::ThinVec;
|
||||
|
||||
@ -64,12 +63,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
ExprKind::Cast(ref expr, ref ty) => {
|
||||
let expr = self.lower_expr(expr);
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
hir::ExprKind::Cast(expr, self.arena.alloc(ty.into_inner()))
|
||||
hir::ExprKind::Cast(expr, ty)
|
||||
}
|
||||
ExprKind::Type(ref expr, ref ty) => {
|
||||
let expr = self.lower_expr(expr);
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
hir::ExprKind::Type(expr, self.arena.alloc(ty.into_inner()))
|
||||
hir::ExprKind::Type(expr, ty)
|
||||
}
|
||||
ExprKind::AddrOf(k, m, ref ohs) => {
|
||||
let ohs = self.lower_expr(ohs);
|
||||
@ -490,9 +489,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
None => FunctionRetTy::Default(span),
|
||||
};
|
||||
let ast_decl = FnDecl { inputs: vec![], output };
|
||||
let decl = self.arena.alloc(
|
||||
self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None).into_inner(),
|
||||
);
|
||||
let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None);
|
||||
let body_id = self.lower_fn_body(&ast_decl, |this| {
|
||||
this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
|
||||
body(this)
|
||||
@ -670,7 +667,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
) -> hir::ExprKind<'hir> {
|
||||
// Lower outside new scope to preserve `is_in_loop_condition`.
|
||||
let fn_decl = self.lower_fn_decl(decl, None, false, None);
|
||||
let fn_decl = self.arena.alloc(fn_decl.into_inner());
|
||||
|
||||
self.with_new_scopes(move |this| {
|
||||
let prev = this.current_item;
|
||||
@ -733,7 +729,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
// have to conserve the state of being inside a loop condition for the
|
||||
// closure argument types.
|
||||
let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
|
||||
let fn_decl = self.arena.alloc(fn_decl.into_inner());
|
||||
|
||||
self.with_new_scopes(move |this| {
|
||||
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
|
||||
@ -816,7 +811,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
let is_unit = fields.is_empty();
|
||||
let struct_path = [sym::ops, path];
|
||||
let struct_path = self.std_path(span, &struct_path, None, is_unit);
|
||||
let struct_path = hir::QPath::Resolved(None, P(struct_path));
|
||||
let struct_path = hir::QPath::Resolved(None, self.arena.alloc(struct_path));
|
||||
|
||||
if is_unit {
|
||||
hir::ExprKind::Path(struct_path)
|
||||
@ -1325,9 +1320,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
assoc_fn_name: &str,
|
||||
args: &'hir [hir::Expr<'hir>],
|
||||
) -> hir::ExprKind<'hir> {
|
||||
let ty_path = P(self.std_path(span, ty_path_components, None, false));
|
||||
let ty = P(self.ty_path(ty_path_id, span, hir::QPath::Resolved(None, ty_path)));
|
||||
let fn_seg = P(hir::PathSegment::from_ident(Ident::from_str(assoc_fn_name)));
|
||||
let ty_path = self.arena.alloc(self.std_path(span, ty_path_components, None, false));
|
||||
let ty =
|
||||
self.arena.alloc(self.ty_path(ty_path_id, span, hir::QPath::Resolved(None, ty_path)));
|
||||
let fn_seg = self.arena.alloc(hir::PathSegment::from_ident(Ident::from_str(assoc_fn_name)));
|
||||
let fn_path = hir::QPath::TypeRelative(ty, fn_seg);
|
||||
let fn_expr =
|
||||
self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
|
||||
@ -1338,11 +1334,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
&mut self,
|
||||
span: Span,
|
||||
components: &[Symbol],
|
||||
params: Option<P<hir::GenericArgs<'hir>>>,
|
||||
params: Option<&'hir hir::GenericArgs<'hir>>,
|
||||
attrs: AttrVec,
|
||||
) -> hir::Expr<'hir> {
|
||||
let path = self.std_path(span, components, params, true);
|
||||
self.expr(span, hir::ExprKind::Path(hir::QPath::Resolved(None, P(path))), attrs)
|
||||
self.expr(
|
||||
span,
|
||||
hir::ExprKind::Path(hir::QPath::Resolved(None, self.arena.alloc(path))),
|
||||
attrs,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn expr_ident(
|
||||
@ -1372,10 +1372,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
) -> hir::Expr<'hir> {
|
||||
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
|
||||
None,
|
||||
P(hir::Path {
|
||||
self.arena.alloc(hir::Path {
|
||||
span,
|
||||
res: Res::Local(binding),
|
||||
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
||||
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
|
||||
}),
|
||||
));
|
||||
|
||||
|
@ -8,7 +8,6 @@ use super::ParamMode;
|
||||
use crate::hir;
|
||||
use crate::hir::def::{DefKind, Res};
|
||||
use crate::hir::def_id::DefId;
|
||||
use crate::hir::ptr::P;
|
||||
use crate::util::nodemap::NodeMap;
|
||||
|
||||
use rustc_target::spec::abi;
|
||||
@ -278,11 +277,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
|
||||
},
|
||||
);
|
||||
hir::ItemKind::Static(
|
||||
self.arena.alloc(ty.into_inner()),
|
||||
m,
|
||||
self.lower_const_body(span, Some(e)),
|
||||
)
|
||||
hir::ItemKind::Static(ty, m, self.lower_const_body(span, Some(e)))
|
||||
}
|
||||
ItemKind::Const(ref t, ref e) => {
|
||||
let ty = self.lower_ty(
|
||||
@ -293,10 +288,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
|
||||
},
|
||||
);
|
||||
hir::ItemKind::Const(
|
||||
self.arena.alloc(ty.into_inner()),
|
||||
self.lower_const_body(span, Some(e)),
|
||||
)
|
||||
hir::ItemKind::Const(ty, self.lower_const_body(span, Some(e)))
|
||||
}
|
||||
ItemKind::Fn(FnSig { ref decl, header }, ref generics, ref body) => {
|
||||
let fn_def_id = self.resolver.definitions().local_def_id(id);
|
||||
@ -323,7 +315,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
)
|
||||
},
|
||||
);
|
||||
let decl = this.arena.alloc(decl.into_inner());
|
||||
let sig = hir::FnSig { decl, header: this.lower_fn_header(header) };
|
||||
hir::ItemKind::Fn(sig, generics, body_id)
|
||||
})
|
||||
@ -335,7 +326,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
None => {
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
|
||||
hir::ItemKind::TyAlias(self.arena.alloc(ty.into_inner()), generics)
|
||||
hir::ItemKind::TyAlias(ty, generics)
|
||||
}
|
||||
Some(bounds) => {
|
||||
let ty = hir::OpaqueTy {
|
||||
@ -431,7 +422,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
self.lower_defaultness(defaultness, true /* [1] */),
|
||||
generics,
|
||||
trait_ref,
|
||||
self.arena.alloc(lowered_ty.into_inner()),
|
||||
lowered_ty,
|
||||
new_impl_items,
|
||||
)
|
||||
}
|
||||
@ -637,17 +628,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
/// `NodeId`s. (See, e.g., #56128.)
|
||||
fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> hir::Path<'hir> {
|
||||
debug!("rebuild_use_path(path = {:?})", path);
|
||||
let segments = path
|
||||
.segments
|
||||
.iter()
|
||||
.map(|seg| hir::PathSegment {
|
||||
let segments =
|
||||
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
|
||||
ident: seg.ident,
|
||||
hir_id: seg.hir_id.map(|_| self.next_id()),
|
||||
res: seg.res,
|
||||
args: None,
|
||||
infer_args: seg.infer_args,
|
||||
})
|
||||
.collect();
|
||||
}));
|
||||
hir::Path { span: path.span, res: path.res, segments }
|
||||
}
|
||||
|
||||
@ -658,7 +646,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
|
||||
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
|
||||
hir::VisibilityKind::Restricted {
|
||||
path: P(self.rebuild_use_path(path)),
|
||||
path: self.arena.alloc(self.rebuild_use_path(path)),
|
||||
hir_id: self.next_id(),
|
||||
}
|
||||
}
|
||||
@ -686,14 +674,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
)
|
||||
},
|
||||
);
|
||||
let fn_dec = self.arena.alloc(fn_dec.into_inner());
|
||||
let fn_args = self.arena.alloc_from_iter(fn_args.into_iter());
|
||||
|
||||
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
|
||||
}
|
||||
ForeignItemKind::Static(ref t, m) => {
|
||||
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
|
||||
hir::ForeignItemKind::Static(self.arena.alloc(ty.into_inner()), m)
|
||||
hir::ForeignItemKind::Static(ty, m)
|
||||
}
|
||||
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
|
||||
ForeignItemKind::Macro(_) => panic!("macro shouldn't exist here"),
|
||||
@ -752,8 +739,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
);
|
||||
self.arena.alloc(t)
|
||||
} else {
|
||||
let t = self.lower_ty(&f.ty, ImplTraitContext::disallowed());
|
||||
self.arena.alloc(t.into_inner())
|
||||
self.lower_ty(&f.ty, ImplTraitContext::disallowed())
|
||||
};
|
||||
hir::StructField {
|
||||
span: f.span,
|
||||
@ -776,7 +762,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
AssocItemKind::Const(ref ty, ref default) => {
|
||||
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
let ty = self.arena.alloc(ty.into_inner());
|
||||
(
|
||||
generics,
|
||||
hir::TraitItemKind::Const(
|
||||
@ -799,11 +784,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id)))
|
||||
}
|
||||
AssocItemKind::TyAlias(ref bounds, ref default) => {
|
||||
let ty = default.as_ref().map(|x| {
|
||||
&*self
|
||||
.arena
|
||||
.alloc(self.lower_ty(x, ImplTraitContext::disallowed()).into_inner())
|
||||
});
|
||||
let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed()));
|
||||
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
|
||||
let kind = hir::TraitItemKind::Type(
|
||||
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
|
||||
@ -855,7 +836,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
AssocItemKind::Const(ref ty, ref expr) => {
|
||||
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
let ty = self.arena.alloc(ty.into_inner());
|
||||
(
|
||||
generics,
|
||||
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
|
||||
@ -890,7 +870,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
Some(ty) => match ty.kind.opaque_top_hack() {
|
||||
None => {
|
||||
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
|
||||
let ty = self.arena.alloc(ty.into_inner());
|
||||
hir::ImplItemKind::TyAlias(ty)
|
||||
}
|
||||
Some(bs) => {
|
||||
@ -966,7 +945,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
let res = self.expect_full_res(id);
|
||||
let res = self.lower_res(res);
|
||||
hir::VisibilityKind::Restricted {
|
||||
path: P(self.lower_path_extra(res, path, ParamMode::Explicit, explicit_owner)),
|
||||
path: self.arena.alloc(self.lower_path_extra(
|
||||
res,
|
||||
path,
|
||||
ParamMode::Explicit,
|
||||
explicit_owner,
|
||||
)),
|
||||
hir_id: lowered_id,
|
||||
}
|
||||
}
|
||||
@ -1270,7 +1254,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
)
|
||||
},
|
||||
);
|
||||
let decl = self.arena.alloc(decl.into_inner());
|
||||
(generics, hir::FnSig { header, decl })
|
||||
}
|
||||
|
||||
@ -1315,7 +1298,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
pub(super) fn lower_generics(
|
||||
&mut self,
|
||||
generics: &Generics,
|
||||
itctx: ImplTraitContext<'_>,
|
||||
itctx: ImplTraitContext<'_, 'hir>,
|
||||
) -> hir::Generics<'hir> {
|
||||
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
|
||||
// FIXME: this could probably be done with less rightward drift. It also looks like two
|
||||
@ -1382,11 +1365,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause<'hir> {
|
||||
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
|
||||
hir::WhereClause {
|
||||
predicates: wc
|
||||
.predicates
|
||||
.iter()
|
||||
.map(|predicate| this.lower_where_predicate(predicate))
|
||||
.collect(),
|
||||
predicates: this.arena.alloc_from_iter(
|
||||
wc.predicates.iter().map(|predicate| this.lower_where_predicate(predicate)),
|
||||
),
|
||||
span: wc.span,
|
||||
}
|
||||
})
|
||||
@ -1402,23 +1383,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
}) => {
|
||||
self.with_in_scope_lifetime_defs(&bound_generic_params, |this| {
|
||||
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
||||
bound_generic_params: this.lower_generic_params(
|
||||
bound_generic_params,
|
||||
&NodeMap::default(),
|
||||
ImplTraitContext::disallowed(),
|
||||
bound_generic_params: this.arena.alloc_from_iter(
|
||||
this.lower_generic_params(
|
||||
bound_generic_params,
|
||||
&NodeMap::default(),
|
||||
ImplTraitContext::disallowed(),
|
||||
)
|
||||
.into_iter(),
|
||||
),
|
||||
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
|
||||
bounds: bounds
|
||||
.iter()
|
||||
.filter_map(|bound| match *bound {
|
||||
bounds: this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| {
|
||||
match *bound {
|
||||
// Ignore `?Trait` bounds.
|
||||
// They were copied into type parameters already.
|
||||
GenericBound::Trait(_, TraitBoundModifier::Maybe) => None,
|
||||
_ => Some(
|
||||
this.lower_param_bound(bound, ImplTraitContext::disallowed()),
|
||||
),
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
})),
|
||||
span,
|
||||
})
|
||||
})
|
||||
|
@ -364,22 +364,6 @@ impl<'hir> PathSegment<'hir> {
|
||||
PathSegment { ident, hir_id: None, res: None, infer_args: true, args: None }
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
ident: Ident,
|
||||
hir_id: Option<HirId>,
|
||||
res: Option<Res>,
|
||||
args: GenericArgs<'_>,
|
||||
infer_args: bool,
|
||||
) -> Self {
|
||||
PathSegment {
|
||||
ident,
|
||||
hir_id,
|
||||
res,
|
||||
infer_args,
|
||||
args: if args.is_empty() { None } else { Some(P(args)) },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generic_args(&self) -> &GenericArgs<'hir> {
|
||||
if let Some(ref args) = self.args {
|
||||
args
|
||||
|
@ -22,14 +22,6 @@ pub fn P<T: 'static>(value: T) -> P<T> {
|
||||
P { ptr: box value }
|
||||
}
|
||||
|
||||
impl<T: 'static> P<T> {
|
||||
// HACK(eddyb) used by HIR lowering in a few places still.
|
||||
// NOTE: do not make this more public than `pub(super)`.
|
||||
pub(super) fn into_inner(self) -> T {
|
||||
*self.ptr
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Deref for P<T> {
|
||||
type Target = T;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user