Skip to content

Commit

Permalink
Auto merge of #67032 - cjgillot:hirene, r=Zoxc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Dec 31, 2019
2 parents a9dd56f + ac8c0f4 commit bf2d145
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 333 deletions.
4 changes: 2 additions & 2 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ pub fn walk_generic_args<'v, V: Visitor<'v>>(
_path_span: Span,
generic_args: &'v GenericArgs<'v>,
) {
walk_list!(visitor, visit_generic_arg, &generic_args.args);
walk_list!(visitor, visit_generic_arg, generic_args.args);
walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings);
}

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

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

Expand Down
182 changes: 97 additions & 85 deletions src/librustc/hir/lowering.rs

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions src/librustc/hir/lowering/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
hir::Arm {
hir_id: self.next_id(),
attrs: self.lower_attrs_arena(&arm.attrs),
attrs: self.lower_attrs(&arm.attrs),
pat: self.lower_pat(&arm.pat),
guard: match arm.guard {
Some(ref x) => Some(hir::Guard::If(self.lower_expr(x))),
Expand Down Expand Up @@ -827,7 +827,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, self.arena.alloc(struct_path));
let struct_path = hir::QPath::Resolved(None, struct_path);

if is_unit {
hir::ExprKind::Path(struct_path)
Expand Down Expand Up @@ -1336,7 +1336,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
assoc_fn_name: &str,
args: &'hir [hir::Expr<'hir>],
) -> hir::ExprKind<'hir> {
let ty_path = self.arena.alloc(self.std_path(span, ty_path_components, None, false));
let ty_path = 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)));
Expand All @@ -1354,11 +1354,7 @@ impl<'hir> LoweringContext<'_, '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, self.arena.alloc(path))),
attrs,
)
self.expr(span, hir::ExprKind::Path(hir::QPath::Resolved(None, path)), attrs)
}

pub(super) fn expr_ident(
Expand Down
92 changes: 52 additions & 40 deletions src/librustc/hir/lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::ImplTraitTypeIdVisitor;
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;
Expand Down Expand Up @@ -225,7 +226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
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) {
Expand Down Expand Up @@ -506,7 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let new_id = this.lower_node_id(new_node_id);
let res = this.lower_res(res);
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
let kind = hir::ItemKind::Use(this.arena.alloc(path), hir::UseKind::Single);
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
let vis = this.rebuild_vis(&vis);

this.insert_item(hir::Item {
Expand All @@ -521,15 +522,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit, None);
let path = self.arena.alloc(path);
hir::ItemKind::Use(path, hir::UseKind::Single)
}
UseTreeKind::Glob => {
let path = self.arena.alloc(self.lower_path(
id,
&Path { segments, span: path.span },
ParamMode::Explicit,
));
let path =
self.lower_path(id, &Path { segments, span: path.span }, ParamMode::Explicit);
hir::ItemKind::Use(path, hir::UseKind::Glob)
}
UseTreeKind::Nested(ref trees) => {
Expand Down Expand Up @@ -617,7 +614,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err);
let res = self.lower_res(res);
let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit, None);
let path = self.arena.alloc(path);
hir::ItemKind::Use(path, hir::UseKind::ListStem)
}
}
Expand All @@ -626,7 +622,7 @@ impl<'hir> LoweringContext<'_, '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>) -> hir::Path<'hir> {
fn rebuild_use_path(&mut self, path: &hir::Path<'hir>) -> &'hir hir::Path<'hir> {
debug!("rebuild_use_path(path = {:?})", path);
let segments =
self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment {
Expand All @@ -636,7 +632,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
args: None,
infer_args: seg.infer_args,
}));
hir::Path { span: path.span, res: path.res, segments }
self.arena.alloc(hir::Path { span: path.span, res: path.res, segments })
}

fn rebuild_vis(&mut self, vis: &hir::Visibility<'hir>) -> hir::Visibility<'hir> {
Expand All @@ -646,7 +642,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
hir::VisibilityKind::Restricted { ref path, hir_id: _ } => {
hir::VisibilityKind::Restricted {
path: self.arena.alloc(self.rebuild_use_path(path)),
path: self.rebuild_use_path(path),
hir_id: self.next_id(),
}
}
Expand All @@ -659,7 +655,7 @@ impl<'hir> LoweringContext<'_, '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(
Expand All @@ -674,7 +670,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
},
);
let fn_args = self.arena.alloc_from_iter(fn_args.into_iter());

hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
}
Expand Down Expand Up @@ -703,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

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),
Expand Down Expand Up @@ -751,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
},
vis: self.lower_visibility(&f.vis, None),
ty,
attrs: self.lower_attrs_arena(&f.attrs),
attrs: self.lower_attrs(&f.attrs),
}
}

Expand All @@ -772,7 +767,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::Fn(ref sig, None) => {
let names = self.lower_fn_params_to_names(&sig.decl);
let names: &[Ident] = self.arena.alloc_from_iter(names.into_iter());
let (generics, sig) =
self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
Expand All @@ -799,7 +793,7 @@ impl<'hir> LoweringContext<'_, '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,
Expand Down Expand Up @@ -886,7 +880,7 @@ impl<'hir> LoweringContext<'_, '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] */),
Expand Down Expand Up @@ -945,12 +939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let res = self.expect_full_res(id);
let res = self.lower_res(res);
hir::VisibilityKind::Restricted {
path: self.arena.alloc(self.lower_path_extra(
res,
path,
ParamMode::Explicit,
explicit_owner,
)),
path: self.lower_path_extra(res, path, ParamMode::Explicit, explicit_owner),
hir_id: lowered_id,
}
}
Expand Down Expand Up @@ -993,7 +982,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

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,
Expand Down Expand Up @@ -1133,7 +1122,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let stmt = this.stmt_let_pat(
stmt_attrs,
desugared_span,
Some(this.arena.alloc(expr)),
Some(expr),
parameter.pat,
hir::LocalSource::AsyncFn,
);
Expand Down Expand Up @@ -1163,7 +1152,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let move_stmt = this.stmt_let_pat(
AttrVec::new(),
desugared_span,
Some(this.arena.alloc(move_expr)),
Some(move_expr),
move_pat,
hir::LocalSource::AsyncFn,
);
Expand All @@ -1174,7 +1163,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let pattern_stmt = this.stmt_let_pat(
stmt_attrs,
desugared_span,
Some(this.arena.alloc(pattern_expr)),
Some(pattern_expr),
parameter.pat,
hir::LocalSource::AsyncFn,
);
Expand Down Expand Up @@ -1295,11 +1284,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

pub(super) fn lower_generics(
pub(super) fn lower_generics_mut(
&mut self,
generics: &Generics,
itctx: ImplTraitContext<'_, 'hir>,
) -> hir::Generics<'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
Expand Down Expand Up @@ -1355,13 +1344,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

hir::Generics {
params: self.lower_generic_params(&generics.params, &add_bounds, itctx),
GenericsCtor {
params: self.lower_generic_params_mut(&generics.params, &add_bounds, itctx).collect(),
where_clause: self.lower_where_clause(&generics.where_clause),
span: generics.span,
}
}

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 {
Expand All @@ -1383,13 +1381,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
}) => {
self.with_in_scope_lifetime_defs(&bound_generic_params, |this| {
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: this.arena.alloc_from_iter(
this.lower_generic_params(
bound_generic_params,
&NodeMap::default(),
ImplTraitContext::disallowed(),
)
.into_iter(),
bound_generic_params: this.lower_generic_params(
bound_generic_params,
&NodeMap::default(),
ImplTraitContext::disallowed(),
),
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
bounds: this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| {
Expand Down Expand Up @@ -1426,3 +1421,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}
}

/// Helper struct for delayed construction of Generics.
pub(super) struct GenericsCtor<'hir> {
pub(super) params: SmallVec<[hir::GenericParam<'hir>; 4]>,
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,
}
}
}
Loading

0 comments on commit bf2d145

Please sign in to comment.