From 6713fe93d8865702e83b4baab955c07470c9b2fe Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 1 Apr 2021 19:05:14 +0200 Subject: [PATCH 1/5] Remove mutability in ResolverAstLowering. --- compiler/rustc_ast_lowering/src/lib.rs | 16 ++++++++-------- compiler/rustc_resolve/src/lib.rs | 23 +++++++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 77738b2c5cc75..5fec4a3ba340d 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -162,7 +162,7 @@ struct LoweringContext<'a, 'hir: 'a> { } pub trait ResolverAstLowering { - fn def_key(&mut self, id: DefId) -> DefKey; + fn def_key(&self, id: DefId) -> DefKey; fn def_span(&self, id: LocalDefId) -> Span; @@ -174,17 +174,17 @@ pub trait ResolverAstLowering { fn get_partial_res(&self, id: NodeId) -> Option; /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`. - fn get_import_res(&mut self, id: NodeId) -> PerNS>>; + fn get_import_res(&self, id: NodeId) -> PerNS>>; /// Obtains resolution for a label with the given `NodeId`. - fn get_label_res(&mut self, id: NodeId) -> Option; - - /// We must keep the set of definitions up to date as we add nodes that weren't in the AST. - /// This should only return `None` during testing. - fn definitions(&mut self) -> &mut Definitions; + fn get_label_res(&self, id: NodeId) -> Option; fn create_stable_hashing_context(&self) -> StableHashingContext<'_>; + fn definitions(&self) -> &Definitions; + + fn init_def_id_to_hir_id_mapping(&mut self, mapping: IndexVec>); + fn lint_buffer(&mut self) -> &mut LintBuffer; fn next_node_id(&mut self) -> NodeId; @@ -412,7 +412,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } - self.resolver.definitions().init_def_id_to_hir_id_mapping(def_id_to_hir_id); + self.resolver.init_def_id_to_hir_id_mapping(def_id_to_hir_id); let krate = hir::Crate { owners: self.owners, hir_hash }; self.arena.alloc(krate) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 84ce492ba7203..be66db5212407 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1134,9 +1134,9 @@ impl<'a, 'b> DefIdTree for &'a Resolver<'b> { /// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that /// the resolver is no longer needed as all the relevant information is inline. impl ResolverAstLowering for Resolver<'_> { - fn def_key(&mut self, id: DefId) -> DefKey { + fn def_key(&self, id: DefId) -> DefKey { if let Some(id) = id.as_local() { - self.definitions().def_key(id) + self.definitions.def_key(id) } else { self.cstore().def_key(id) } @@ -1163,22 +1163,29 @@ impl ResolverAstLowering for Resolver<'_> { self.partial_res_map.get(&id).cloned() } - fn get_import_res(&mut self, id: NodeId) -> PerNS> { + fn get_import_res(&self, id: NodeId) -> PerNS> { self.import_res_map.get(&id).cloned().unwrap_or_default() } - fn get_label_res(&mut self, id: NodeId) -> Option { + fn get_label_res(&self, id: NodeId) -> Option { self.label_res_map.get(&id).cloned() } - fn definitions(&mut self) -> &mut Definitions { - &mut self.definitions - } - fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { StableHashingContext::new(self.session, &self.definitions, self.crate_loader.cstore()) } + fn definitions(&self) -> &Definitions { + &self.definitions + } + + fn init_def_id_to_hir_id_mapping( + &mut self, + mapping: IndexVec>, + ) { + self.definitions.init_def_id_to_hir_id_mapping(mapping) + } + fn lint_buffer(&mut self) -> &mut LintBuffer { &mut self.lint_buffer } From 525f3790e3cdfe770655cf4bc037f6c9099c0d8a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 2 Apr 2021 16:47:08 +0200 Subject: [PATCH 2/5] Store next_disambiguator in Definitions. --- compiler/rustc_hir/src/definitions.rs | 10 ++++++++-- compiler/rustc_resolve/src/lib.rs | 13 +------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index d813c887eee9a..1557edd32f35f 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -29,6 +29,7 @@ pub struct DefPathTable { index_to_key: IndexVec, def_path_hashes: IndexVec, def_path_hash_to_index: DefPathHashMap, + next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, } impl DefPathTable { @@ -380,7 +381,6 @@ impl Definitions { parent: LocalDefId, data: DefPathData, expn_id: ExpnId, - mut next_disambiguator: impl FnMut(LocalDefId, DefPathData) -> u32, span: Span, ) -> LocalDefId { debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id); @@ -388,7 +388,13 @@ impl Definitions { // The root node must be created with `create_root_def()`. assert!(data != DefPathData::CrateRoot); - let disambiguator = next_disambiguator(parent, data); + // Find the next free disambiguator for this key. + let disambiguator = { + let next_disamb = self.table.next_disambiguator.entry((parent, data)).or_insert(0); + let disambiguator = *next_disamb; + *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow"); + disambiguator + }; let key = DefKey { parent: Some(parent.local_def_index), disambiguated_data: DisambiguatedDefPathData { data, disambiguator }, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index be66db5212407..0d7986db7be28 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1037,7 +1037,6 @@ pub struct Resolver<'a> { /// and how the `impl Trait` fragments were introduced. invocation_parents: FxHashMap, - next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, /// Some way to know that we are in a *trait* impl in `visit_assoc_item`. /// FIXME: Replace with a more general AST map (together with some other fields). trait_impl_items: FxHashSet, @@ -1230,16 +1229,7 @@ impl ResolverAstLowering for Resolver<'_> { self.definitions.def_key(self.node_id_to_def_id[&node_id]), ); - // Find the next free disambiguator for this key. - let next_disambiguator = &mut self.next_disambiguator; - let next_disambiguator = |parent, data| { - let next_disamb = next_disambiguator.entry((parent, data)).or_insert(0); - let disambiguator = *next_disamb; - *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow"); - disambiguator - }; - - let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator, span); + let def_id = self.definitions.create_def(parent, data, expn_id, span); // Some things for which we allocate `LocalDefId`s don't correspond to // anything in the AST, so they don't have a `NodeId`. For these cases @@ -1406,7 +1396,6 @@ impl<'a> Resolver<'a> { def_id_to_node_id, placeholder_field_indices: Default::default(), invocation_parents, - next_disambiguator: Default::default(), trait_impl_items: Default::default(), legacy_const_generic_args: Default::default(), item_generics_num_lifetimes: Default::default(), From 5eef76d8763d20ee05a6b8b378df2bb94fdcdbae Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 14 Jul 2021 00:48:51 +0200 Subject: [PATCH 3/5] Separate Definitions and CrateStore from ResolverOutputs. --- compiler/rustc_interface/src/passes.rs | 11 ++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- compiler/rustc_middle/src/hir/map/mod.rs | 16 +++--- compiler/rustc_middle/src/hir/mod.rs | 9 ++-- compiler/rustc_middle/src/query/mod.rs | 1 + compiler/rustc_middle/src/ty/context.rs | 53 ++++++++++---------- compiler/rustc_middle/src/ty/mod.rs | 3 -- compiler/rustc_query_impl/src/stats.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 23 +++++---- 9 files changed, 63 insertions(+), 59 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index d11cc52b50860..c340ae30b5121 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -13,6 +13,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_errors::{Applicability, ErrorReported, PResult}; use rustc_expand::base::ExtCtxt; use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE}; +use rustc_hir::definitions::Definitions; use rustc_hir::Crate; use rustc_lint::LintStore; use rustc_metadata::creader::CStore; @@ -29,7 +30,7 @@ use rustc_query_impl::{OnDiskCache, Queries as TcxQueries}; use rustc_resolve::{Resolver, ResolverArenas}; use rustc_serialize::json; use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode}; -use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn}; +use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn}; use rustc_session::lint; use rustc_session::output::{filename_for_input, filename_for_metadata}; use rustc_session::search_paths::PathKind; @@ -142,7 +143,9 @@ mod boxed_resolver { f((&mut *resolver).as_mut().unwrap()) } - pub fn to_resolver_outputs(resolver: Rc>) -> ResolverOutputs { + pub fn to_resolver_outputs( + resolver: Rc>, + ) -> (Definitions, Box, ResolverOutputs) { match Rc::try_unwrap(resolver) { Ok(resolver) => { let mut resolver = resolver.into_inner(); @@ -844,7 +847,7 @@ pub fn create_global_ctxt<'tcx>( let krate = resolver .borrow_mut() .access(|resolver| lower_to_hir(sess, &lint_store, resolver, krate, hir_arena)); - let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver); + let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver); let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess); @@ -869,6 +872,8 @@ pub fn create_global_ctxt<'tcx>( sess, lint_store, arena, + definitions, + cstore, resolver_outputs, krate, dep_graph, diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index eeb907d01148b..fc5c7b99e2a72 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -453,7 +453,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_def_path_table(&mut self) { - let table = self.tcx.resolutions(()).definitions.def_path_table(); + let table = self.tcx.definitions.def_path_table(); if self.is_proc_macro { for def_index in std::iter::once(CRATE_DEF_INDEX) .chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)) @@ -475,7 +475,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_def_path_hash_map(&mut self) -> Lazy> { self.lazy(DefPathHashMapRef::BorrowedFromTcx( - self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(), + self.tcx.definitions.def_path_hash_to_def_index_map(), )) } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 394a1fc227095..3490740554e07 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -169,7 +169,7 @@ impl<'hir> Map<'hir> { pub fn def_key(&self, def_id: LocalDefId) -> DefKey { // Accessing the DefKey is ok, since it is part of DefPathHash. - self.tcx.untracked_resolutions.definitions.def_key(def_id) + self.tcx.definitions.def_key(def_id) } pub fn def_path_from_hir_id(&self, id: HirId) -> Option { @@ -178,13 +178,13 @@ impl<'hir> Map<'hir> { pub fn def_path(&self, def_id: LocalDefId) -> DefPath { // Accessing the DefPath is ok, since it is part of DefPathHash. - self.tcx.untracked_resolutions.definitions.def_path(def_id) + self.tcx.definitions.def_path(def_id) } #[inline] pub fn def_path_hash(self, def_id: LocalDefId) -> DefPathHash { // Accessing the DefPathHash is ok, it is incr. comp. stable. - self.tcx.untracked_resolutions.definitions.def_path_hash(def_id) + self.tcx.definitions.def_path_hash(def_id) } #[inline] @@ -201,20 +201,20 @@ impl<'hir> Map<'hir> { #[inline] pub fn opt_local_def_id(&self, hir_id: HirId) -> Option { // FIXME(#85914) is this access safe for incr. comp.? - self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id) + self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id) } #[inline] pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId { // FIXME(#85914) is this access safe for incr. comp.? - self.tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id) + self.tcx.definitions.local_def_id_to_hir_id(def_id) } pub fn iter_local_def_id(&self) -> impl Iterator + '_ { // Create a dependency to the crate to be sure we reexcute this when the amount of // definitions change. self.tcx.ensure().hir_crate(()); - self.tcx.untracked_resolutions.definitions.iter_local_def_id() + self.tcx.definitions.iter_local_def_id() } pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option { @@ -1096,7 +1096,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { upstream_crates.hash_stable(&mut hcx, &mut stable_hasher); source_file_names.hash_stable(&mut hcx, &mut stable_hasher); if tcx.sess.opts.debugging_opts.incremental_relative_spans { - let definitions = &tcx.untracked_resolutions.definitions; + let definitions = &tcx.definitions; let mut owner_spans: Vec<_> = krate .owners .iter_enumerated() @@ -1123,7 +1123,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> { .crates(()) .iter() .map(|&cnum| { - let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum); + let stable_crate_id = tcx.cstore.stable_crate_id(cnum); let hash = tcx.crate_hash(cnum); (stable_crate_id, hash) }) diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 95d7273b17b44..0588bff43e66c 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -72,11 +72,10 @@ pub fn provide(providers: &mut Providers) { providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id].as_ref().map(|i| &i.nodes); providers.hir_owner_parent = |tcx, id| { // Accessing the def_key is ok since its value is hashed as part of `id`'s DefPathHash. - let parent = tcx.untracked_resolutions.definitions.def_key(id).parent; + let parent = tcx.definitions.def_key(id).parent; let parent = parent.map_or(CRATE_HIR_ID, |local_def_index| { let def_id = LocalDefId { local_def_index }; - let mut parent_hir_id = - tcx.untracked_resolutions.definitions.local_def_id_to_hir_id(def_id); + let mut parent_hir_id = tcx.definitions.local_def_id_to_hir_id(def_id); if let Some(local_id) = tcx.hir_crate(()).owners[parent_hir_id.owner].as_ref().unwrap().parenting.get(&id) { @@ -88,7 +87,7 @@ pub fn provide(providers: &mut Providers) { }; providers.hir_attrs = |tcx, id| tcx.hir_crate(()).owners[id].as_ref().map_or(AttributeMap::EMPTY, |o| &o.attrs); - providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id); + providers.source_span = |tcx, def_id| tcx.definitions.def_span(def_id); providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP); providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); @@ -109,6 +108,6 @@ pub fn provide(providers: &mut Providers) { providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls; providers.expn_that_defined = |tcx, id| { let id = id.expect_local(); - tcx.resolutions(()).definitions.expansion_that_defined(id) + tcx.definitions.expansion_that_defined(id) }; } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index ad3f61d07843a..cdcee187e7c0d 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -25,6 +25,7 @@ rustc_queries! { /// This span is meant for dep-tracking rather than diagnostics. It should not be used outside /// of rustc_middle::hir::source_map. query source_span(key: LocalDefId) -> Span { + eval_always desc { "get the source span" } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index caa7008f1085e..ee22a5b4deb3a 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -46,6 +46,7 @@ use rustc_middle::mir::FakeReadCause; use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext}; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; +use rustc_session::cstore::CrateStoreDyn; use rustc_session::lint::{Level, Lint}; use rustc_session::Limit; use rustc_session::Session; @@ -999,6 +1000,9 @@ pub struct GlobalCtxt<'tcx> { /// Common consts, pre-interned for your convenience. pub consts: CommonConsts<'tcx>, + pub definitions: rustc_hir::definitions::Definitions, + pub cstore: Box, + /// Output of the resolver. pub(crate) untracked_resolutions: ty::ResolverOutputs, @@ -1145,7 +1149,9 @@ impl<'tcx> TyCtxt<'tcx> { s: &'tcx Session, lint_store: Lrc, arena: &'tcx WorkerLocal>, - resolutions: ty::ResolverOutputs, + definitions: rustc_hir::definitions::Definitions, + cstore: Box, + untracked_resolutions: ty::ResolverOutputs, krate: &'tcx hir::Crate<'tcx>, dep_graph: DepGraph, on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>, @@ -1168,7 +1174,9 @@ impl<'tcx> TyCtxt<'tcx> { arena, interners, dep_graph, - untracked_resolutions: resolutions, + definitions, + cstore, + untracked_resolutions, prof: s.prof.clone(), types: common_types, lifetimes: common_lifetimes, @@ -1254,9 +1262,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey { // Accessing the DefKey is ok, since it is part of DefPathHash. if let Some(id) = id.as_local() { - self.untracked_resolutions.definitions.def_key(id) + self.definitions.def_key(id) } else { - self.untracked_resolutions.cstore.def_key(id) + self.cstore.def_key(id) } } @@ -1268,9 +1276,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath { // Accessing the DefPath is ok, since it is part of DefPathHash. if let Some(id) = id.as_local() { - self.untracked_resolutions.definitions.def_path(id) + self.definitions.def_path(id) } else { - self.untracked_resolutions.cstore.def_path(id) + self.cstore.def_path(id) } } @@ -1278,9 +1286,9 @@ impl<'tcx> TyCtxt<'tcx> { pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash { // Accessing the DefPathHash is ok, it is incr. comp. stable. if let Some(def_id) = def_id.as_local() { - self.untracked_resolutions.definitions.def_path_hash(def_id) + self.definitions.def_path_hash(def_id) } else { - self.untracked_resolutions.cstore.def_path_hash(def_id) + self.cstore.def_path_hash(def_id) } } @@ -1289,7 +1297,7 @@ impl<'tcx> TyCtxt<'tcx> { if crate_num == LOCAL_CRATE { self.sess.local_stable_crate_id() } else { - self.untracked_resolutions.cstore.stable_crate_id(crate_num) + self.cstore.stable_crate_id(crate_num) } } @@ -1300,7 +1308,7 @@ impl<'tcx> TyCtxt<'tcx> { if stable_crate_id == self.sess.local_stable_crate_id() { LOCAL_CRATE } else { - self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id) + self.cstore.stable_crate_id_to_crate_num(stable_crate_id) } } @@ -1315,13 +1323,12 @@ impl<'tcx> TyCtxt<'tcx> { // If this is a DefPathHash from the local crate, we can look up the // DefId in the tcx's `Definitions`. if stable_crate_id == self.sess.local_stable_crate_id() { - self.untracked_resolutions.definitions.local_def_path_hash_to_def_id(hash).to_def_id() + self.definitions.local_def_path_hash_to_def_id(hash).to_def_id() } else { // If this is a DefPathHash from an upstream crate, let the CrateStore map // it to a DefId. - let cstore = &self.untracked_resolutions.cstore; - let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id); - cstore.def_path_hash_to_def_id(cnum, hash) + let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id); + self.cstore.def_path_hash_to_def_id(cnum, hash) } } @@ -1333,7 +1340,7 @@ impl<'tcx> TyCtxt<'tcx> { let (crate_name, stable_crate_id) = if def_id.is_local() { (self.crate_name, self.sess.local_stable_crate_id()) } else { - let cstore = &self.untracked_resolutions.cstore; + let cstore = &self.cstore; (cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate)) }; @@ -1349,30 +1356,24 @@ impl<'tcx> TyCtxt<'tcx> { /// Note that this is *untracked* and should only be used within the query /// system if the result is otherwise tracked through queries - pub fn cstore_untracked(self) -> &'tcx ty::CrateStoreDyn { - &*self.untracked_resolutions.cstore + pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn { + &*self.cstore } /// Note that this is *untracked* and should only be used within the query /// system if the result is otherwise tracked through queries pub fn definitions_untracked(self) -> &'tcx hir::definitions::Definitions { - &self.untracked_resolutions.definitions + &self.definitions } #[inline(always)] pub fn create_stable_hashing_context(self) -> StableHashingContext<'tcx> { - let resolutions = &self.gcx.untracked_resolutions; - StableHashingContext::new(self.sess, &resolutions.definitions, &*resolutions.cstore) + StableHashingContext::new(self.sess, &self.definitions, &*self.cstore) } #[inline(always)] pub fn create_no_span_stable_hashing_context(self) -> StableHashingContext<'tcx> { - let resolutions = &self.gcx.untracked_resolutions; - StableHashingContext::ignore_spans( - self.sess, - &resolutions.definitions, - &*resolutions.cstore, - ) + StableHashingContext::ignore_spans(self.sess, &self.definitions, &*self.cstore) } pub fn serialize_query_result_cache(self, encoder: &mut FileEncoder) -> FileEncodeResult { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 37d99766da9d2..e0dce5cfbe214 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -36,7 +36,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_IN use rustc_hir::Node; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; -use rustc_session::cstore::CrateStoreDyn; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::{sym, Span}; use rustc_target::abi::Align; @@ -120,8 +119,6 @@ mod sty; #[derive(Debug)] pub struct ResolverOutputs { - pub definitions: rustc_hir::definitions::Definitions, - pub cstore: Box, pub visibilities: FxHashMap, pub extern_crate_map: FxHashMap, pub maybe_unused_trait_imports: FxHashSet, diff --git a/compiler/rustc_query_impl/src/stats.rs b/compiler/rustc_query_impl/src/stats.rs index c3bbd51f3d3cc..1cd5cb53187c1 100644 --- a/compiler/rustc_query_impl/src/stats.rs +++ b/compiler/rustc_query_impl/src/stats.rs @@ -81,7 +81,7 @@ pub fn print_stats(tcx: TyCtxt<'_>) { queries.iter().filter(|q| q.local_def_id_keys.is_some()).collect(); def_id_density.sort_by_key(|q| q.local_def_id_keys.unwrap()); eprintln!("\nLocal DefId density:"); - let total = tcx.resolutions(()).definitions.def_index_count() as f64; + let total = tcx.definitions.def_index_count() as f64; for q in def_id_density.iter().rev() { let local = q.local_def_id_keys.unwrap(); eprintln!(" {} - {} = ({}%)", q.name, local, (local as f64 * 100.0) / total); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 0d7986db7be28..36f63b3a07a95 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -54,7 +54,7 @@ use rustc_middle::span_bug; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, DefIdTree, MainDefinition, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; -use rustc_session::cstore::{CrateStore, MetadataLoaderDyn}; +use rustc_session::cstore::{CrateStore, CrateStoreDyn, MetadataLoaderDyn}; use rustc_session::lint; use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer}; use rustc_session::Session; @@ -1438,9 +1438,10 @@ impl<'a> Resolver<'a> { Default::default() } - pub fn into_outputs(self) -> ResolverOutputs { + pub fn into_outputs(self) -> (Definitions, Box, ResolverOutputs) { 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()); let visibilities = self.visibilities; let extern_crate_map = self.extern_crate_map; let export_map = self.export_map; @@ -1449,9 +1450,7 @@ impl<'a> Resolver<'a> { let glob_map = self.glob_map; let main_def = self.main_def; let confused_type_with_std_module = self.confused_type_with_std_module; - ResolverOutputs { - definitions, - cstore: Box::new(self.crate_loader.into_cstore()), + let resolutions = ResolverOutputs { visibilities, extern_crate_map, export_map, @@ -1467,14 +1466,15 @@ impl<'a> Resolver<'a> { trait_impls: self.trait_impls, proc_macros, confused_type_with_std_module, - } + }; + (definitions, cstore, resolutions) } - pub fn clone_outputs(&self) -> ResolverOutputs { + pub fn clone_outputs(&self) -> (Definitions, Box, ResolverOutputs) { let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); - ResolverOutputs { - definitions: self.definitions.clone(), - cstore: Box::new(self.cstore().clone()), + let definitions = self.definitions.clone(); + let cstore = Box::new(self.cstore().clone()); + let resolutions = ResolverOutputs { visibilities: self.visibilities.clone(), extern_crate_map: self.extern_crate_map.clone(), export_map: self.export_map.clone(), @@ -1490,7 +1490,8 @@ impl<'a> Resolver<'a> { trait_impls: self.trait_impls.clone(), proc_macros, confused_type_with_std_module: self.confused_type_with_std_module.clone(), - } + }; + (definitions, cstore, resolutions) } pub fn cstore(&self) -> &CStore { From bfca57cf55777461e9bbc0975f2993b0c5d6a718 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 31 Jul 2021 22:50:43 +0200 Subject: [PATCH 4/5] Stop emitting lints during lowering. --- compiler/rustc_ast_lowering/src/lib.rs | 3 --- compiler/rustc_interface/src/passes.rs | 30 ++++++++++++-------------- compiler/rustc_resolve/src/lib.rs | 4 ---- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 5fec4a3ba340d..8f122db54c4a0 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -55,7 +55,6 @@ use rustc_hir::intravisit; use rustc_hir::{ConstArg, GenericArg, ParamName}; use rustc_index::vec::{Idx, IndexVec}; use rustc_query_system::ich::StableHashingContext; -use rustc_session::lint::LintBuffer; use rustc_session::utils::{FlattenNonterminals, NtToTokenstream}; use rustc_session::Session; use rustc_span::hygiene::ExpnId; @@ -185,8 +184,6 @@ pub trait ResolverAstLowering { fn init_def_id_to_hir_id_mapping(&mut self, mapping: IndexVec>); - fn lint_buffer(&mut self) -> &mut LintBuffer; - fn next_node_id(&mut self) -> NodeId; fn take_trait_map(&mut self, node: NodeId) -> Option>; diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index c340ae30b5121..5768ce6f6dd5c 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -482,12 +482,23 @@ pub fn configure_and_expand( } }); + sess.time("early_lint_checks", || { + rustc_lint::check_ast_crate( + sess, + lint_store, + &krate, + &krate.attrs, + false, + Some(std::mem::take(resolver.lint_buffer())), + rustc_lint::BuiltinCombinedEarlyLintPass::new(), + ) + }); + Ok(krate) } pub fn lower_to_hir<'res, 'tcx>( sess: &'tcx Session, - lint_store: &LintStore, resolver: &'res mut Resolver<'_>, krate: Rc, arena: &'tcx rustc_ast_lowering::Arena<'tcx>, @@ -501,18 +512,6 @@ pub fn lower_to_hir<'res, 'tcx>( arena, ); - sess.time("early_lint_checks", || { - rustc_lint::check_ast_crate( - sess, - lint_store, - &krate, - &krate.attrs, - false, - Some(std::mem::take(resolver.lint_buffer())), - rustc_lint::BuiltinCombinedEarlyLintPass::new(), - ) - }); - // Drop AST to free memory sess.time("drop_ast", || std::mem::drop(krate)); @@ -844,9 +843,8 @@ pub fn create_global_ctxt<'tcx>( dep_graph.assert_ignored(); let sess = &compiler.session(); - let krate = resolver - .borrow_mut() - .access(|resolver| lower_to_hir(sess, &lint_store, resolver, krate, hir_arena)); + let krate = + resolver.borrow_mut().access(|resolver| lower_to_hir(sess, resolver, krate, hir_arena)); let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver); let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 36f63b3a07a95..c752f459a67f0 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1185,10 +1185,6 @@ impl ResolverAstLowering for Resolver<'_> { self.definitions.init_def_id_to_hir_id_mapping(mapping) } - fn lint_buffer(&mut self) -> &mut LintBuffer { - &mut self.lint_buffer - } - fn next_node_id(&mut self) -> NodeId { self.next_node_id() } From 87fae8857fe3ee492d0e8883d07270e73f858179 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 5 Jul 2021 22:26:23 +0200 Subject: [PATCH 5/5] Make ResolverAstLowering a struct. --- Cargo.lock | 3 +- compiler/rustc_ast_lowering/Cargo.toml | 1 + compiler/rustc_ast_lowering/src/expr.rs | 3 +- compiler/rustc_ast_lowering/src/index.rs | 4 +- compiler/rustc_ast_lowering/src/item.rs | 1 + compiler/rustc_ast_lowering/src/lib.rs | 186 +++++++++++++----- compiler/rustc_ast_lowering/src/pat.rs | 1 + compiler/rustc_ast_lowering/src/path.rs | 7 +- compiler/rustc_interface/Cargo.toml | 1 + compiler/rustc_interface/src/passes.rs | 27 ++- compiler/rustc_middle/src/ty/mod.rs | 19 ++ compiler/rustc_resolve/Cargo.toml | 1 - .../rustc_resolve/src/build_reduced_graph.rs | 8 +- compiler/rustc_resolve/src/check_unused.rs | 1 - compiler/rustc_resolve/src/def_collector.rs | 1 - compiler/rustc_resolve/src/late.rs | 1 - compiler/rustc_resolve/src/lib.rs | 100 +++------- compiler/rustc_resolve/src/macros.rs | 1 - src/librustdoc/lib.rs | 1 - .../passes/collect_intra_doc_links/early.rs | 2 - 20 files changed, 222 insertions(+), 147 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd50defbe6f6c..c04ac7160b94e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3598,6 +3598,7 @@ dependencies = [ "rustc_errors", "rustc_hir", "rustc_index", + "rustc_middle", "rustc_query_system", "rustc_session", "rustc_span", @@ -4007,6 +4008,7 @@ dependencies = [ "rustc_expand", "rustc_hir", "rustc_incremental", + "rustc_index", "rustc_lint", "rustc_metadata", "rustc_middle", @@ -4371,7 +4373,6 @@ dependencies = [ "bitflags", "rustc_arena", "rustc_ast", - "rustc_ast_lowering", "rustc_ast_pretty", "rustc_attr", "rustc_data_structures", diff --git a/compiler/rustc_ast_lowering/Cargo.toml b/compiler/rustc_ast_lowering/Cargo.toml index 7989af24d9986..e344d8a7637c4 100644 --- a/compiler/rustc_ast_lowering/Cargo.toml +++ b/compiler/rustc_ast_lowering/Cargo.toml @@ -14,6 +14,7 @@ rustc_hir = { path = "../rustc_hir" } rustc_target = { path = "../rustc_target" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_index = { path = "../rustc_index" } +rustc_middle = { path = "../rustc_middle" } rustc_query_system = { path = "../rustc_query_system" } rustc_span = { path = "../rustc_span" } rustc_errors = { path = "../rustc_errors" } diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 6ee1dbe4ae3ee..87663b065886c 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1,3 +1,4 @@ +use super::ResolverAstLoweringExt; use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs}; use rustc_ast::attr; @@ -346,7 +347,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let node_id = self.resolver.next_node_id(); // Add a definition for the in-band const def. - self.resolver.create_def( + self.create_def( parent_def_id, node_id, DefPathData::AnonConst, diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 8a9dad2cdd7d8..ed5f5924a2d60 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -6,6 +6,7 @@ use rustc_hir::definitions; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::*; use rustc_index::vec::{Idx, IndexVec}; +use rustc_middle::span_bug; use rustc_session::Session; use rustc_span::source_map::SourceMap; use rustc_span::{Span, DUMMY_SP}; @@ -71,7 +72,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { // owner of that node. if cfg!(debug_assertions) { if hir_id.owner != self.owner { - panic!( + span_bug!( + span, "inconsistent DepNode at `{:?}` for `{:?}`: \ current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", self.source_map.span_to_diagnostic_string(span), diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 92cae4da89ab5..e51c361e7b303 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1,3 +1,4 @@ +use super::ResolverAstLoweringExt; use super::{AnonymousLifetimeMode, LoweringContext, ParamMode}; use super::{ImplTraitContext, ImplTraitPosition}; use crate::Arena; diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 8f122db54c4a0..7f5d164d744a9 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -49,12 +49,14 @@ use rustc_data_structures::sync::Lrc; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res}; -use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::definitions::{DefKey, DefPathData, Definitions}; use rustc_hir::intravisit; use rustc_hir::{ConstArg, GenericArg, ParamName}; use rustc_index::vec::{Idx, IndexVec}; +use rustc_middle::ty::ResolverOutputs; use rustc_query_system::ich::StableHashingContext; +use rustc_session::cstore::CrateStoreDyn; use rustc_session::utils::{FlattenNonterminals, NtToTokenstream}; use rustc_session::Session; use rustc_span::hygiene::ExpnId; @@ -85,13 +87,17 @@ struct LoweringContext<'a, 'hir: 'a> { /// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes. sess: &'a Session, - resolver: &'a mut dyn ResolverAstLowering, + definitions: &'a mut Definitions, + cstore: &'a CrateStoreDyn, + resolver: &'a mut ResolverOutputs, /// HACK(Centril): there is a cyclic dependency between the parser and lowering /// if we don't have this function pointer. To avoid that dependency so that /// `rustc_middle` is independent of the parser, we use dynamic dispatch here. nt_to_tokenstream: NtToTokenstream, + item_generics_num_lifetimes: fn(&Session, &CrateStoreDyn, DefId) -> usize, + /// Used to allocate HIR nodes. arena: &'hir Arena<'hir>, @@ -160,48 +166,79 @@ struct LoweringContext<'a, 'hir: 'a> { allow_into_future: Option>, } -pub trait ResolverAstLowering { - fn def_key(&self, id: DefId) -> DefKey; - - fn def_span(&self, id: LocalDefId) -> Span; - - fn item_generics_num_lifetimes(&self, def: DefId) -> usize; - - fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option>; - - /// Obtains resolution for a `NodeId` with a single resolution. +trait ResolverAstLoweringExt { + fn legacy_const_generic_args(&self, expr: &Expr) -> Option>; fn get_partial_res(&self, id: NodeId) -> Option; - - /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`. fn get_import_res(&self, id: NodeId) -> PerNS>>; - - /// Obtains resolution for a label with the given `NodeId`. fn get_label_res(&self, id: NodeId) -> Option; + fn next_node_id(&mut self) -> NodeId; + fn opt_local_def_id(&self, node: NodeId) -> Option; + fn local_def_id(&self, node: NodeId) -> LocalDefId; +} - fn create_stable_hashing_context(&self) -> StableHashingContext<'_>; +impl ResolverAstLoweringExt for ResolverOutputs { + fn legacy_const_generic_args(&self, expr: &Expr) -> Option> { + if let ExprKind::Path(None, path) = &expr.kind { + // Don't perform legacy const generics rewriting if the path already + // has generic arguments. + if path.segments.last().unwrap().args.is_some() { + return None; + } - fn definitions(&self) -> &Definitions; + let partial_res = self.partial_res_map.get(&expr.id)?; + if partial_res.unresolved_segments() != 0 { + return None; + } - fn init_def_id_to_hir_id_mapping(&mut self, mapping: IndexVec>); + if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() { + // We only support cross-crate argument rewriting. Uses + // within the same crate should be updated to use the new + // const generics style. + if def_id.is_local() { + return None; + } - fn next_node_id(&mut self) -> NodeId; + if let Some(v) = self.legacy_const_generic_args.get(&def_id) { + return v.clone(); + } + } + } - fn take_trait_map(&mut self, node: NodeId) -> Option>; + None + } - fn opt_local_def_id(&self, node: NodeId) -> Option; + /// Obtains resolution for a `NodeId` with a single resolution. + fn get_partial_res(&self, id: NodeId) -> Option { + self.partial_res_map.get(&id).cloned() + } - fn local_def_id(&self, node: NodeId) -> LocalDefId; + /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`. + fn get_import_res(&self, id: NodeId) -> PerNS>> { + self.import_res_map.get(&id).cloned().unwrap_or_default() + } - fn def_path_hash(&self, def_id: DefId) -> DefPathHash; + /// Obtains resolution for a label with the given `NodeId`. + fn get_label_res(&self, id: NodeId) -> Option { + self.label_res_map.get(&id).cloned() + } - fn create_def( - &mut self, - parent: LocalDefId, - node_id: ast::NodeId, - data: DefPathData, - expn_id: ExpnId, - span: Span, - ) -> LocalDefId; + fn next_node_id(&mut self) -> NodeId { + let next = self + .next_node_id + .as_usize() + .checked_add(1) + .expect("input too large; ran out of NodeIds"); + self.next_node_id = NodeId::from_usize(next); + self.next_node_id + } + + fn opt_local_def_id(&self, node: NodeId) -> Option { + self.node_id_to_def_id.get(&node).copied() + } + + fn local_def_id(&self, node: NodeId) -> LocalDefId { + self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node)) + } } /// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree, @@ -281,17 +318,23 @@ impl<'a> ImplTraitContext<'_, 'a> { pub fn lower_crate<'a, 'hir>( sess: &'a Session, krate: &'a Crate, - resolver: &'a mut dyn ResolverAstLowering, + definitions: &'a mut Definitions, + cstore: &'a CrateStoreDyn, + resolver: &'a mut ResolverOutputs, nt_to_tokenstream: NtToTokenstream, + item_generics_num_lifetimes: fn(&Session, &CrateStoreDyn, DefId) -> usize, arena: &'hir Arena<'hir>, ) -> &'hir hir::Crate<'hir> { let _prof_timer = sess.prof.verbose_generic_activity("hir_lowering"); - let owners = IndexVec::from_fn_n(|_| None, resolver.definitions().def_index_count()); + let owners = IndexVec::from_fn_n(|_| None, definitions.def_index_count()); LoweringContext { sess, + definitions, + cstore, resolver, nt_to_tokenstream, + item_generics_num_lifetimes, arena, owners, bodies: Vec::new(), @@ -409,7 +452,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } - self.resolver.init_def_id_to_hir_id_mapping(def_id_to_hir_id); + self.definitions.init_def_id_to_hir_id_mapping(def_id_to_hir_id); let krate = hir::Crate { owners: self.owners, hir_hash }; self.arena.alloc(krate) @@ -418,7 +461,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// Compute the hash for the HIR of the full crate. /// This hash will then be part of the crate_hash which is stored in the metadata. fn compute_hir_hash(&mut self) -> Fingerprint { - let definitions = self.resolver.definitions(); + let definitions = &*self.definitions; let mut hir_body_nodes: Vec<_> = self .owners .iter_enumerated() @@ -431,11 +474,61 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { hir_body_nodes.sort_unstable_by_key(|bn| bn.0); let mut stable_hasher = StableHasher::new(); - let mut hcx = self.resolver.create_stable_hashing_context(); + let mut hcx = self.create_stable_hashing_context(); hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher); stable_hasher.finish() } + fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { + StableHashingContext::new(self.sess, self.definitions, self.cstore) + } + + fn def_key(&self, id: DefId) -> DefKey { + if let Some(id) = id.as_local() { + self.definitions.def_key(id) + } else { + self.cstore.def_key(id) + } + } + + fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize { + if let Some(def_id) = def_id.as_local() { + self.resolver.item_generics_num_lifetimes[&def_id] + } else { + (self.item_generics_num_lifetimes)(self.sess, self.cstore, def_id) + } + } + + fn create_def( + &mut self, + parent: LocalDefId, + node_id: ast::NodeId, + data: DefPathData, + expn_id: ExpnId, + span: Span, + ) -> LocalDefId { + assert!( + !self.resolver.node_id_to_def_id.contains_key(&node_id), + "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", + node_id, + data, + self.definitions.def_key(self.resolver.node_id_to_def_id[&node_id]), + ); + + let def_id = self.definitions.create_def(parent, data, expn_id, span); + + // Some things for which we allocate `LocalDefId`s don't correspond to + // anything in the AST, so they don't have a `NodeId`. For these cases + // 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.resolver.node_id_to_def_id.insert(node_id, def_id); + } + assert_eq!(self.resolver.def_id_to_node_id.push(node_id), def_id); + + def_id + } + fn with_hir_id_owner( &mut self, owner: NodeId, @@ -479,7 +572,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { .into_iter() .filter_map(|node_id| { let hir_id = self.node_id_to_hir_id[node_id]?; - let traits = self.resolver.take_trait_map(node_id)?; + let traits = self.resolver.trait_map.remove(&node_id)?; Some((hir_id.local_id, traits.into_boxed_slice())) }) .collect(); @@ -495,11 +588,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { bodies.sort_by_key(|(k, _)| *k); let bodies = SortedMap::from_presorted_elements(bodies); let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies); - let (nodes, parenting) = - index::index_hir(self.sess, self.resolver.definitions(), node, &bodies); + let (nodes, parenting) = index::index_hir(self.sess, self.definitions, node, &bodies); let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies }; let attrs = { - let mut hcx = self.resolver.create_stable_hashing_context(); + let mut hcx = self.create_stable_hashing_context(); let mut stable_hasher = StableHasher::new(); attrs.hash_stable(&mut hcx, &mut stable_hasher); let hash = stable_hasher.finish(); @@ -516,7 +608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { node: hir::OwnerNode<'hir>, bodies: &SortedMap>, ) -> (Fingerprint, Fingerprint) { - let mut hcx = self.resolver.create_stable_hashing_context(); + let mut hcx = self.create_stable_hashing_context(); let mut stable_hasher = StableHasher::new(); hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| { node.hash_stable(hcx, &mut stable_hasher) @@ -591,7 +683,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { allow_internal_unstable, reason, self.sess.edition(), - self.resolver.create_stable_hashing_context(), + self.create_stable_hashing_context(), ) } @@ -671,7 +763,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }; // Add a definition for the in-band lifetime def. - self.resolver.create_def( + self.create_def( parent_def_id, node_id, DefPathData::LifetimeNs(str_name), @@ -1054,7 +1146,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // constructing the HIR for `impl bounds...` and then lowering that. let impl_trait_node_id = self.resolver.next_node_id(); - self.resolver.create_def( + self.create_def( parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, @@ -1129,7 +1221,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let node_id = self.resolver.next_node_id(); // Add a definition for the in-band const def. - self.resolver.create_def( + self.create_def( parent_def_id, node_id, DefPathData::AnonConst, @@ -1397,7 +1489,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(|&(name, span)| { let def_node_id = lctx.resolver.next_node_id(); let hir_id = lctx.lower_node_id(def_node_id); - lctx.resolver.create_def( + lctx.create_def( opaque_ty_def_id, def_node_id, DefPathData::LifetimeNs(name.ident().name), diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index 0a9b264aa4263..06b1d8a90cbe2 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -1,3 +1,4 @@ +use super::ResolverAstLoweringExt; use super::{ImplTraitContext, LoweringContext, ParamMode}; use rustc_ast::ptr::P; diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index 78afc33974826..b1bae5c6ace68 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -1,3 +1,4 @@ +use super::ResolverAstLoweringExt; use super::{AnonymousLifetimeMode, ImplTraitContext, LoweringContext, ParamMode}; use super::{GenericArgsCtor, ParenthesizedGenericArgs}; @@ -49,7 +50,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // which may need lifetime elision performed. let parent_def_id = |this: &mut Self, def_id: DefId| DefId { krate: def_id.krate, - index: this.resolver.def_key(def_id).parent.expect("missing parent"), + index: this.def_key(def_id).parent.expect("missing parent"), }; let type_def_id = match partial_res.base_res() { Res::Def(DefKind::AssocTy, def_id) if i + 2 == proj_start => { @@ -88,8 +89,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { _ => ParenthesizedGenericArgs::Err, }; - let num_lifetimes = type_def_id - .map_or(0, |def_id| self.resolver.item_generics_num_lifetimes(def_id)); + let num_lifetimes = + type_def_id.map_or(0, |def_id| self.item_generics_num_lifetimes(def_id)); self.lower_path_segment( p.span, segment, diff --git a/compiler/rustc_interface/Cargo.toml b/compiler/rustc_interface/Cargo.toml index f5823e521b9e6..454c5562e30e3 100644 --- a/compiler/rustc_interface/Cargo.toml +++ b/compiler/rustc_interface/Cargo.toml @@ -18,6 +18,7 @@ rustc_attr = { path = "../rustc_attr" } rustc_borrowck = { path = "../rustc_borrowck" } rustc_builtin_macros = { path = "../rustc_builtin_macros" } rustc_expand = { path = "../rustc_expand" } +rustc_index = { path = "../rustc_index" } rustc_parse = { path = "../rustc_parse" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 5768ce6f6dd5c..4578e06229ee5 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -497,18 +497,29 @@ pub fn configure_and_expand( Ok(krate) } -pub fn lower_to_hir<'res, 'tcx>( - sess: &'tcx Session, - resolver: &'res mut Resolver<'_>, +fn lower_to_hir<'tcx>( + sess: &Session, + definitions: &mut Definitions, + cstore: &CrateStoreDyn, + resolver: &mut ResolverOutputs, krate: Rc, arena: &'tcx rustc_ast_lowering::Arena<'tcx>, ) -> &'tcx Crate<'tcx> { // Lower AST to HIR. let hir_crate = rustc_ast_lowering::lower_crate( sess, - &*krate, + &krate, + definitions, + cstore, resolver, rustc_parse::nt_to_tokenstream, + |sess, cstore, def_id| { + cstore + .as_any() + .downcast_ref::() + .unwrap() + .item_generics_num_lifetimes(def_id, sess) + }, arena, ); @@ -842,10 +853,14 @@ pub fn create_global_ctxt<'tcx>( // incr. comp. yet. dep_graph.assert_ignored(); + let (mut definitions, cstore, mut resolver_outputs) = + BoxedResolver::to_resolver_outputs(resolver); + let sess = &compiler.session(); + + // Lower AST to HIR. let krate = - resolver.borrow_mut().access(|resolver| lower_to_hir(sess, resolver, krate, hir_arena)); - let (definitions, cstore, resolver_outputs) = BoxedResolver::to_resolver_outputs(resolver); + lower_to_hir(sess, &mut definitions, &*cstore, &mut resolver_outputs, krate, hir_arena); let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess); diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index e0dce5cfbe214..87daac13b594b 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -26,6 +26,7 @@ use crate::ty; use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef}; use crate::ty::util::Discr; use rustc_ast as ast; +use rustc_ast::node_id::NodeMap; use rustc_attr as attr; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; @@ -34,6 +35,7 @@ use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_INDEX}; use rustc_hir::Node; +use rustc_index::vec::IndexVec; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; use rustc_span::symbol::{kw, Ident, Symbol}; @@ -136,6 +138,23 @@ pub struct ResolverOutputs { /// Mapping from ident span to path span for paths that don't exist as written, but that /// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`. pub confused_type_with_std_module: FxHashMap, + pub item_generics_num_lifetimes: FxHashMap, + + pub legacy_const_generic_args: FxHashMap>>, + + /// Resolutions for nodes that have a single resolution. + pub partial_res_map: NodeMap, + /// Resolutions for import nodes, which have multiple resolutions in different namespaces. + pub import_res_map: NodeMap>>>, + /// Resolutions for labels (node IDs of their corresponding blocks or loops). + pub label_res_map: NodeMap, + + pub next_node_id: ast::NodeId, + + pub node_id_to_def_id: FxHashMap, + pub def_id_to_node_id: IndexVec, + + pub trait_map: NodeMap>, } #[derive(Clone, Copy, Debug)] diff --git a/compiler/rustc_resolve/Cargo.toml b/compiler/rustc_resolve/Cargo.toml index bd27c16c732a9..1127a42e600fb 100644 --- a/compiler/rustc_resolve/Cargo.toml +++ b/compiler/rustc_resolve/Cargo.toml @@ -13,7 +13,6 @@ tracing = "0.1" rustc_ast = { path = "../rustc_ast" } rustc_arena = { path = "../rustc_arena" } rustc_middle = { path = "../rustc_middle" } -rustc_ast_lowering = { path = "../rustc_ast_lowering" } rustc_ast_pretty = { path = "../rustc_ast_pretty" } rustc_attr = { path = "../rustc_attr" } rustc_data_structures = { path = "../rustc_data_structures" } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index d57591186d87c..9b6fa886c5f58 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -16,7 +16,6 @@ use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError use rustc_ast::visit::{self, AssocCtxt, Visitor}; use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind}; use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId}; -use rustc_ast_lowering::ResolverAstLowering; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; use rustc_errors::{struct_span_err, Applicability}; @@ -27,7 +26,7 @@ use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; use rustc_metadata::creader::LoadedMacro; use rustc_middle::bug; use rustc_middle::hir::exports::Export; -use rustc_middle::ty; +use rustc_middle::ty::{self, DefIdTree}; use rustc_session::cstore::CrateStore; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind}; use rustc_span::source_map::{respan, Spanned}; @@ -112,10 +111,7 @@ impl<'a> Resolver<'a> { loop { match self.get_module(def_id) { Some(module) => return module, - None => { - def_id.index = - self.def_key(def_id).parent.expect("non-root `DefId` without parent") - } + None => def_id = self.parent(def_id).expect("non-root `DefId` without parent"), } } } diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 601f2d96ff5eb..5780a7007c43a 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -30,7 +30,6 @@ use crate::Resolver; use rustc_ast as ast; use rustc_ast::node_id::NodeMap; use rustc_ast::visit::{self, Visitor}; -use rustc_ast_lowering::ResolverAstLowering; use rustc_data_structures::fx::FxHashSet; use rustc_errors::pluralize; use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_IMPORTS}; diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 688b7b1a8c6d2..4ed3c1f69c948 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -2,7 +2,6 @@ use crate::{ImplTraitContext, Resolver}; use rustc_ast::visit::{self, FnKind}; use rustc_ast::walk_list; use rustc_ast::*; -use rustc_ast_lowering::ResolverAstLowering; use rustc_expand::expand::AstFragment; use rustc_hir::def_id::LocalDefId; use rustc_hir::definitions::*; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 12123c946cc08..34d013b5d470f 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -14,7 +14,6 @@ use crate::{ResolutionError, Resolver, Segment, UseError}; use rustc_ast::ptr::P; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; use rustc_ast::*; -use rustc_ast_lowering::ResolverAstLowering; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::DiagnosticId; use rustc_hir::def::Namespace::{self, *}; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index c752f459a67f0..02d3edd7ba759 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -34,7 +34,6 @@ use rustc_ast::{self as ast, NodeId}; use rustc_ast::{Crate, CRATE_NODE_ID}; use rustc_ast::{Expr, ExprKind, LitKind}; use rustc_ast::{ItemKind, ModKind, Path}; -use rustc_ast_lowering::ResolverAstLowering; use rustc_ast_pretty::pprust; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_data_structures::ptr_key::PtrKey; @@ -43,9 +42,9 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::*; use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId}; use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; -use rustc_hir::definitions::{DefKey, DefPathData, Definitions}; +use rustc_hir::definitions::{DefPathData, Definitions}; use rustc_hir::TraitCandidate; use rustc_index::vec::IndexVec; use rustc_metadata::creader::{CStore, CrateLoader}; @@ -1130,84 +1129,15 @@ impl<'a, 'b> DefIdTree for &'a Resolver<'b> { } } -/// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that -/// the resolver is no longer needed as all the relevant information is inline. -impl ResolverAstLowering for Resolver<'_> { - fn def_key(&self, id: DefId) -> DefKey { - if let Some(id) = id.as_local() { - self.definitions.def_key(id) - } else { - self.cstore().def_key(id) - } - } - - #[inline] - fn def_span(&self, id: LocalDefId) -> Span { - self.definitions.def_span(id) - } - - fn item_generics_num_lifetimes(&self, def_id: DefId) -> usize { - if let Some(def_id) = def_id.as_local() { - self.item_generics_num_lifetimes[&def_id] - } else { - self.cstore().item_generics_num_lifetimes(def_id, self.session) - } - } - - fn legacy_const_generic_args(&mut self, expr: &Expr) -> Option> { - self.legacy_const_generic_args(expr) - } - - fn get_partial_res(&self, id: NodeId) -> Option { - self.partial_res_map.get(&id).cloned() - } - - fn get_import_res(&self, id: NodeId) -> PerNS> { - self.import_res_map.get(&id).cloned().unwrap_or_default() - } - - fn get_label_res(&self, id: NodeId) -> Option { - self.label_res_map.get(&id).cloned() - } - - fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new(self.session, &self.definitions, self.crate_loader.cstore()) - } - - fn definitions(&self) -> &Definitions { - &self.definitions - } - - fn init_def_id_to_hir_id_mapping( - &mut self, - mapping: IndexVec>, - ) { - self.definitions.init_def_id_to_hir_id_mapping(mapping) - } - - fn next_node_id(&mut self) -> NodeId { - self.next_node_id() - } - - fn take_trait_map(&mut self, node: NodeId) -> Option> { - self.trait_map.remove(&node) - } - +impl Resolver<'_> { fn opt_local_def_id(&self, node: NodeId) -> Option { self.node_id_to_def_id.get(&node).copied() } - fn local_def_id(&self, node: NodeId) -> LocalDefId { + pub fn local_def_id(&self, node: NodeId) -> LocalDefId { self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node)) } - fn def_path_hash(&self, def_id: DefId) -> DefPathHash { - match def_id.as_local() { - Some(def_id) => self.definitions.def_path_hash(def_id), - None => self.cstore().def_path_hash(def_id), - } - } - /// Adds a definition with a parent definition. fn create_def( &mut self, @@ -1462,6 +1392,15 @@ impl<'a> Resolver<'a> { trait_impls: self.trait_impls, proc_macros, confused_type_with_std_module, + item_generics_num_lifetimes: self.item_generics_num_lifetimes, + legacy_const_generic_args: self.legacy_const_generic_args, + partial_res_map: self.partial_res_map, + import_res_map: self.import_res_map, + label_res_map: self.label_res_map, + next_node_id: self.next_node_id, + node_id_to_def_id: self.node_id_to_def_id, + def_id_to_node_id: self.def_id_to_node_id, + trait_map: self.trait_map, }; (definitions, cstore, resolutions) } @@ -1486,10 +1425,23 @@ impl<'a> Resolver<'a> { trait_impls: self.trait_impls.clone(), proc_macros, confused_type_with_std_module: self.confused_type_with_std_module.clone(), + item_generics_num_lifetimes: self.item_generics_num_lifetimes.clone(), + 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(), + label_res_map: self.label_res_map.clone(), + next_node_id: self.next_node_id.clone(), + node_id_to_def_id: self.node_id_to_def_id.clone(), + def_id_to_node_id: self.def_id_to_node_id.clone(), + trait_map: self.trait_map.clone(), }; (definitions, cstore, resolutions) } + fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { + StableHashingContext::new(self.session, &self.definitions, self.crate_loader.cstore()) + } + pub fn cstore(&self) -> &CStore { self.crate_loader.cstore() } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 52685ec697ce8..68040a8d02c0f 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -7,7 +7,6 @@ use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BuiltinMacroState use crate::{CrateLint, DeriveData, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak}; use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding}; use rustc_ast::{self as ast, Inline, ItemKind, ModKind, NodeId}; -use rustc_ast_lowering::ResolverAstLowering; use rustc_ast_pretty::pprust; use rustc_attr::StabilityLevel; use rustc_data_structures::fx::FxHashSet; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 95de28e0c5b26..92b7a551157fb 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -32,7 +32,6 @@ extern crate tracing; // Dependencies listed in Cargo.toml do not need `extern crate`. extern crate rustc_ast; -extern crate rustc_ast_lowering; extern crate rustc_ast_pretty; extern crate rustc_attr; extern crate rustc_const_eval; diff --git a/src/librustdoc/passes/collect_intra_doc_links/early.rs b/src/librustdoc/passes/collect_intra_doc_links/early.rs index 4cebf741e2002..99b31918064fb 100644 --- a/src/librustdoc/passes/collect_intra_doc_links/early.rs +++ b/src/librustdoc/passes/collect_intra_doc_links/early.rs @@ -62,8 +62,6 @@ impl visit::Visitor<'_> for IntraLinkCrateLoader { } fn visit_item(&mut self, item: &ast::Item) { - use rustc_ast_lowering::ResolverAstLowering; - if let ast::ItemKind::Mod(..) = item.kind { let new_mod = self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id));