Skip to content

Commit bf2d145

Browse files
committed
Auto merge of #67032 - cjgillot:hirene, r=Zoxc
Allocate HIR on an arena 4/4 This is the fourth and last PR in the series started by #66931, #66936 and #66942. The last commits should compile on their own. The difference with the previous PR is given by cjgillot/rust@hirene-ty...hirene A few more cleanups may be necessary, please tell me. r? @eddyb like the other cc @Zoxc
2 parents a9dd56f + ac8c0f4 commit bf2d145

File tree

21 files changed

+186
-333
lines changed

21 files changed

+186
-333
lines changed

src/librustc/hir/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ pub fn walk_generic_args<'v, V: Visitor<'v>>(
672672
_path_span: Span,
673673
generic_args: &'v GenericArgs<'v>,
674674
) {
675-
walk_list!(visitor, visit_generic_arg, &generic_args.args);
675+
walk_list!(visitor, visit_generic_arg, generic_args.args);
676676
walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
677677
}
678678

@@ -780,7 +780,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
780780
}
781781

782782
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
783-
walk_list!(visitor, visit_generic_param, &generics.params);
783+
walk_list!(visitor, visit_generic_param, generics.params);
784784
walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
785785
}
786786

src/librustc/hir/lowering.rs

+97-85
Large diffs are not rendered by default.

src/librustc/hir/lowering/expr.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
464464
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
465465
hir::Arm {
466466
hir_id: self.next_id(),
467-
attrs: self.lower_attrs_arena(&arm.attrs),
467+
attrs: self.lower_attrs(&arm.attrs),
468468
pat: self.lower_pat(&arm.pat),
469469
guard: match arm.guard {
470470
Some(ref x) => Some(hir::Guard::If(self.lower_expr(x))),
@@ -827,7 +827,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
827827
let is_unit = fields.is_empty();
828828
let struct_path = [sym::ops, path];
829829
let struct_path = self.std_path(span, &struct_path, None, is_unit);
830-
let struct_path = hir::QPath::Resolved(None, self.arena.alloc(struct_path));
830+
let struct_path = hir::QPath::Resolved(None, struct_path);
831831

832832
if is_unit {
833833
hir::ExprKind::Path(struct_path)
@@ -1336,7 +1336,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13361336
assoc_fn_name: &str,
13371337
args: &'hir [hir::Expr<'hir>],
13381338
) -> hir::ExprKind<'hir> {
1339-
let ty_path = self.arena.alloc(self.std_path(span, ty_path_components, None, false));
1339+
let ty_path = self.std_path(span, ty_path_components, None, false);
13401340
let ty =
13411341
self.arena.alloc(self.ty_path(ty_path_id, span, hir::QPath::Resolved(None, ty_path)));
13421342
let fn_seg = self.arena.alloc(hir::PathSegment::from_ident(Ident::from_str(assoc_fn_name)));
@@ -1354,11 +1354,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13541354
attrs: AttrVec,
13551355
) -> hir::Expr<'hir> {
13561356
let path = self.std_path(span, components, params, true);
1357-
self.expr(
1358-
span,
1359-
hir::ExprKind::Path(hir::QPath::Resolved(None, self.arena.alloc(path))),
1360-
attrs,
1361-
)
1357+
self.expr(span, hir::ExprKind::Path(hir::QPath::Resolved(None, path)), attrs)
13621358
}
13631359

13641360
pub(super) fn expr_ident(

src/librustc/hir/lowering/item.rs

+52-40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::ImplTraitTypeIdVisitor;
55
use super::LoweringContext;
66
use super::ParamMode;
77

8+
use crate::arena::Arena;
89
use crate::hir;
910
use crate::hir::def::{DefKind, Res};
1011
use 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(&param.attrs),
985+
attrs: self.lower_attrs(&param.attrs),
997986
hir_id: self.lower_node_id(param.id),
998987
pat: self.lower_pat(&param.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

Comments
 (0)