use super::LoweringContext;
use super::ParamMode;
+use crate::arena::Arena;
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;
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
let mut ident = i.ident;
let mut vis = self.lower_visibility(&i.vis, None);
- let attrs = self.lower_attrs_arena(&i.attrs);
+ let attrs = self.lower_attrs(&i.attrs);
if let ItemKind::MacroDef(ref def) = i.kind {
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) {
id: NodeId,
ident: &mut Ident,
attrs: &'hir [Attribute],
- vis: &mut hir::Visibility,
+ vis: &mut hir::Visibility<'hir>,
i: &ItemKind,
) -> hir::ItemKind<'hir> {
match *i {
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(
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);
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 {
self.lower_defaultness(defaultness, true /* [1] */),
generics,
trait_ref,
- self.arena.alloc(lowered_ty.into_inner()),
+ lowered_ty,
new_impl_items,
)
}
tree: &UseTree,
prefix: &Path,
id: NodeId,
- vis: &mut hir::Visibility,
+ vis: &mut hir::Visibility<'hir>,
ident: &mut Ident,
attrs: &'hir [Attribute],
) -> hir::ItemKind<'hir> {
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
/// many times in the HIR tree; for each occurrence, we need to assign distinct
/// `NodeId`s. (See, e.g., #56128.)
- fn rebuild_use_path(&mut self, path: &hir::Path) -> hir::Path {
+ 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 }
}
- fn rebuild_vis(&mut self, vis: &hir::Visibility) -> hir::Visibility {
+ fn rebuild_vis(&mut self, vis: &hir::Visibility<'hir>) -> hir::Visibility<'hir> {
let vis_kind = match vis.node {
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
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(),
}
}
hir::ForeignItem {
hir_id: self.lower_node_id(i.id),
ident: i.ident,
- attrs: self.lower_attrs_arena(&i.attrs),
+ attrs: self.lower_attrs(&i.attrs),
kind: match i.kind {
ForeignItemKind::Fn(ref fdec, ref generics) => {
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
)
},
);
- 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"),
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
hir::Variant {
- attrs: self.lower_attrs_arena(&v.attrs),
+ attrs: self.lower_attrs(&v.attrs),
data: self.lower_variant_data(&v.data),
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
id: self.lower_node_id(v.id),
);
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,
},
vis: self.lower_visibility(&f.vis, None),
ty,
- attrs: self.lower_attrs_arena(&f.attrs),
+ attrs: self.lower_attrs(&f.attrs),
}
}
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(
(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()),
hir::TraitItem {
hir_id: self.lower_node_id(i.id),
ident: i.ident,
- attrs: self.lower_attrs_arena(&i.attrs),
+ attrs: self.lower_attrs(&i.attrs),
generics,
kind,
span: i.span,
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())),
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) => {
hir::ImplItem {
hir_id: self.lower_node_id(i.id),
ident: i.ident,
- attrs: self.lower_attrs_arena(&i.attrs),
+ attrs: self.lower_attrs(&i.attrs),
generics,
vis: self.lower_visibility(&i.vis, None),
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
// [1] since `default impl` is not yet implemented, this is always true in impls
}
- fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
+ fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef<'hir> {
hir::ImplItemRef {
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
ident: i.ident,
&mut self,
v: &Visibility,
explicit_owner: Option<NodeId>,
- ) -> hir::Visibility {
+ ) -> hir::Visibility<'hir> {
let node = match v.node {
VisibilityKind::Public => hir::VisibilityKind::Public,
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
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,
}
}
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
hir::Param {
- attrs: self.lower_attrs_arena(¶m.attrs),
+ attrs: self.lower_attrs(¶m.attrs),
hir_id: self.lower_node_id(param.id),
pat: self.lower_pat(¶m.pat),
span: param.span,
fn_def_id: DefId,
impl_trait_return_allow: bool,
is_async: Option<NodeId>,
- ) -> (hir::Generics, hir::FnSig) {
+ ) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
let header = self.lower_fn_header(sig.header);
let (generics, decl) = self.add_in_band_defs(
generics,
}
}
- pub(super) fn lower_generics(
+ pub(super) fn lower_generics_mut(
&mut self,
generics: &Generics,
- itctx: ImplTraitContext<'_>,
- ) -> hir::Generics {
+ itctx: ImplTraitContext<'_, 'hir>,
+ ) -> GenericsCtor<'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
// control paths where `report_error` is called are the only paths that advance to after the
}
}
- hir::Generics {
- params: self.lower_generic_params(&generics.params, &add_bounds, itctx),
+ GenericsCtor {
+ params: self.lower_generic_params_mut(&generics.params, &add_bounds, itctx),
where_clause: self.lower_where_clause(&generics.where_clause),
span: generics.span,
}
}
- fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause {
+ pub(super) fn lower_generics(
+ &mut self,
+ generics: &Generics,
+ itctx: ImplTraitContext<'_, 'hir>,
+ ) -> hir::Generics<'hir> {
+ let generics_ctor = self.lower_generics_mut(generics, itctx);
+ generics_ctor.into_generics(self.arena)
+ }
+
+ 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,
}
})
}
- fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate {
+ fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
match *pred {
WherePredicate::BoundPredicate(WhereBoundPredicate {
ref bound_generic_params,
ImplTraitContext::disallowed(),
),
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,
})
})
}
}
}
+
+/// Helper struct for delayed construction of Generics.
+pub(super) struct GenericsCtor<'hir> {
+ pub(super) params: Vec<hir::GenericParam<'hir>>,
+ where_clause: hir::WhereClause<'hir>,
+ span: Span,
+}
+
+impl GenericsCtor<'hir> {
+ pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> {
+ hir::Generics {
+ params: arena.alloc_from_iter(self.params),
+ where_clause: self.where_clause,
+ span: self.span,
+ }
+ }
+}