]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/hir/lowering/item.rs
Retire HirVec.
[rust.git] / src / librustc / hir / lowering / item.rs
index 0157e89c96b04a22d747ed2a550a317160ecf9fc..7174addb15501df0768d4d36ccffcdbeb8d6644b 100644 (file)
@@ -5,10 +5,10 @@
 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;
@@ -226,7 +226,7 @@ fn lower_item_id_use_tree(
     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) {
@@ -258,7 +258,7 @@ fn lower_item_kind(
         id: NodeId,
         ident: &mut Ident,
         attrs: &'hir [Attribute],
-        vis: &mut hir::Visibility,
+        vis: &mut hir::Visibility<'hir>,
         i: &ItemKind,
     ) -> hir::ItemKind<'hir> {
         match *i {
@@ -278,11 +278,7 @@ fn lower_item_kind(
                         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 +289,7 @@ fn lower_item_kind(
                         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);
@@ -334,7 +327,7 @@ fn lower_item_kind(
                 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 {
@@ -430,7 +423,7 @@ fn lower_item_kind(
                     self.lower_defaultness(defaultness, true /* [1] */),
                     generics,
                     trait_ref,
-                    self.arena.alloc(lowered_ty.into_inner()),
+                    lowered_ty,
                     new_impl_items,
                 )
             }
@@ -465,7 +458,7 @@ fn lower_use_tree(
         tree: &UseTree,
         prefix: &Path,
         id: NodeId,
-        vis: &mut hir::Visibility,
+        vis: &mut hir::Visibility<'hir>,
         ident: &mut Ident,
         attrs: &'hir [Attribute],
     ) -> hir::ItemKind<'hir> {
@@ -634,30 +627,27 @@ fn lower_use_tree(
     /// 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(),
                 }
             }
@@ -670,7 +660,7 @@ fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
         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(
@@ -685,14 +675,12 @@ fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'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"),
@@ -715,7 +703,7 @@ fn lower_global_asm(&mut self, ga: &GlobalAsm) -> &'hir hir::GlobalAsm {
 
     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),
@@ -751,8 +739,7 @@ fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::Stru
             );
             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,
@@ -764,7 +751,7 @@ fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::Stru
             },
             vis: self.lower_visibility(&f.vis, None),
             ty,
-            attrs: self.lower_attrs_arena(&f.attrs),
+            attrs: self.lower_attrs(&f.attrs),
         }
     }
 
@@ -775,7 +762,6 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'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(
@@ -797,11 +783,7 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'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()),
@@ -816,7 +798,7 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
         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,
@@ -853,7 +835,6 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'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())),
@@ -888,7 +869,6 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'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) => {
@@ -905,7 +885,7 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
         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] */),
@@ -916,7 +896,7 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
         // [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,
@@ -950,7 +930,7 @@ fn lower_visibility(
         &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),
@@ -964,7 +944,12 @@ fn lower_visibility(
                 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,
                 }
             }
@@ -1007,7 +992,7 @@ fn lower_body(
 
     fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
         hir::Param {
-            attrs: self.lower_attrs_arena(&param.attrs),
+            attrs: self.lower_attrs(&param.attrs),
             hir_id: self.lower_node_id(param.id),
             pat: self.lower_pat(&param.pat),
             span: param.span,
@@ -1253,7 +1238,7 @@ fn lower_method_sig(
         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,
@@ -1309,11 +1294,11 @@ fn lower_asyncness(&mut self, a: IsAsync) -> hir::IsAsync {
         }
     }
 
-    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
@@ -1369,27 +1354,34 @@ pub(super) fn lower_generics(
             }
         }
 
-        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,
@@ -1405,17 +1397,16 @@ fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicat
                             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,
                     })
                 })
@@ -1440,3 +1431,20 @@ fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicat
         }
     }
 }
+
+/// 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,
+        }
+    }
+}