@@ -5,6 +5,7 @@ use super::ImplTraitTypeIdVisitor;
55use super::LoweringContext;
66use super::ParamMode;
77
8+ use crate::arena::Arena;
89use crate::hir;
910use crate::hir::def::{DefKind, Res};
1011use crate::hir::def_id::DefId;
@@ -225,7 +226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
225226 pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
226227 let mut ident = i.ident;
227228 let mut vis = self.lower_visibility(&i.vis, None);
228- let attrs = self.lower_attrs_arena (&i.attrs);
229+ let attrs = self.lower_attrs (&i.attrs);
229230
230231 if let ItemKind::MacroDef(ref def) = i.kind {
231232 if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) {
@@ -506,7 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
506507 let new_id = this.lower_node_id(new_node_id);
507508 let res = this.lower_res(res);
508509 let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
509- let kind = hir::ItemKind::Use(this.arena.alloc( path) , hir::UseKind::Single);
510+ let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
510511 let vis = this.rebuild_vis(&vis);
511512
512513 this.insert_item(hir::Item {
@@ -521,15 +522,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
521522 }
522523
523524 let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit, None);
524- let path = self.arena.alloc(path);
525525 hir::ItemKind::Use(path, hir::UseKind::Single)
526526 }
527527 UseTreeKind::Glob => {
528- let path = self.arena.alloc(self.lower_path(
529- id,
530- &Path { segments, span: path.span },
531- ParamMode::Explicit,
532- ));
528+ let path =
529+ self.lower_path(id, &Path { segments, span: path.span }, ParamMode::Explicit);
533530 hir::ItemKind::Use(path, hir::UseKind::Glob)
534531 }
535532 UseTreeKind::Nested(ref trees) => {
@@ -617,7 +614,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
617614 let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err);
618615 let res = self.lower_res(res);
619616 let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit, None);
620- let path = self.arena.alloc(path);
621617 hir::ItemKind::Use(path, hir::UseKind::ListStem)
622618 }
623619 }
@@ -626,7 +622,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
626622 /// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
627623 /// many times in the HIR tree; for each occurrence, we need to assign distinct
628624 /// `NodeId`s. (See, e.g., #56128.)
629- fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> hir::Path<'hir> {
625+ fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> &'hir hir::Path<'hir> {
630626 debug!("rebuild_use_path(path = {:?})", path);
631627 let segments =
632628 self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
@@ -636,7 +632,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
636632 args: None,
637633 infer_args: seg.infer_args,
638634 }));
639- hir::Path { span: path.span, res: path.res, segments }
635+ self.arena.alloc( hir::Path { span: path.span, res: path.res, segments })
640636 }
641637
642638 fn rebuild_vis(&mut self, vis: &hir::Visibility<'hir>) -> hir::Visibility<'hir> {
@@ -646,7 +642,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
646642 hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
647643 hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
648644 hir::VisibilityKind::Restricted {
649- path: self.arena.alloc(self. rebuild_use_path(path) ),
645+ path: self.rebuild_use_path(path),
650646 hir_id: self.next_id(),
651647 }
652648 }
@@ -659,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
659655 hir::ForeignItem {
660656 hir_id: self.lower_node_id(i.id),
661657 ident: i.ident,
662- attrs: self.lower_attrs_arena (&i.attrs),
658+ attrs: self.lower_attrs (&i.attrs),
663659 kind: match i.kind {
664660 ForeignItemKind::Fn(ref fdec, ref generics) => {
665661 let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
@@ -674,7 +670,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
674670 )
675671 },
676672 );
677- let fn_args = self.arena.alloc_from_iter(fn_args.into_iter());
678673
679674 hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
680675 }
@@ -703,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
703698
704699 fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
705700 hir::Variant {
706- attrs: self.lower_attrs_arena (&v.attrs),
701+ attrs: self.lower_attrs (&v.attrs),
707702 data: self.lower_variant_data(&v.data),
708703 disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
709704 id: self.lower_node_id(v.id),
@@ -751,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
751746 },
752747 vis: self.lower_visibility(&f.vis, None),
753748 ty,
754- attrs: self.lower_attrs_arena (&f.attrs),
749+ attrs: self.lower_attrs (&f.attrs),
755750 }
756751 }
757752
@@ -772,7 +767,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
772767 }
773768 AssocItemKind::Fn(ref sig, None) => {
774769 let names = self.lower_fn_params_to_names(&sig.decl);
775- let names: &[Ident] = self.arena.alloc_from_iter(names.into_iter());
776770 let (generics, sig) =
777771 self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
778772 (generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
@@ -799,7 +793,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
799793 hir::TraitItem {
800794 hir_id: self.lower_node_id(i.id),
801795 ident: i.ident,
802- attrs: self.lower_attrs_arena (&i.attrs),
796+ attrs: self.lower_attrs (&i.attrs),
803797 generics,
804798 kind,
805799 span: i.span,
@@ -886,7 +880,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
886880 hir::ImplItem {
887881 hir_id: self.lower_node_id(i.id),
888882 ident: i.ident,
889- attrs: self.lower_attrs_arena (&i.attrs),
883+ attrs: self.lower_attrs (&i.attrs),
890884 generics,
891885 vis: self.lower_visibility(&i.vis, None),
892886 defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
@@ -945,12 +939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
945939 let res = self.expect_full_res(id);
946940 let res = self.lower_res(res);
947941 hir::VisibilityKind::Restricted {
948- path: self.arena.alloc(self.lower_path_extra(
949- res,
950- path,
951- ParamMode::Explicit,
952- explicit_owner,
953- )),
942+ path: self.lower_path_extra(res, path, ParamMode::Explicit, explicit_owner),
954943 hir_id: lowered_id,
955944 }
956945 }
@@ -993,7 +982,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
993982
994983 fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
995984 hir::Param {
996- attrs: self.lower_attrs_arena (¶m.attrs),
985+ attrs: self.lower_attrs (¶m.attrs),
997986 hir_id: self.lower_node_id(param.id),
998987 pat: self.lower_pat(¶m.pat),
999988 span: param.span,
@@ -1133,7 +1122,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11331122 let stmt = this.stmt_let_pat(
11341123 stmt_attrs,
11351124 desugared_span,
1136- Some(this.arena.alloc( expr) ),
1125+ Some(expr),
11371126 parameter.pat,
11381127 hir::LocalSource::AsyncFn,
11391128 );
@@ -1163,7 +1152,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11631152 let move_stmt = this.stmt_let_pat(
11641153 AttrVec::new(),
11651154 desugared_span,
1166- Some(this.arena.alloc( move_expr) ),
1155+ Some(move_expr),
11671156 move_pat,
11681157 hir::LocalSource::AsyncFn,
11691158 );
@@ -1174,7 +1163,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11741163 let pattern_stmt = this.stmt_let_pat(
11751164 stmt_attrs,
11761165 desugared_span,
1177- Some(this.arena.alloc( pattern_expr) ),
1166+ Some(pattern_expr),
11781167 parameter.pat,
11791168 hir::LocalSource::AsyncFn,
11801169 );
@@ -1295,11 +1284,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
12951284 }
12961285 }
12971286
1298- pub(super) fn lower_generics (
1287+ pub(super) fn lower_generics_mut (
12991288 &mut self,
13001289 generics: &Generics,
13011290 itctx: ImplTraitContext<'_, 'hir>,
1302- ) -> hir::Generics <'hir> {
1291+ ) -> GenericsCtor <'hir> {
13031292 // Collect `?Trait` bounds in where clause and move them to parameter definitions.
13041293 // FIXME: this could probably be done with less rightward drift. It also looks like two
13051294 // control paths where `report_error` is called are the only paths that advance to after the
@@ -1355,13 +1344,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
13551344 }
13561345 }
13571346
1358- hir::Generics {
1359- params: self.lower_generic_params (&generics.params, &add_bounds, itctx),
1347+ GenericsCtor {
1348+ params: self.lower_generic_params_mut (&generics.params, &add_bounds, itctx).collect( ),
13601349 where_clause: self.lower_where_clause(&generics.where_clause),
13611350 span: generics.span,
13621351 }
13631352 }
13641353
1354+ pub(super) fn lower_generics(
1355+ &mut self,
1356+ generics: &Generics,
1357+ itctx: ImplTraitContext<'_, 'hir>,
1358+ ) -> hir::Generics<'hir> {
1359+ let generics_ctor = self.lower_generics_mut(generics, itctx);
1360+ generics_ctor.into_generics(self.arena)
1361+ }
1362+
13651363 fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause<'hir> {
13661364 self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
13671365 hir::WhereClause {
@@ -1383,13 +1381,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13831381 }) => {
13841382 self.with_in_scope_lifetime_defs(&bound_generic_params, |this| {
13851383 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1386- bound_generic_params: this.arena.alloc_from_iter(
1387- this.lower_generic_params(
1388- bound_generic_params,
1389- &NodeMap::default(),
1390- ImplTraitContext::disallowed(),
1391- )
1392- .into_iter(),
1384+ bound_generic_params: this.lower_generic_params(
1385+ bound_generic_params,
1386+ &NodeMap::default(),
1387+ ImplTraitContext::disallowed(),
13931388 ),
13941389 bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
13951390 bounds: this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| {
@@ -1426,3 +1421,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
14261421 }
14271422 }
14281423}
1424+
1425+ /// Helper struct for delayed construction of Generics.
1426+ pub(super) struct GenericsCtor<'hir> {
1427+ pub(super) params: SmallVec<[hir::GenericParam<'hir>; 4]>,
1428+ where_clause: hir::WhereClause<'hir>,
1429+ span: Span,
1430+ }
1431+
1432+ impl GenericsCtor<'hir> {
1433+ pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> {
1434+ hir::Generics {
1435+ params: arena.alloc_from_iter(self.params),
1436+ where_clause: self.where_clause,
1437+ span: self.span,
1438+ }
1439+ }
1440+ }
0 commit comments