Skip to content

Commit 4dc79f1

Browse files
committed
Use Arena inside hir::Crate.
1 parent fc5deca commit 4dc79f1

File tree

22 files changed

+109
-97
lines changed

22 files changed

+109
-97
lines changed

src/librustc/arena.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ macro_rules! arena_types {
9393
rustc::hir::def_id::CrateNum
9494
>
9595
>,
96-
[few] hir_forest: rustc::hir::map::Forest,
9796
[few] diagnostic_items: rustc_data_structures::fx::FxHashMap<
9897
syntax::symbol::Symbol,
9998
rustc::hir::def_id::DefId,
@@ -123,6 +122,10 @@ macro_rules! arena_types {
123122
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
124123
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
125124
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc::hir::HirId, rustc::hir::Upvar>,
125+
// HIR nodes arenas
126+
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
127+
[] attribute: syntax::ast::Attribute,
128+
[] macro_def: rustc::hir::MacroDef,
126129
], $tcx);
127130
)
128131
}

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ pub trait Visitor<'v>: Sized {
382382
}
383383

384384
/// Walks the contents of a crate. See also `Crate::visit_all_items`.
385-
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
385+
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
386386
visitor.visit_mod(&krate.module, krate.span, CRATE_HIR_ID);
387-
walk_list!(visitor, visit_attribute, &krate.attrs);
388-
walk_list!(visitor, visit_macro_def, &krate.exported_macros);
387+
walk_list!(visitor, visit_attribute, krate.attrs);
388+
walk_list!(visitor, visit_macro_def, krate.exported_macros);
389389
}
390390

391391
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {

src/librustc/hir/lowering.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
mod expr;
3636
mod item;
3737

38+
use crate::arena::Arena;
3839
use crate::dep_graph::DepGraph;
3940
use crate::hir::{self, ParamName};
4041
use crate::hir::HirVec;
@@ -77,7 +78,7 @@ use rustc_error_codes::*;
7778

7879
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
7980

80-
pub struct LoweringContext<'a> {
81+
pub struct LoweringContext<'a, 'hir: 'a> {
8182
crate_root: Option<Symbol>,
8283

8384
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
@@ -90,6 +91,9 @@ pub struct LoweringContext<'a> {
9091
/// librustc is independent of the parser, we use dynamic dispatch here.
9192
nt_to_tokenstream: NtToTokenstream,
9293

94+
/// Used to allocate HIR nodes
95+
arena: &'hir Arena<'hir>,
96+
9397
/// The items being lowered are collected here.
9498
items: BTreeMap<hir::HirId, hir::Item>,
9599

@@ -240,13 +244,14 @@ impl<'a> ImplTraitContext<'a> {
240244
}
241245
}
242246

243-
pub fn lower_crate(
244-
sess: &Session,
245-
dep_graph: &DepGraph,
246-
krate: &Crate,
247-
resolver: &mut dyn Resolver,
247+
pub fn lower_crate<'a, 'hir>(
248+
sess: &'a Session,
249+
dep_graph: &'a DepGraph,
250+
krate: &'a Crate,
251+
resolver: &'a mut dyn Resolver,
248252
nt_to_tokenstream: NtToTokenstream,
249-
) -> hir::Crate {
253+
arena: &'hir Arena<'hir>,
254+
) -> hir::Crate<'hir> {
250255
// We're constructing the HIR here; we don't care what we will
251256
// read, since we haven't even constructed the *input* to
252257
// incr. comp. yet.
@@ -259,6 +264,7 @@ pub fn lower_crate(
259264
sess,
260265
resolver,
261266
nt_to_tokenstream,
267+
arena,
262268
items: BTreeMap::new(),
263269
trait_items: BTreeMap::new(),
264270
impl_items: BTreeMap::new(),
@@ -382,19 +388,19 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
382388
}
383389
}
384390

385-
impl<'a> LoweringContext<'a> {
386-
fn lower_crate(mut self, c: &Crate) -> hir::Crate {
391+
impl<'a, 'hir> LoweringContext<'a, 'hir> {
392+
fn lower_crate(mut self, c: &Crate) -> hir::Crate<'hir> {
387393
/// Full-crate AST visitor that inserts into a fresh
388394
/// `LoweringContext` any information that may be
389395
/// needed from arbitrary locations in the crate,
390396
/// e.g., the number of lifetime generic parameters
391397
/// declared for every type and trait definition.
392-
struct MiscCollector<'tcx, 'interner> {
393-
lctx: &'tcx mut LoweringContext<'interner>,
398+
struct MiscCollector<'tcx, 'lowering, 'hir> {
399+
lctx: &'tcx mut LoweringContext<'lowering, 'hir>,
394400
hir_id_owner: Option<NodeId>,
395401
}
396402

397-
impl MiscCollector<'_, '_> {
403+
impl MiscCollector<'_, '_, '_> {
398404
fn allocate_use_tree_hir_id_counters(
399405
&mut self,
400406
tree: &UseTree,
@@ -434,7 +440,7 @@ impl<'a> LoweringContext<'a> {
434440
}
435441
}
436442

437-
impl<'tcx, 'interner> Visitor<'tcx> for MiscCollector<'tcx, 'interner> {
443+
impl<'tcx, 'lowering, 'hir> Visitor<'tcx> for MiscCollector<'tcx, 'lowering, 'hir> {
438444
fn visit_pat(&mut self, p: &'tcx Pat) {
439445
if let PatKind::Paren(..) | PatKind::Rest = p.kind {
440446
// Doesn't generate a HIR node
@@ -537,7 +543,7 @@ impl<'a> LoweringContext<'a> {
537543
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
538544

539545
let module = self.lower_mod(&c.module);
540-
let attrs = self.lower_attrs(&c.attrs);
546+
let attrs = self.arena.alloc_from_iter(self.lower_attrs(&c.attrs).into_iter());
541547
let body_ids = body_ids(&self.bodies);
542548

543549
self.resolver
@@ -548,8 +554,8 @@ impl<'a> LoweringContext<'a> {
548554
module,
549555
attrs,
550556
span: c.span,
551-
exported_macros: hir::HirVec::from(self.exported_macros),
552-
non_exported_macro_attrs: hir::HirVec::from(self.non_exported_macro_attrs),
557+
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
558+
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
553559
items: self.items,
554560
trait_items: self.trait_items,
555561
impl_items: self.impl_items,
@@ -750,7 +756,7 @@ impl<'a> LoweringContext<'a> {
750756
f: F,
751757
) -> (Vec<hir::GenericParam>, T)
752758
where
753-
F: FnOnce(&mut LoweringContext<'_>) -> (Vec<hir::GenericParam>, T),
759+
F: FnOnce(&mut LoweringContext<'_, '_>) -> (Vec<hir::GenericParam>, T),
754760
{
755761
assert!(!self.is_collecting_in_band_lifetimes);
756762
assert!(self.lifetimes_to_define.is_empty());
@@ -867,7 +873,7 @@ impl<'a> LoweringContext<'a> {
867873
// for them.
868874
fn with_in_scope_lifetime_defs<T, F>(&mut self, params: &[GenericParam], f: F) -> T
869875
where
870-
F: FnOnce(&mut LoweringContext<'_>) -> T,
876+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
871877
{
872878
let old_len = self.in_scope_lifetimes.len();
873879
let lt_def_names = params.iter().filter_map(|param| match param.kind {
@@ -896,7 +902,7 @@ impl<'a> LoweringContext<'a> {
896902
f: F,
897903
) -> (hir::Generics, T)
898904
where
899-
F: FnOnce(&mut LoweringContext<'_>, &mut Vec<hir::GenericParam>) -> T,
905+
F: FnOnce(&mut LoweringContext<'_, '_>, &mut Vec<hir::GenericParam>) -> T,
900906
{
901907
let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(
902908
&generics.params,
@@ -945,7 +951,7 @@ impl<'a> LoweringContext<'a> {
945951

946952
fn with_dyn_type_scope<T, F>(&mut self, in_scope: bool, f: F) -> T
947953
where
948-
F: FnOnce(&mut LoweringContext<'_>) -> T,
954+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
949955
{
950956
let was_in_dyn_type = self.is_in_dyn_type;
951957
self.is_in_dyn_type = in_scope;
@@ -959,7 +965,7 @@ impl<'a> LoweringContext<'a> {
959965

960966
fn with_new_scopes<T, F>(&mut self, f: F) -> T
961967
where
962-
F: FnOnce(&mut LoweringContext<'_>) -> T,
968+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
963969
{
964970
let was_in_loop_condition = self.is_in_loop_condition;
965971
self.is_in_loop_condition = false;
@@ -1446,7 +1452,7 @@ impl<'a> LoweringContext<'a> {
14461452
span: Span,
14471453
fn_def_id: Option<DefId>,
14481454
opaque_ty_node_id: NodeId,
1449-
lower_bounds: impl FnOnce(&mut LoweringContext<'_>) -> hir::GenericBounds,
1455+
lower_bounds: impl FnOnce(&mut LoweringContext<'_, '_>) -> hir::GenericBounds,
14501456
) -> hir::TyKind {
14511457
debug!(
14521458
"lower_opaque_impl_trait(fn_def_id={:?}, opaque_ty_node_id={:?}, span={:?})",
@@ -1563,8 +1569,8 @@ impl<'a> LoweringContext<'a> {
15631569
// This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
15641570
// appear in the bounds, excluding lifetimes that are created within the bounds.
15651571
// E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`.
1566-
struct ImplTraitLifetimeCollector<'r, 'a> {
1567-
context: &'r mut LoweringContext<'a>,
1572+
struct ImplTraitLifetimeCollector<'r, 'a, 'hir> {
1573+
context: &'r mut LoweringContext<'a, 'hir>,
15681574
parent: DefIndex,
15691575
opaque_ty_id: NodeId,
15701576
collect_elided_lifetimes: bool,
@@ -1574,7 +1580,7 @@ impl<'a> LoweringContext<'a> {
15741580
output_lifetime_params: Vec<hir::GenericParam>,
15751581
}
15761582

1577-
impl<'r, 'a, 'v> hir::intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a> {
1583+
impl<'r, 'a, 'v, 'hir> hir::intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a, 'hir> {
15781584
fn nested_visit_map<'this>(
15791585
&'this mut self,
15801586
) -> hir::intravisit::NestedVisitorMap<'this, 'v> {
@@ -2757,8 +2763,9 @@ impl<'a> LoweringContext<'a> {
27572763
let node = match p.kind {
27582764
PatKind::Wild => hir::PatKind::Wild,
27592765
PatKind::Ident(ref binding_mode, ident, ref sub) => {
2760-
let lower_sub = |this: &mut Self| sub.as_ref().map(|x| this.lower_pat(x));
2761-
self.lower_pat_ident(p, binding_mode, ident, lower_sub)
2766+
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
2767+
let node = self.lower_pat_ident(p, binding_mode, ident, lower_sub);
2768+
node
27622769
}
27632770
PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
27642771
PatKind::TupleStruct(ref path, ref pats) => {

src/librustc/hir/lowering/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use syntax::symbol::{sym, Symbol};
1313

1414
use rustc_error_codes::*;
1515

16-
impl LoweringContext<'_> {
16+
impl LoweringContext<'_, '_> {
1717
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> HirVec<hir::Expr> {
1818
exprs.iter().map(|x| self.lower_expr(x)).collect()
1919
}
@@ -473,7 +473,7 @@ impl LoweringContext<'_> {
473473
ret_ty: Option<AstP<Ty>>,
474474
span: Span,
475475
async_gen_kind: hir::AsyncGeneratorKind,
476-
body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
476+
body: impl FnOnce(&mut LoweringContext<'_, '_>) -> hir::Expr,
477477
) -> hir::ExprKind {
478478
let output = match ret_ty {
479479
Some(ty) => FunctionRetTy::Ty(ty),
@@ -909,7 +909,7 @@ impl LoweringContext<'_> {
909909

910910
fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
911911
where
912-
F: FnOnce(&mut LoweringContext<'_>) -> T,
912+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
913913
{
914914
let len = self.catch_scopes.len();
915915
self.catch_scopes.push(catch_id);
@@ -928,7 +928,7 @@ impl LoweringContext<'_> {
928928

929929
fn with_loop_scope<T, F>(&mut self, loop_id: NodeId, f: F) -> T
930930
where
931-
F: FnOnce(&mut LoweringContext<'_>) -> T,
931+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
932932
{
933933
// We're no longer in the base loop's condition; we're in another loop.
934934
let was_in_loop_condition = self.is_in_loop_condition;
@@ -953,7 +953,7 @@ impl LoweringContext<'_> {
953953

954954
fn with_loop_condition_scope<T, F>(&mut self, f: F) -> T
955955
where
956-
F: FnOnce(&mut LoweringContext<'_>) -> T,
956+
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
957957
{
958958
let was_in_loop_condition = self.is_in_loop_condition;
959959
self.is_in_loop_condition = true;

src/librustc/hir/lowering/item.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use syntax_pos::Span;
2424

2525
use rustc_error_codes::*;
2626

27-
pub(super) struct ItemLowerer<'tcx, 'interner> {
28-
pub(super) lctx: &'tcx mut LoweringContext<'interner>,
27+
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
28+
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
2929
}
3030

31-
impl<'tcx, 'interner> ItemLowerer<'tcx, 'interner> {
31+
impl<'a, 'lowering, 'hir> ItemLowerer<'a, 'lowering, 'hir> {
3232
fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
3333
where
3434
F: FnOnce(&mut Self),
@@ -44,8 +44,8 @@ impl<'tcx, 'interner> ItemLowerer<'tcx, 'interner> {
4444
}
4545
}
4646

47-
impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> {
48-
fn visit_mod(&mut self, m: &'tcx Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
47+
impl<'a, 'lowering, 'hir> Visitor<'a> for ItemLowerer<'a, 'lowering, 'hir> {
48+
fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
4949
let hir_id = self.lctx.lower_node_id(n);
5050

5151
self.lctx.modules.insert(hir_id, hir::ModuleItems {
@@ -60,7 +60,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> {
6060
self.lctx.current_module = old;
6161
}
6262

63-
fn visit_item(&mut self, item: &'tcx Item) {
63+
fn visit_item(&mut self, item: &'a Item) {
6464
let mut item_hir_id = None;
6565
self.lctx.with_hir_id_owner(item.id, |lctx| {
6666
lctx.without_in_scope_lifetime_defs(|lctx| {
@@ -85,7 +85,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> {
8585
}
8686
}
8787

88-
fn visit_trait_item(&mut self, item: &'tcx AssocItem) {
88+
fn visit_trait_item(&mut self, item: &'a AssocItem) {
8989
self.lctx.with_hir_id_owner(item.id, |lctx| {
9090
let hir_item = lctx.lower_trait_item(item);
9191
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
@@ -96,7 +96,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> {
9696
visit::walk_trait_item(self, item);
9797
}
9898

99-
fn visit_impl_item(&mut self, item: &'tcx AssocItem) {
99+
fn visit_impl_item(&mut self, item: &'a AssocItem) {
100100
self.lctx.with_hir_id_owner(item.id, |lctx| {
101101
let hir_item = lctx.lower_impl_item(item);
102102
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
@@ -107,7 +107,7 @@ impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> {
107107
}
108108
}
109109

110-
impl LoweringContext<'_> {
110+
impl LoweringContext<'_, '_> {
111111
// Same as the method above, but accepts `hir::GenericParam`s
112112
// instead of `ast::GenericParam`s.
113113
// This should only be used with generics that have already had their
@@ -116,7 +116,7 @@ impl LoweringContext<'_> {
116116
fn with_parent_item_lifetime_defs<T>(
117117
&mut self,
118118
parent_hir_id: hir::HirId,
119-
f: impl FnOnce(&mut LoweringContext<'_>) -> T,
119+
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
120120
) -> T {
121121
let old_len = self.in_scope_lifetimes.len();
122122

@@ -144,7 +144,7 @@ impl LoweringContext<'_> {
144144
// from their surrounding environment.
145145
fn without_in_scope_lifetime_defs<T>(
146146
&mut self,
147-
f: impl FnOnce(&mut LoweringContext<'_>) -> T,
147+
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
148148
) -> T {
149149
let old_in_scope_lifetimes = std::mem::replace(&mut self.in_scope_lifetimes, vec![]);
150150

@@ -1055,7 +1055,7 @@ impl LoweringContext<'_> {
10551055

10561056
fn lower_body(
10571057
&mut self,
1058-
f: impl FnOnce(&mut LoweringContext<'_>) -> (HirVec<hir::Param>, hir::Expr),
1058+
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> (HirVec<hir::Param>, hir::Expr),
10591059
) -> hir::BodyId {
10601060
let prev_gen_kind = self.generator_kind.take();
10611061
let (parameters, result) = f(self);
@@ -1076,7 +1076,7 @@ impl LoweringContext<'_> {
10761076
pub(super) fn lower_fn_body(
10771077
&mut self,
10781078
decl: &FnDecl,
1079-
body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
1079+
body: impl FnOnce(&mut LoweringContext<'_, '_>) -> hir::Expr,
10801080
) -> hir::BodyId {
10811081
self.lower_body(|this| (
10821082
decl.inputs.iter().map(|x| this.lower_param(x)).collect(),

src/librustc/hir/map/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2222
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
2323
pub(super) struct NodeCollector<'a, 'hir> {
2424
/// The crate
25-
krate: &'hir Crate,
25+
krate: &'hir Crate<'hir>,
2626

2727
/// Source map
2828
source_map: &'a SourceMap,
@@ -99,7 +99,7 @@ where
9999

100100
impl<'a, 'hir> NodeCollector<'a, 'hir> {
101101
pub(super) fn root(sess: &'a Session,
102-
krate: &'hir Crate,
102+
krate: &'hir Crate<'hir>,
103103
dep_graph: &'a DepGraph,
104104
definitions: &'a definitions::Definitions,
105105
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,

0 commit comments

Comments
 (0)