Consume resolutions for lowering separately.

This commit is contained in:
Camille GILLOT 2022-06-15 19:42:43 +02:00
parent 8ffbd814af
commit ae5959f4ba
5 changed files with 59 additions and 36 deletions

View File

@ -15,7 +15,7 @@
use rustc_hir::definitions::Definitions;
use rustc_hir::PredicateOrigin;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::ResolverOutputs;
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::Session;
use rustc_span::source_map::DesugaringKind;
@ -30,7 +30,8 @@ pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) sess: &'a Session,
pub(super) definitions: &'a mut Definitions,
pub(super) cstore: &'a CrateStoreDyn,
pub(super) resolver: &'a mut ResolverOutputs,
pub(super) resolutions: &'a ResolverOutputs,
pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) arena: &'hir Arena<'hir>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
@ -62,12 +63,12 @@ fn with_lctx(
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) {
let next_node_id = self.resolver.next_node_id;
let mut lctx = LoweringContext {
// Pseudo-globals.
sess: &self.sess,
definitions: self.definitions,
cstore: self.cstore,
resolutions: self.resolutions,
resolver: self.resolver,
arena: self.arena,
@ -80,8 +81,6 @@ fn with_lctx(
node_id_to_local_id: Default::default(),
local_id_to_def_id: SortedMap::new(),
trait_map: Default::default(),
local_node_id_to_def_id: FxHashMap::default(),
next_node_id,
// Lowering state.
catch_scope: None,

View File

@ -56,7 +56,7 @@
use rustc_hir::definitions::{DefPathData, Definitions};
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::ResolverOutputs;
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::parse::feature_err;
@ -91,7 +91,8 @@ struct LoweringContext<'a, 'hir: 'a> {
definitions: &'a mut Definitions,
cstore: &'a CrateStoreDyn,
resolver: &'a mut ResolverOutputs,
resolutions: &'a ResolverOutputs,
resolver: &'a mut ResolverAstLowering,
/// Used to allocate HIR nodes.
arena: &'hir Arena<'hir>,
@ -133,10 +134,6 @@ struct LoweringContext<'a, 'hir: 'a> {
/// NodeIds that are lowered inside the current HIR owner.
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
// The next_node_id is reset for each item.
next_node_id: ast::NodeId,
local_node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
allow_try_trait: Option<Lrc<[Symbol]>>,
allow_gen_future: Option<Lrc<[Symbol]>>,
allow_into_future: Option<Lrc<[Symbol]>>,
@ -174,7 +171,7 @@ trait ResolverAstLoweringExt {
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
}
impl ResolverAstLoweringExt for ResolverOutputs {
impl ResolverAstLoweringExt for ResolverAstLowering {
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
if let ExprKind::Path(None, path) = &expr.kind {
// Don't perform legacy const generics rewriting if the path already
@ -415,7 +412,8 @@ pub fn lower_crate<'hir>(
krate: &Crate,
definitions: &mut Definitions,
cstore: &CrateStoreDyn,
resolver: &mut ResolverOutputs,
resolutions: &ResolverOutputs,
mut resolver: ResolverAstLowering,
arena: &'hir Arena<'hir>,
) -> &'hir hir::Crate<'hir> {
let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering");
@ -430,7 +428,8 @@ pub fn lower_crate<'hir>(
sess,
definitions,
cstore,
resolver,
resolutions,
resolver: &mut resolver,
arena,
ast_index: &ast_index,
owners: &mut owners,
@ -438,7 +437,7 @@ pub fn lower_crate<'hir>(
.lower_node(def_id);
}
let hir_hash = compute_hir_hash(sess, definitions, cstore, resolver, &owners);
let hir_hash = compute_hir_hash(sess, definitions, cstore, resolutions, &owners);
let krate = hir::Crate { owners, hir_hash };
arena.alloc(krate)
}
@ -464,7 +463,7 @@ fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
self.sess,
self.definitions,
self.cstore,
&self.resolver.source_span,
&self.resolutions.source_span,
)
}
@ -489,25 +488,21 @@ fn create_def(
// we don't need a mapping from `NodeId` to `LocalDefId`.
if node_id != ast::DUMMY_NODE_ID {
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
self.local_node_id_to_def_id.insert(node_id, def_id);
self.resolver.node_id_to_def_id.insert(node_id, def_id);
}
def_id
}
fn next_node_id(&mut self) -> NodeId {
let start = self.next_node_id;
let start = self.resolver.next_node_id;
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
self.next_node_id = ast::NodeId::from_u32(next);
self.resolver.next_node_id = ast::NodeId::from_u32(next);
start
}
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
if node <= self.resolver.next_node_id {
self.resolver.node_id_to_def_id.get(&node).copied()
} else {
self.local_node_id_to_def_id.get(&node).copied()
}
self.resolver.node_id_to_def_id.get(&node).copied()
}
fn local_def_id(&self, node: NodeId) -> LocalDefId {

View File

@ -21,7 +21,7 @@
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, ResolverOutputs, TyCtxt};
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
use rustc_mir_build as mir_build;
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
use rustc_passes::{self, hir_stats, layout_test};
@ -139,7 +139,8 @@ pub fn access<F: for<'a> FnOnce(&mut Resolver<'a>) -> R, R>(&mut self, f: F) ->
pub fn to_resolver_outputs(
resolver: Rc<RefCell<BoxedResolver>>,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
) -> (Definitions, Box<CrateStoreDyn>, ty::ResolverOutputs, ty::ResolverAstLowering)
{
match Rc::try_unwrap(resolver) {
Ok(resolver) => {
let mut resolver = resolver.into_inner();
@ -485,13 +486,21 @@ fn lower_to_hir<'tcx>(
sess: &Session,
definitions: &mut Definitions,
cstore: &CrateStoreDyn,
resolver: &mut ResolverOutputs,
resolutions: &ty::ResolverOutputs,
resolver: ty::ResolverAstLowering,
krate: Rc<ast::Crate>,
arena: &'tcx rustc_ast_lowering::Arena<'tcx>,
) -> &'tcx Crate<'tcx> {
// Lower AST to HIR.
let hir_crate =
rustc_ast_lowering::lower_crate(sess, &krate, definitions, cstore, resolver, arena);
let hir_crate = rustc_ast_lowering::lower_crate(
sess,
&krate,
definitions,
cstore,
resolutions,
resolver,
arena,
);
// Drop AST to free memory
sess.time("drop_ast", || std::mem::drop(krate));
@ -829,14 +838,21 @@ pub fn create_global_ctxt<'tcx>(
// incr. comp. yet.
dep_graph.assert_ignored();
let (mut definitions, cstore, mut resolver_outputs) =
let (mut definitions, cstore, resolver_outputs, resolver_for_lowering) =
BoxedResolver::to_resolver_outputs(resolver);
let sess = &compiler.session();
// Lower AST to HIR.
let krate =
lower_to_hir(sess, &mut definitions, &*cstore, &mut resolver_outputs, krate, hir_arena);
let krate = lower_to_hir(
sess,
&mut definitions,
&*cstore,
&resolver_outputs,
resolver_for_lowering,
krate,
hir_arena,
);
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

View File

@ -160,7 +160,12 @@ pub struct ResolverOutputs {
/// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
pub confused_type_with_std_module: FxHashMap<Span, Span>,
pub registered_tools: RegisteredTools,
}
/// Resolutions that should only be used for lowering.
/// This struct is meant to be consumed by lowering.
#[derive(Debug)]
pub struct ResolverAstLowering {
pub legacy_const_generic_args: FxHashMap<DefId, Option<Vec<usize>>>,
/// Resolutions for nodes that have a single resolution.

View File

@ -1392,7 +1392,9 @@ pub fn arenas() -> ResolverArenas<'a> {
Default::default()
}
pub fn into_outputs(self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
pub fn into_outputs(
self,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions;
let cstore = Box::new(self.crate_loader.into_cstore());
@ -1429,6 +1431,8 @@ pub fn into_outputs(self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs)
proc_macros,
confused_type_with_std_module,
registered_tools: self.registered_tools,
};
let resolutions_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args,
partial_res_map: self.partial_res_map,
import_res_map: self.import_res_map,
@ -1441,10 +1445,12 @@ pub fn into_outputs(self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs)
trait_map: self.trait_map,
builtin_macro_kinds: self.builtin_macro_kinds,
};
(definitions, cstore, resolutions)
(definitions, cstore, resolutions, resolutions_lowering)
}
pub fn clone_outputs(&self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs) {
pub fn clone_outputs(
&self,
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions.clone();
let cstore = Box::new(self.cstore().clone());
@ -1469,6 +1475,8 @@ pub fn clone_outputs(&self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs
confused_type_with_std_module: self.confused_type_with_std_module.clone(),
registered_tools: self.registered_tools.clone(),
access_levels: self.access_levels.clone(),
};
let resolutions_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args.clone(),
partial_res_map: self.partial_res_map.clone(),
import_res_map: self.import_res_map.clone(),
@ -1481,7 +1489,7 @@ pub fn clone_outputs(&self) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs
trait_map: self.trait_map.clone(),
builtin_macro_kinds: self.builtin_macro_kinds.clone(),
};
(definitions, cstore, resolutions)
(definitions, cstore, resolutions, resolutions_lowering)
}
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {