Skip to content

Commit 250c71b

Browse files
committed
Make AST lowering a query.
1 parent 43bb31b commit 250c71b

File tree

14 files changed

+111
-164
lines changed

14 files changed

+111
-164
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,6 +3869,7 @@ name = "rustc_hir"
38693869
version = "0.0.0"
38703870
dependencies = [
38713871
"odht",
3872+
"rustc_arena",
38723873
"rustc_ast",
38733874
"rustc_data_structures",
38743875
"rustc_error_messages",

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::ResolverAstLoweringExt;
22
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
3-
use super::{LoweringContext, ParamMode};
4-
use crate::{Arena, FnDeclKind};
3+
use super::{FnDeclKind, LoweringContext, ParamMode};
54

65
use rustc_ast::ptr::P;
76
use rustc_ast::visit::AssocCtxt;
@@ -12,12 +11,9 @@ use rustc_errors::struct_span_err;
1211
use rustc_hir as hir;
1312
use rustc_hir::def::{DefKind, Res};
1413
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
15-
use rustc_hir::definitions::Definitions;
1614
use rustc_hir::PredicateOrigin;
1715
use rustc_index::vec::{Idx, IndexVec};
18-
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
19-
use rustc_session::cstore::CrateStoreDyn;
20-
use rustc_session::Session;
16+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
2117
use rustc_span::source_map::DesugaringKind;
2218
use rustc_span::symbol::{kw, sym, Ident};
2319
use rustc_span::Span;
@@ -27,12 +23,8 @@ use smallvec::{smallvec, SmallVec};
2723
use std::iter;
2824

2925
pub(super) struct ItemLowerer<'a, 'hir> {
30-
pub(super) sess: &'a Session,
31-
pub(super) definitions: &'a mut Definitions,
32-
pub(super) cstore: &'a CrateStoreDyn,
33-
pub(super) resolutions: &'a ResolverOutputs,
26+
pub(super) tcx: TyCtxt<'hir>,
3427
pub(super) resolver: &'a mut ResolverAstLowering,
35-
pub(super) arena: &'hir Arena<'hir>,
3628
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
3729
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
3830
}
@@ -65,12 +57,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
6557
) {
6658
let mut lctx = LoweringContext {
6759
// Pseudo-globals.
68-
sess: &self.sess,
69-
definitions: self.definitions,
70-
cstore: self.cstore,
71-
resolutions: self.resolutions,
60+
tcx: self.tcx,
61+
sess: &self.tcx.sess,
7262
resolver: self.resolver,
73-
arena: self.arena,
63+
arena: self.tcx.hir_arena,
7464

7565
// HirId handling.
7666
bodies: Vec::new(),
@@ -145,7 +135,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
145135
let def_id = self.resolver.node_id_to_def_id[&item.id];
146136

147137
let parent_id = {
148-
let parent = self.definitions.def_key(def_id).parent;
138+
let parent = self.tcx.hir().def_key(def_id).parent;
149139
let local_def_index = parent.unwrap();
150140
LocalDefId { local_def_index }
151141
};

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ use rustc_errors::{struct_span_err, Applicability};
5353
use rustc_hir as hir;
5454
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
56-
use rustc_hir::definitions::{DefPathData, Definitions};
56+
use rustc_hir::definitions::DefPathData;
5757
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
5858
use rustc_index::vec::{Idx, IndexVec};
59-
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
60-
use rustc_query_system::ich::StableHashingContext;
61-
use rustc_session::cstore::CrateStoreDyn;
59+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6260
use rustc_session::parse::feature_err;
6361
use rustc_session::Session;
6462
use rustc_span::hygiene::MacroKind;
@@ -83,19 +81,13 @@ mod item;
8381
mod pat;
8482
mod path;
8583

86-
rustc_hir::arena_types!(rustc_arena::declare_arena);
87-
88-
struct LoweringContext<'a, 'hir: 'a> {
89-
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
90-
sess: &'a Session,
91-
92-
definitions: &'a mut Definitions,
93-
cstore: &'a CrateStoreDyn,
94-
resolutions: &'a ResolverOutputs,
84+
struct LoweringContext<'a, 'hir> {
85+
tcx: TyCtxt<'hir>,
86+
sess: &'hir Session,
9587
resolver: &'a mut ResolverAstLowering,
9688

9789
/// Used to allocate HIR nodes.
98-
arena: &'hir Arena<'hir>,
90+
arena: &'hir hir::Arena<'hir>,
9991

10092
/// Bodies inside the owner being lowered.
10193
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
@@ -391,61 +383,58 @@ fn index_crate<'a>(
391383
/// Compute the hash for the HIR of the full crate.
392384
/// This hash will then be part of the crate_hash which is stored in the metadata.
393385
fn compute_hir_hash(
394-
sess: &Session,
395-
definitions: &Definitions,
396-
cstore: &CrateStoreDyn,
397-
resolver: &ResolverOutputs,
386+
tcx: TyCtxt<'_>,
398387
owners: &IndexVec<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
399388
) -> Fingerprint {
400389
let mut hir_body_nodes: Vec<_> = owners
401390
.iter_enumerated()
402391
.filter_map(|(def_id, info)| {
403392
let info = info.as_owner()?;
404-
let def_path_hash = definitions.def_path_hash(def_id);
393+
let def_path_hash = tcx.hir().def_path_hash(def_id);
405394
Some((def_path_hash, info))
406395
})
407396
.collect();
408397
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
409398

410-
let mut stable_hasher = StableHasher::new();
411-
let mut hcx = StableHashingContext::new(sess, definitions, cstore, &resolver.source_span);
412-
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
413-
stable_hasher.finish()
399+
tcx.with_stable_hashing_context(|mut hcx| {
400+
let mut stable_hasher = StableHasher::new();
401+
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
402+
stable_hasher.finish()
403+
})
414404
}
415405

416-
pub fn lower_crate<'hir>(
417-
sess: &Session,
418-
krate: &Crate,
419-
definitions: &mut Definitions,
420-
cstore: &CrateStoreDyn,
421-
resolutions: &ResolverOutputs,
422-
mut resolver: ResolverAstLowering,
423-
arena: &'hir Arena<'hir>,
424-
) -> &'hir hir::Crate<'hir> {
425-
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
406+
pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> {
407+
let sess = tcx.sess;
408+
let krate = tcx.untracked_crate.steal();
409+
let mut resolver = tcx.resolver_for_lowering(()).steal();
426410

427-
let ast_index = index_crate(&resolver.node_id_to_def_id, krate);
428-
429-
let mut owners =
430-
IndexVec::from_fn_n(|_| hir::MaybeOwner::Phantom, definitions.def_index_count());
411+
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
412+
let mut owners = IndexVec::from_fn_n(
413+
|_| hir::MaybeOwner::Phantom,
414+
tcx.definitions_untracked().def_index_count(),
415+
);
431416

432417
for def_id in ast_index.indices() {
433418
item::ItemLowerer {
434-
sess,
435-
definitions,
436-
cstore,
437-
resolutions,
419+
tcx,
438420
resolver: &mut resolver,
439-
arena,
440421
ast_index: &ast_index,
441422
owners: &mut owners,
442423
}
443424
.lower_node(def_id);
444425
}
445426

446-
let hir_hash = compute_hir_hash(sess, definitions, cstore, resolutions, &owners);
447-
let krate = hir::Crate { owners, hir_hash };
448-
arena.alloc(krate)
427+
// Drop AST to free memory
428+
std::mem::drop(ast_index);
429+
sess.time("drop_ast", || std::mem::drop(krate));
430+
431+
// Discard hygiene data, which isn't required after lowering to HIR.
432+
if !sess.opts.debugging_opts.keep_hygiene_data {
433+
rustc_span::hygiene::clear_syntax_context_map();
434+
}
435+
436+
let hir_hash = compute_hir_hash(tcx, &owners);
437+
hir::Crate { owners, hir_hash }
449438
}
450439

451440
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -464,15 +453,6 @@ enum ParenthesizedGenericArgs {
464453
}
465454

466455
impl<'a, 'hir> LoweringContext<'a, 'hir> {
467-
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
468-
StableHashingContext::new(
469-
self.sess,
470-
self.definitions,
471-
self.cstore,
472-
&self.resolutions.source_span,
473-
)
474-
}
475-
476456
fn create_def(
477457
&mut self,
478458
parent: LocalDefId,
@@ -484,10 +464,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
484464
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
485465
node_id,
486466
data,
487-
self.definitions.def_key(self.local_def_id(node_id)),
467+
self.tcx.hir().def_key(self.local_def_id(node_id)),
488468
);
489469

490-
let def_id = self.definitions.create_def(parent, data);
470+
let def_id = self.tcx.create_def(parent, data);
491471

492472
// Some things for which we allocate `LocalDefId`s don't correspond to
493473
// anything in the AST, so they don't have a `NodeId`. For these cases
@@ -578,7 +558,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
578558
bodies.sort_by_key(|(k, _)| *k);
579559
let bodies = SortedMap::from_presorted_elements(bodies);
580560
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
581-
let (nodes, parenting) = index::index_hir(self.sess, self.definitions, node, &bodies);
561+
let (nodes, parenting) =
562+
index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies);
582563
let nodes = hir::OwnerNodes {
583564
hash_including_bodies,
584565
hash_without_bodies,
@@ -587,10 +568,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
587568
local_id_to_def_id,
588569
};
589570
let attrs = {
590-
let mut hcx = self.create_stable_hashing_context();
591-
let mut stable_hasher = StableHasher::new();
592-
attrs.hash_stable(&mut hcx, &mut stable_hasher);
593-
let hash = stable_hasher.finish();
571+
let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
572+
let mut stable_hasher = StableHasher::new();
573+
attrs.hash_stable(&mut hcx, &mut stable_hasher);
574+
stable_hasher.finish()
575+
});
594576
hir::AttributeMap { map: attrs, hash }
595577
};
596578

@@ -604,18 +586,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604586
node: hir::OwnerNode<'hir>,
605587
bodies: &SortedMap<hir::ItemLocalId, &'hir hir::Body<'hir>>,
606588
) -> (Fingerprint, Fingerprint) {
607-
let mut hcx = self.create_stable_hashing_context();
608-
let mut stable_hasher = StableHasher::new();
609-
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| {
610-
node.hash_stable(hcx, &mut stable_hasher)
611-
});
612-
let hash_including_bodies = stable_hasher.finish();
613-
let mut stable_hasher = StableHasher::new();
614-
hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| {
615-
node.hash_stable(hcx, &mut stable_hasher)
616-
});
617-
let hash_without_bodies = stable_hasher.finish();
618-
(hash_including_bodies, hash_without_bodies)
589+
self.tcx.with_stable_hashing_context(|mut hcx| {
590+
let mut stable_hasher = StableHasher::new();
591+
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| {
592+
node.hash_stable(hcx, &mut stable_hasher)
593+
});
594+
let hash_including_bodies = stable_hasher.finish();
595+
let mut stable_hasher = StableHasher::new();
596+
hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| {
597+
node.hash_stable(hcx, &mut stable_hasher)
598+
});
599+
let hash_without_bodies = stable_hasher.finish();
600+
(hash_including_bodies, hash_without_bodies)
601+
})
619602
}
620603

621604
/// This method allocates a new `HirId` for the given `NodeId` and stores it in
@@ -703,12 +686,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
703686
span: Span,
704687
allow_internal_unstable: Option<Lrc<[Symbol]>>,
705688
) -> Span {
706-
span.mark_with_reason(
707-
allow_internal_unstable,
708-
reason,
709-
self.sess.edition(),
710-
self.create_stable_hashing_context(),
711-
)
689+
self.tcx.with_stable_hashing_context(|hcx| {
690+
span.mark_with_reason(allow_internal_unstable, reason, self.sess.edition(), hcx)
691+
})
712692
}
713693

714694
/// Intercept all spans entering HIR.

compiler/rustc_hir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
doctest = false
88

99
[dependencies]
10+
rustc_arena = { path = "../rustc_arena" }
1011
rustc_target = { path = "../rustc_target" }
1112
rustc_macros = { path = "../rustc_macros" }
1213
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_hir/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! arena_types {
99
// HIR types
1010
[] hir_krate: rustc_hir::Crate<'tcx>,
1111
[] arm: rustc_hir::Arm<'tcx>,
12-
[] asm_operand: (rustc_hir::InlineAsmOperand<'tcx>, Span),
12+
[] asm_operand: (rustc_hir::InlineAsmOperand<'tcx>, rustc_span::Span),
1313
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
1414
[] attribute: rustc_ast::Attribute,
1515
[] block: rustc_hir::Block<'tcx>,

compiler/rustc_hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ extern crate rustc_macros;
1818
#[macro_use]
1919
extern crate rustc_data_structures;
2020

21+
extern crate self as rustc_hir;
22+
2123
mod arena;
2224
pub mod def;
2325
pub mod def_path_hash_map;
@@ -41,3 +43,5 @@ pub use hir_id::*;
4143
pub use lang_items::{LangItem, LanguageItems};
4244
pub use stable_hash_impls::HashStableContext;
4345
pub use target::{MethodKind, Target};
46+
47+
arena_types!(rustc_arena::declare_arena);

0 commit comments

Comments
 (0)