Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate HIR on an arena 1/4 #66931

Merged
merged 12 commits into from
Dec 22, 2019
Prev Previous commit
Next Next commit
Use Arena inside hir::Body.
  • Loading branch information
cjgillot committed Dec 21, 2019
commit 9694ab9e18f0b6b306dafaaccb8cc33a293302bd
1 change: 1 addition & 0 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ macro_rules! arena_types {
[] foreign_item: rustc::hir::ForeignItem<$tcx>,
[] impl_item_ref: rustc::hir::ImplItemRef,
[] macro_def: rustc::hir::MacroDef<$tcx>,
[] param: rustc::hir::Param,
[] path: rustc::hir::Path,
[] struct_field: rustc::hir::StructField<$tcx>,
[] trait_item_ref: rustc::hir::TraitItemRef,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub trait Visitor<'v>: Sized {
walk_item(self, i)
}

fn visit_body(&mut self, b: &'v Body) {
fn visit_body(&mut self, b: &'v Body<'v>) {
walk_body(self, b);
}

Expand Down Expand Up @@ -401,8 +401,8 @@ pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hi
}
}

pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
walk_list!(visitor, visit_param, &body.params);
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body<'v>) {
walk_list!(visitor, visit_param, body.params);
visitor.visit_expr(&body.value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct LoweringContext<'a, 'hir: 'a> {

trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem<'hir>>,
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
bodies: BTreeMap<hir::BodyId, hir::Body>,
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
exported_macros: Vec<hir::MacroDef<'hir>>,
non_exported_macro_attrs: Vec<ast::Attribute>,

Expand Down Expand Up @@ -3428,7 +3428,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'hir>>) -> Vec<hir::BodyId> {
// Sorting by span ensures that we get things in order within a
// file, and also puts the files in a sensible order.
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
Expand Down
17 changes: 10 additions & 7 deletions src/librustc/hir/lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::ImplTraitTypeIdVisitor;
use super::AnonymousLifetimeMode;
use super::ParamMode;

use crate::hir::{self, HirVec};
use crate::hir;
use crate::hir::ptr::P;
use crate::hir::def_id::DefId;
use crate::hir::def::{Res, DefKind};
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<'a, 'lowering, 'hir> Visitor<'a> for ItemLowerer<'a, 'lowering, 'hir> {
}
}

impl LoweringContext<'_, 'hir> {
impl<'hir> LoweringContext<'_, 'hir> {
// Same as the method above, but accepts `hir::GenericParam`s
// instead of `ast::GenericParam`s.
// This should only be used with generics that have already had their
Expand Down Expand Up @@ -1052,7 +1052,7 @@ impl LoweringContext<'_, 'hir> {
}
}

fn record_body(&mut self, params: HirVec<hir::Param>, value: hir::Expr) -> hir::BodyId {
fn record_body(&mut self, params: &'hir [hir::Param], value: hir::Expr) -> hir::BodyId {
let body = hir::Body {
generator_kind: self.generator_kind,
params,
Expand All @@ -1065,7 +1065,7 @@ impl LoweringContext<'_, 'hir> {

fn lower_body(
&mut self,
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> (HirVec<hir::Param>, hir::Expr),
f: impl FnOnce(&mut Self) -> (&'hir [hir::Param], hir::Expr),
) -> hir::BodyId {
let prev_gen_kind = self.generator_kind.take();
let (parameters, result) = f(self);
Expand All @@ -1089,7 +1089,9 @@ impl LoweringContext<'_, 'hir> {
body: impl FnOnce(&mut LoweringContext<'_, '_>) -> hir::Expr,
) -> hir::BodyId {
self.lower_body(|this| (
decl.inputs.iter().map(|x| this.lower_param(x)).collect(),
this.arena.alloc_from_iter(
decl.inputs.iter().map(|x| this.lower_param(x))
),
body(this),
))
}
Expand All @@ -1111,7 +1113,7 @@ impl LoweringContext<'_, 'hir> {
}

pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
self.lower_body(|this| (hir_vec![], match expr {
self.lower_body(|this| (&[], match expr {
Some(expr) => this.lower_expr(expr),
None => this.expr_err(span),
}))
Expand Down Expand Up @@ -1299,7 +1301,8 @@ impl LoweringContext<'_, 'hir> {
);
this.expr_block(P(body), AttrVec::new())
});
(HirVec::from(parameters), this.expr(body_span, async_expr, AttrVec::new()))

(this.arena.alloc_from_iter(parameters), this.expr(body_span, async_expr, AttrVec::new()))
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl<'hir> Map<'hir> {
self.forest.krate.impl_item(id)
}

pub fn body(&self, id: BodyId) -> &'hir Body {
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.read(id.hir_id);

// N.B., intentionally bypass `self.forest.krate()` so that we
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ pub struct Crate<'hir> {

pub trait_items: BTreeMap<TraitItemId, TraitItem<'hir>>,
pub impl_items: BTreeMap<ImplItemId, ImplItem<'hir>>,
pub bodies: BTreeMap<BodyId, Body>,
pub bodies: BTreeMap<BodyId, Body<'hir>>,
pub trait_impls: BTreeMap<DefId, Vec<HirId>>,

/// A list of the body ids written out in the order in which they
Expand All @@ -787,7 +787,7 @@ impl Crate<'hir> {
&self.impl_items[&id]
}

pub fn body(&self, id: BodyId) -> &Body {
pub fn body(&self, id: BodyId) -> &Body<'hir> {
&self.bodies[&id]
}
}
Expand Down Expand Up @@ -1353,13 +1353,13 @@ pub struct BodyId {
/// All bodies have an **owner**, which can be accessed via the HIR
/// map using `body_owner_def_id()`.
#[derive(RustcEncodable, RustcDecodable, Debug)]
pub struct Body {
pub params: HirVec<Param>,
pub struct Body<'hir> {
pub params: &'hir [Param],
pub value: Expr,
pub generator_kind: Option<GeneratorKind>,
}

impl Body {
impl Body<'hir> {
pub fn id(&self) -> BodyId {
BodyId {
hir_id: self.value.hir_id,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct BodyResolver<'tcx>(&'tcx hir::Crate<'tcx>);
impl<'tcx> BodyResolver<'tcx> {
/// Returns a reference to the `hir::Body` with the given `BodyId`.
/// **Does not do any tracking**; use carefully.
fn body(self, id: hir::BodyId) -> &'tcx hir::Body {
fn body(self, id: hir::BodyId) -> &'tcx hir::Body<'tcx> {
self.0.body(id)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item<'_> {
}
}

impl<'a> HashStable<StableHashingContext<'a>> for hir::Body {
impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let hir::Body {
params,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl<'a, 'tcx> Visitor<'tcx> for FindLocalByTypeVisitor<'a, 'tcx> {
intravisit::walk_local(self, local);
}

fn visit_body(&mut self, body: &'tcx Body) {
for param in &body.params {
fn visit_body(&mut self, body: &'tcx Body<'tcx>) {
for param in body.params {
if let (None, Some(ty)) = (
self.found_arg_pattern,
self.node_matches_type(param.hir_id),
Expand Down Expand Up @@ -113,7 +113,7 @@ fn closure_return_type_suggestion(
span: Span,
err: &mut DiagnosticBuilder<'_>,
output: &FunctionRetTy,
body: &Body,
body: &Body<'_>,
descr: &str,
name: &str,
ret: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ for LateContextAndPass<'a, 'tcx, T> {
});
}

fn visit_body(&mut self, body: &'tcx hir::Body) {
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
lint_callback!(self, check_body, body);
hir_visit::walk_body(self, body);
lint_callback!(self, check_body_post, body);
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ macro_rules! late_lint_methods {
($macro:path, $args:tt, [$hir:tt]) => (
$macro!($args, [$hir], [
fn check_param(a: &$hir hir::Param);
fn check_body(a: &$hir hir::Body);
fn check_body_post(a: &$hir hir::Body);
fn check_body(a: &$hir hir::Body<$hir>);
fn check_body_post(a: &$hir hir::Body<$hir>);
fn check_name(a: Span, b: ast::Name);
fn check_crate(a: &$hir hir::Crate<$hir>);
fn check_crate_post(a: &$hir hir::Crate<$hir>);
Expand All @@ -114,13 +114,13 @@ macro_rules! late_lint_methods {
fn check_fn(
a: hir::intravisit::FnKind<$hir>,
b: &$hir hir::FnDecl,
c: &$hir hir::Body,
c: &$hir hir::Body<$hir>,
d: Span,
e: hir::HirId);
fn check_fn_post(
a: hir::intravisit::FnKind<$hir>,
b: &$hir hir::FnDecl,
c: &$hir hir::Body,
c: &$hir hir::Body<$hir>,
d: Span,
e: hir::HirId
);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ impl<'tcx> ScopeTree {
pub fn yield_in_scope_for_expr(&self,
scope: Scope,
expr_hir_id: hir::HirId,
body: &'tcx hir::Body) -> Option<Span> {
body: &'tcx hir::Body<'tcx>) -> Option<Span> {
self.yield_in_scope(scope).and_then(|YieldData { span, expr_and_pat_count, .. }| {
let mut visitor = ExprLocatorVisitor {
hir_id: expr_hir_id,
Expand Down Expand Up @@ -1362,7 +1362,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
resolve_block(self, b);
}

fn visit_body(&mut self, body: &'tcx hir::Body) {
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
let body_id = body.id();
let owner_id = self.tcx.hir().body_owner(body_id);

Expand All @@ -1387,7 +1387,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {

// The arguments and `self` are parented to the fn.
self.cx.var_parent = self.cx.parent.take();
for param in &body.params {
for param in body.params {
self.visit_pat(&param.pat);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ fn signal_shadowing_problem(tcx: TyCtxt<'_>, name: ast::Name, orig: Original, sh

// Adds all labels in `b` to `ctxt.labels_in_fn`, signalling a warning
// if one of the label shadows a lifetime or another label.
fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body) {
fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) {
struct GatherLabels<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
scope: ScopeRef<'a>,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/nonstandard_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
cx: &LateContext<'_, '_>,
fk: FnKind<'_>,
_: &hir::FnDecl,
_: &hir::Body,
_: &hir::Body<'_>,
_: Span,
id: hir::HirId,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ fn construct_fn<'a, 'tcx, A>(
abi: Abi,
return_ty: Ty<'tcx>,
return_ty_span: Span,
body: &'tcx hir::Body,
body: &'tcx hir::Body<'tcx>,
) -> Body<'tcx>
where
A: Iterator<Item=ArgInfo<'tcx>>
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> {
self.check_patterns(false, &loc.pat);
}

fn visit_body(&mut self, body: &'tcx hir::Body) {
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
intravisit::walk_body(self, body);

for param in &body.params {
for param in body.params {
self.check_irrefutable(&param.pat, "function argument", None);
self.check_patterns(false, &param.pat);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_passes/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum ConstKind {
}

impl ConstKind {
fn for_body(body: &hir::Body, hir_map: &Map<'_>) -> Option<Self> {
fn for_body(body: &hir::Body<'_>, hir_map: &Map<'_>) -> Option<Self> {
let is_const_fn = |id| hir_map.fn_sig_by_hir_id(id).unwrap().header.is_const();

let owner = hir_map.body_owner(body.id());
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
self.recurse_into(kind, |this| hir::intravisit::walk_anon_const(this, anon));
}

fn visit_body(&mut self, body: &'tcx hir::Body) {
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
let kind = ConstKind::for_body(body, self.tcx.hir());
self.recurse_into(kind, |this| hir::intravisit::walk_body(this, body));
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_passes/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn visit_fn<'tcx>(

let body = ir.tcx.hir().body(body_id);

for param in &body.params {
for param in body.params {
let is_shorthand = match param.pat.kind {
rustc::hir::PatKind::Struct(..) => true,
_ => false,
Expand Down Expand Up @@ -1463,8 +1463,8 @@ impl<'tcx> Liveness<'_, 'tcx> {
}
}

fn warn_about_unused_args(&self, body: &hir::Body, entry_ln: LiveNode) {
for p in &body.params {
fn warn_about_unused_args(&self, body: &hir::Body<'_>, entry_ln: LiveNode) {
for p in body.params {
self.check_unused_vars_in_pat(&p.pat, Some(entry_ln), |spans, hir_id, ln, var| {
if self.live_on_entry(ln, var).is_none() {
self.report_dead_assign(hir_id, spans, var, true);
Expand Down
Loading