From 4b08fbaea84a1ecce23af151c9072a02cb9bcdae Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 7 Dec 2022 13:34:05 +0000 Subject: [PATCH 1/4] ResolverTree does not require access to the crate loader, only the store --- compiler/rustc_resolve/src/effective_visibilities.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 85399385d1fd4..3a7d463a62583 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -107,7 +107,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { r.effective_visibilities.update_eff_vis( r.local_def_id(node_id), eff_vis, - ResolverTree(&r.definitions, &r.crate_loader), + ResolverTree(&r.definitions, r.crate_loader.cstore()), ) } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 82f5d0f534a4b..816dc4cb4dcc4 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1112,15 +1112,15 @@ impl<'a> AsMut> for Resolver<'a> { /// A minimal subset of resolver that can implemenent `DefIdTree`, sometimes /// required to satisfy borrow checker by avoiding borrowing the whole resolver. #[derive(Clone, Copy)] -struct ResolverTree<'a, 'b>(&'a Definitions, &'a CrateLoader<'b>); +struct ResolverTree<'a>(&'a Definitions, &'a CStore); -impl DefIdTree for ResolverTree<'_, '_> { +impl DefIdTree for ResolverTree<'_> { #[inline] fn opt_parent(self, id: DefId) -> Option { - let ResolverTree(definitions, crate_loader) = self; + let ResolverTree(definitions, cstore) = self; match id.as_local() { Some(id) => definitions.def_key(id).parent, - None => crate_loader.cstore().def_key(id).parent, + None => cstore.def_key(id).parent, } .map(|index| DefId { index, ..id }) } @@ -1129,7 +1129,7 @@ impl DefIdTree for ResolverTree<'_, '_> { impl<'a, 'b> DefIdTree for &'a Resolver<'b> { #[inline] fn opt_parent(self, id: DefId) -> Option { - ResolverTree(&self.definitions, &self.crate_loader).opt_parent(id) + ResolverTree(&self.definitions, self.cstore()).opt_parent(id) } } From 2cd36f2c8902544371cfb6547be46842a8e7e314 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 7 Dec 2022 13:37:47 +0000 Subject: [PATCH 2/4] Generate crate loaders on the fly --- compiler/rustc_metadata/src/creader.rs | 60 ++++++++++--------- .../rustc_resolve/src/build_reduced_graph.rs | 10 ++-- compiler/rustc_resolve/src/diagnostics.rs | 2 +- .../src/effective_visibilities.rs | 2 +- .../rustc_resolve/src/late/diagnostics.rs | 9 +-- compiler/rustc_resolve/src/lib.rs | 36 +++++++---- compiler/rustc_resolve/src/macros.rs | 2 +- 7 files changed, 67 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index efeaac8fe9a0f..d77630deb7477 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -68,11 +68,12 @@ impl std::fmt::Debug for CStore { pub struct CrateLoader<'a> { // Immutable configuration. sess: &'a Session, - metadata_loader: Box, + metadata_loader: &'a MetadataLoaderDyn, + definitions: &'a Definitions, local_crate_name: Symbol, // Mutable output. - cstore: CStore, - used_extern_options: FxHashSet, + cstore: &'a mut CStore, + used_extern_options: &'a mut FxHashSet, } pub enum LoadedMacro { @@ -239,47 +240,49 @@ impl CStore { ); } } + + pub fn new(sess: &Session) -> CStore { + let mut stable_crate_ids = FxHashMap::default(); + stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE); + CStore { + // We add an empty entry for LOCAL_CRATE (which maps to zero) in + // order to make array indices in `metas` match with the + // corresponding `CrateNum`. This first entry will always remain + // `None`. + metas: IndexVec::from_elem_n(None, 1), + injected_panic_runtime: None, + allocator_kind: None, + alloc_error_handler_kind: None, + has_global_allocator: false, + has_alloc_error_handler: false, + stable_crate_ids, + unused_externs: Vec::new(), + } + } } impl<'a> CrateLoader<'a> { pub fn new( sess: &'a Session, - metadata_loader: Box, + metadata_loader: &'a MetadataLoaderDyn, local_crate_name: Symbol, + cstore: &'a mut CStore, + definitions: &'a Definitions, + used_extern_options: &'a mut FxHashSet, ) -> Self { - let mut stable_crate_ids = FxHashMap::default(); - stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE); - CrateLoader { sess, metadata_loader, local_crate_name, - cstore: CStore { - // We add an empty entry for LOCAL_CRATE (which maps to zero) in - // order to make array indices in `metas` match with the - // corresponding `CrateNum`. This first entry will always remain - // `None`. - metas: IndexVec::from_elem_n(None, 1), - injected_panic_runtime: None, - allocator_kind: None, - alloc_error_handler_kind: None, - has_global_allocator: false, - has_alloc_error_handler: false, - stable_crate_ids, - unused_externs: Vec::new(), - }, - used_extern_options: Default::default(), + cstore, + used_extern_options, + definitions, } } - pub fn cstore(&self) -> &CStore { &self.cstore } - pub fn into_cstore(self) -> CStore { - self.cstore - } - fn existing_match(&self, name: Symbol, hash: Option, kind: PathKind) -> Option { for (cnum, data) in self.cstore.iter_crate_data() { if data.name() != name { @@ -989,7 +992,6 @@ impl<'a> CrateLoader<'a> { pub fn process_extern_crate( &mut self, item: &ast::Item, - definitions: &Definitions, def_id: LocalDefId, ) -> Option { match item.kind { @@ -1013,7 +1015,7 @@ impl<'a> CrateLoader<'a> { let cnum = self.resolve_crate(name, item.span, dep_kind)?; - let path_len = definitions.def_path(def_id).data.len(); + let path_len = self.definitions.def_path(def_id).data.len(); self.update_extern_crate( cnum, ExternCrate { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 9c90d67aadf71..cb93b2599af2c 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -836,12 +836,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } else if orig_name == Some(kw::SelfLower) { Some(self.r.graph_root) } else { - self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id).map( - |crate_id| { - self.r.extern_crate_map.insert(local_def_id, crate_id); - self.r.expect_module(crate_id.as_def_id()) - }, - ) + self.r.crate_loader().process_extern_crate(item, local_def_id).map(|crate_id| { + self.r.extern_crate_map.insert(local_def_id, crate_id); + self.r.expect_module(crate_id.as_def_id()) + }) } .map(|module| { let used = self.process_macro_use_imports(item, module); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f6b6cf3a94c18..7227c02e22f38 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1298,7 +1298,7 @@ impl<'a> Resolver<'a> { // otherwise cause duplicate suggestions. continue; } - if let Some(crate_id) = self.crate_loader.maybe_process_path_extern(ident.name) { + if let Some(crate_id) = self.crate_loader().maybe_process_path_extern(ident.name) { let crate_root = self.expect_module(crate_id.as_def_id()); suggestions.extend(self.lookup_import_candidates_from_module( lookup_ident, diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 3a7d463a62583..33d02db186ae5 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -107,7 +107,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { r.effective_visibilities.update_eff_vis( r.local_def_id(node_id), eff_vis, - ResolverTree(&r.definitions, r.crate_loader.cstore()), + ResolverTree(&r.definitions, &r.cstore), ) } } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index df59a350ea7c9..d43983ea8150f 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1663,8 +1663,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { if !module.no_implicit_prelude { let extern_prelude = self.r.extern_prelude.clone(); names.extend(extern_prelude.iter().flat_map(|(ident, _)| { - self.r.crate_loader.maybe_process_path_extern(ident.name).and_then( - |crate_id| { + self.r + .crate_loader() + .maybe_process_path_extern(ident.name) + .and_then(|crate_id| { let crate_mod = Res::Def(DefKind::Mod, crate_id.as_def_id()); @@ -1673,8 +1675,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } else { None } - }, - ) + }) })); if let Some(prelude) = self.r.prelude { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 816dc4cb4dcc4..2154017fcb3f9 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -954,7 +954,10 @@ pub struct Resolver<'a> { arenas: &'a ResolverArenas<'a>, dummy_binding: &'a NameBinding<'a>, - crate_loader: CrateLoader<'a>, + local_crate_name: Symbol, + metadata_loader: Box, + cstore: CStore, + used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, /// A small map keeping true kinds of built-in macros that appear to be fn-like on @@ -1129,7 +1132,7 @@ impl DefIdTree for ResolverTree<'_> { impl<'a, 'b> DefIdTree for &'a Resolver<'b> { #[inline] fn opt_parent(self, id: DefId) -> Option { - ResolverTree(&self.definitions, self.cstore()).opt_parent(id) + ResolverTree(&self.definitions, &self.cstore).opt_parent(id) } } @@ -1311,7 +1314,10 @@ impl<'a> Resolver<'a> { vis: ty::Visibility::Public, }), - crate_loader: CrateLoader::new(session, metadata_loader, crate_name), + metadata_loader, + local_crate_name: crate_name, + used_extern_options: Default::default(), + cstore: CStore::new(session), macro_names: FxHashSet::default(), builtin_macros: Default::default(), builtin_macro_kinds: Default::default(), @@ -1403,7 +1409,7 @@ impl<'a> Resolver<'a> { pub fn into_outputs(self) -> 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 cstore = Box::new(self.cstore); let source_span = self.source_span; let expn_that_defined = self.expn_that_defined; let visibilities = self.visibilities; @@ -1501,16 +1507,22 @@ impl<'a> Resolver<'a> { } fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new( - self.session, + StableHashingContext::new(self.session, &self.definitions, &self.cstore, &self.source_span) + } + + pub fn crate_loader(&mut self) -> CrateLoader<'_> { + CrateLoader::new( + &self.session, + &*self.metadata_loader, + self.local_crate_name, + &mut self.cstore, &self.definitions, - self.crate_loader.cstore(), - &self.source_span, + &mut self.used_extern_options, ) } pub fn cstore(&self) -> &CStore { - self.crate_loader.cstore() + &self.cstore } fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc { @@ -1553,7 +1565,7 @@ impl<'a> Resolver<'a> { self.session.time("resolve_main", || self.resolve_main()); self.session.time("resolve_check_unused", || self.check_unused(krate)); self.session.time("resolve_report_errors", || self.report_errors(krate)); - self.session.time("resolve_postprocess", || self.crate_loader.postprocess(krate)); + self.session.time("resolve_postprocess", || self.crate_loader().postprocess(krate)); }); } @@ -1871,10 +1883,10 @@ impl<'a> Resolver<'a> { } else { let crate_id = if finalize { let Some(crate_id) = - self.crate_loader.process_path_extern(ident.name, ident.span) else { return Some(self.dummy_binding); }; + self.crate_loader().process_path_extern(ident.name, ident.span) else { return Some(self.dummy_binding); }; crate_id } else { - self.crate_loader.maybe_process_path_extern(ident.name)? + self.crate_loader().maybe_process_path_extern(ident.name)? }; let crate_root = self.expect_module(crate_id.as_def_id()); let vis = ty::Visibility::::Public; diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 8c7972f8eebb9..32f66ef92c31a 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -455,7 +455,7 @@ impl<'a> ResolverExpand for Resolver<'a> { } fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span { - self.crate_loader.cstore().get_proc_macro_quoted_span_untracked(krate, id, self.session) + self.cstore.get_proc_macro_quoted_span_untracked(krate, id, self.session) } fn declare_proc_macro(&mut self, id: NodeId) { From 1c1d3570ee9f160827fdb99208c5afb4ca0cd0f9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 7 Dec 2022 14:31:50 +0000 Subject: [PATCH 3/4] Move the untracked cstore and source_span into a struct --- Cargo.lock | 1 + compiler/rustc_interface/src/passes.rs | 2 + .../src/rmeta/decoder/cstore_impl.rs | 3 + compiler/rustc_middle/src/hir/map/mod.rs | 4 +- compiler/rustc_middle/src/hir/mod.rs | 2 - compiler/rustc_middle/src/query/mod.rs | 2 + compiler/rustc_middle/src/ty/context.rs | 69 ++++++++----------- compiler/rustc_middle/src/ty/mod.rs | 6 +- compiler/rustc_query_system/src/ich/hcx.rs | 20 ++---- compiler/rustc_resolve/src/diagnostics.rs | 6 +- .../src/effective_visibilities.rs | 5 +- compiler/rustc_resolve/src/imports.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 33 ++++----- compiler/rustc_resolve/src/macros.rs | 2 +- compiler/rustc_session/Cargo.toml | 1 + compiler/rustc_session/src/cstore.rs | 11 ++- 16 files changed, 78 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 844cf99b4688d..aed08776f4515 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4342,6 +4342,7 @@ dependencies = [ "rustc_feature", "rustc_fs_util", "rustc_hir", + "rustc_index", "rustc_lint_defs", "rustc_macros", "rustc_serialize", diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index f808c1438bfc5..4748aea54e34d 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -808,6 +808,7 @@ pub fn create_global_ctxt<'tcx>( definitions, global_ctxt: untracked_resolutions, ast_lowering: untracked_resolver_for_lowering, + untracked, } = resolver_outputs; let gcx = sess.time("setup_global_ctxt", || { @@ -819,6 +820,7 @@ pub fn create_global_ctxt<'tcx>( hir_arena, definitions, untracked_resolutions, + untracked, krate, dep_graph, queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn), diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 33cce0a411e81..9d0ccfeb16898 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -629,6 +629,9 @@ impl CrateStore for CStore { fn as_any(&self) -> &dyn Any { self } + fn untracked_as_any(&mut self) -> &mut dyn Any { + self + } fn crate_name(&self, cnum: CrateNum) -> Symbol { self.get_crate_data(cnum).root.name diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 1bd8f95350879..0450abed51b06 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -14,7 +14,7 @@ use rustc_index::vec::Idx; use rustc_middle::hir::nested_filter; use rustc_span::def_id::StableCrateId; use rustc_span::symbol::{kw, sym, Ident, Symbol}; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::Span; use rustc_target::spec::abi::Abi; #[inline] @@ -1162,7 +1162,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { .filter_map(|(def_id, info)| { let _ = info.as_owner()?; let def_path_hash = definitions.def_path_hash(def_id); - let span = resolutions.source_span.get(def_id).unwrap_or(&DUMMY_SP); + let span = tcx.source_span(def_id); debug_assert_eq!(span.parent(), None); Some((def_path_hash, span)) }) diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 02fd03c02839a..3f6e29ad611c9 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -141,8 +141,6 @@ pub fn provide(providers: &mut Providers) { providers.hir_attrs = |tcx, id| { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; - providers.source_span = - |tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP); providers.def_span = |tcx, def_id| { let def_id = def_id.expect_local(); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index f2f2b22f52a37..ab512804330b9 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -43,6 +43,8 @@ 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 { + // Accesses untracked data + eval_always desc { "getting the source span" } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b44bc14ec26c8..47e6f9eaf546c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -59,7 +59,7 @@ use rustc_query_system::dep_graph::DepNodeIndex; use rustc_query_system::ich::StableHashingContext; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{CrateType, OutputFilenames}; -use rustc_session::cstore::CrateStoreDyn; +use rustc_session::cstore::{CrateStoreDyn, Untracked}; use rustc_session::lint::Lint; use rustc_session::Limit; use rustc_session::Session; @@ -187,15 +187,13 @@ impl<'tcx> CtxtInterners<'tcx> { kind: TyKind<'tcx>, sess: &Session, definitions: &rustc_hir::definitions::Definitions, - cstore: &CrateStoreDyn, - source_span: &IndexVec, + untracked: &Untracked, ) -> Ty<'tcx> { Ty(Interned::new_unchecked( self.type_ .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_kind(&kind); - let stable_hash = - self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind); + let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind); InternedInSet(self.arena.alloc(WithCachedTypeInfo { internee: kind, @@ -213,8 +211,7 @@ impl<'tcx> CtxtInterners<'tcx> { flags: &ty::flags::FlagComputation, sess: &'a Session, definitions: &'a rustc_hir::definitions::Definitions, - cstore: &'a CrateStoreDyn, - source_span: &'a IndexVec, + untracked: &'a Untracked, val: &T, ) -> Fingerprint { // It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them. @@ -223,7 +220,7 @@ impl<'tcx> CtxtInterners<'tcx> { Fingerprint::ZERO } else { let mut hasher = StableHasher::new(); - let mut hcx = StableHashingContext::new(sess, definitions, cstore, source_span); + let mut hcx = StableHashingContext::new(sess, definitions, untracked); val.hash_stable(&mut hcx, &mut hasher); hasher.finish() } @@ -235,16 +232,14 @@ impl<'tcx> CtxtInterners<'tcx> { kind: Binder<'tcx, PredicateKind<'tcx>>, sess: &Session, definitions: &rustc_hir::definitions::Definitions, - cstore: &CrateStoreDyn, - source_span: &IndexVec, + untracked: &Untracked, ) -> Predicate<'tcx> { Predicate(Interned::new_unchecked( self.predicate .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_predicate(kind); - let stable_hash = - self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind); + let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind); InternedInSet(self.arena.alloc(WithCachedTypeInfo { internee: kind, @@ -963,10 +958,9 @@ impl<'tcx> CommonTypes<'tcx> { interners: &CtxtInterners<'tcx>, sess: &Session, definitions: &rustc_hir::definitions::Definitions, - cstore: &CrateStoreDyn, - source_span: &IndexVec, + untracked: &Untracked, ) -> CommonTypes<'tcx> { - let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore, source_span); + let mk = |ty| interners.intern_ty(ty, sess, definitions, untracked); CommonTypes { unit: mk(Tuple(List::empty())), @@ -1114,6 +1108,7 @@ pub struct GlobalCtxt<'tcx> { definitions: RwLock, + untracked: Untracked, /// Output of the resolver. pub(crate) untracked_resolutions: ty::ResolverGlobalCtxt, /// The entire crate as AST. This field serves as the input for the hir_crate query, @@ -1280,6 +1275,7 @@ impl<'tcx> TyCtxt<'tcx> { hir_arena: &'tcx WorkerLocal>, definitions: Definitions, untracked_resolutions: ty::ResolverGlobalCtxt, + untracked: Untracked, krate: Lrc, dep_graph: DepGraph, on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>, @@ -1292,14 +1288,7 @@ impl<'tcx> TyCtxt<'tcx> { s.emit_fatal(err); }); let interners = CtxtInterners::new(arena); - let common_types = CommonTypes::new( - &interners, - s, - &definitions, - &*untracked_resolutions.cstore, - // This is only used to create a stable hashing context. - &untracked_resolutions.source_span, - ); + let common_types = CommonTypes::new(&interners, s, &definitions, &untracked); let common_lifetimes = CommonLifetimes::new(&interners); let common_consts = CommonConsts::new(&interners, &common_types); @@ -1315,6 +1304,7 @@ impl<'tcx> TyCtxt<'tcx> { types: common_types, lifetimes: common_lifetimes, consts: common_consts, + untracked, untracked_resolutions, untracked_crate: Steal::new(krate), on_disk_cache, @@ -1428,7 +1418,7 @@ impl<'tcx> TyCtxt<'tcx> { if let Some(id) = id.as_local() { self.definitions_untracked().def_key(id) } else { - self.untracked_resolutions.cstore.def_key(id) + self.untracked.cstore.def_key(id) } } @@ -1442,7 +1432,7 @@ impl<'tcx> TyCtxt<'tcx> { if let Some(id) = id.as_local() { self.definitions_untracked().def_path(id) } else { - self.untracked_resolutions.cstore.def_path(id) + self.untracked.cstore.def_path(id) } } @@ -1452,7 +1442,7 @@ impl<'tcx> TyCtxt<'tcx> { if let Some(def_id) = def_id.as_local() { self.definitions_untracked().def_path_hash(def_id) } else { - self.untracked_resolutions.cstore.def_path_hash(def_id) + self.untracked.cstore.def_path_hash(def_id) } } @@ -1461,7 +1451,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.untracked.cstore.stable_crate_id(crate_num) } } @@ -1472,7 +1462,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.untracked.cstore.stable_crate_id_to_crate_num(stable_crate_id) } } @@ -1491,7 +1481,7 @@ impl<'tcx> TyCtxt<'tcx> { } 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 cstore = &*self.untracked.cstore; let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id); cstore.def_path_hash_to_def_id(cnum, hash) } @@ -1505,7 +1495,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.untracked.cstore; (cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate)) }; @@ -1604,7 +1594,7 @@ 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 CrateStoreDyn { - &*self.untracked_resolutions.cstore + &*self.untracked.cstore } /// Note that this is *untracked* and should only be used within the query @@ -1618,7 +1608,7 @@ impl<'tcx> TyCtxt<'tcx> { /// system if the result is otherwise tracked through queries #[inline] pub fn source_span_untracked(self, def_id: LocalDefId) -> Span { - self.untracked_resolutions.source_span.get(def_id).copied().unwrap_or(DUMMY_SP) + self.untracked.source_span.get(def_id).copied().unwrap_or(DUMMY_SP) } #[inline(always)] @@ -1627,12 +1617,7 @@ impl<'tcx> TyCtxt<'tcx> { f: impl FnOnce(StableHashingContext<'_>) -> R, ) -> R { let definitions = self.definitions_untracked(); - let hcx = StableHashingContext::new( - self.sess, - &*definitions, - &*self.untracked_resolutions.cstore, - &self.untracked_resolutions.source_span, - ); + let hcx = StableHashingContext::new(self.sess, &*definitions, &self.untracked); f(hcx) } @@ -2428,9 +2413,8 @@ impl<'tcx> TyCtxt<'tcx> { st, self.sess, &self.definitions.read(), - &*self.untracked_resolutions.cstore, // This is only used to create a stable hashing context. - &self.untracked_resolutions.source_span, + &self.untracked, ) } @@ -2440,9 +2424,8 @@ impl<'tcx> TyCtxt<'tcx> { binder, self.sess, &self.definitions.read(), - &*self.untracked_resolutions.cstore, // This is only used to create a stable hashing context. - &self.untracked_resolutions.source_span, + &self.untracked, ) } @@ -3124,4 +3107,6 @@ pub fn provide(providers: &mut ty::query::Providers) { // We want to check if the panic handler was defined in this crate tcx.lang_items().panic_impl().map_or(false, |did| did.is_local()) }; + providers.source_span = + |tcx, def_id| tcx.untracked.source_span.get(def_id).copied().unwrap_or(DUMMY_SP); } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index c062e508ee3df..44740b56a8635 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -46,7 +46,7 @@ use rustc_index::vec::IndexVec; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; use rustc_serialize::{Decodable, Encodable}; -use rustc_session::cstore::CrateStoreDyn; +use rustc_session::cstore::Untracked; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ExpnId, Span}; @@ -153,18 +153,16 @@ pub struct ResolverOutputs { pub definitions: Definitions, pub global_ctxt: ResolverGlobalCtxt, pub ast_lowering: ResolverAstLowering, + pub untracked: Untracked, } #[derive(Debug)] pub struct ResolverGlobalCtxt { - pub cstore: Box, pub visibilities: FxHashMap, /// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error. pub has_pub_restricted: bool, /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. pub expn_that_defined: FxHashMap, - /// Reference span for definitions. - pub source_span: IndexVec, pub effective_visibilities: EffectiveVisibilities, pub extern_crate_map: FxHashMap, pub maybe_unused_trait_imports: FxIndexSet, diff --git a/compiler/rustc_query_system/src/ich/hcx.rs b/compiler/rustc_query_system/src/ich/hcx.rs index 6378ec10875d0..1f55c254acc93 100644 --- a/compiler/rustc_query_system/src/ich/hcx.rs +++ b/compiler/rustc_query_system/src/ich/hcx.rs @@ -7,8 +7,7 @@ use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::definitions::{DefPathHash, Definitions}; -use rustc_index::vec::IndexVec; -use rustc_session::cstore::CrateStore; +use rustc_session::cstore::Untracked; use rustc_session::Session; use rustc_span::source_map::SourceMap; use rustc_span::symbol::Symbol; @@ -21,8 +20,7 @@ use rustc_span::{BytePos, CachingSourceMapView, SourceFile, Span, SpanData, DUMM #[derive(Clone)] pub struct StableHashingContext<'a> { definitions: &'a Definitions, - cstore: &'a dyn CrateStore, - source_span: &'a IndexVec, + untracked: &'a Untracked, // The value of `-Z incremental-ignore-spans`. // This field should only be used by `unstable_opts_incremental_ignore_span` incremental_ignore_spans: bool, @@ -49,19 +47,13 @@ pub(super) enum BodyResolver<'tcx> { impl<'a> StableHashingContext<'a> { #[inline] - pub fn new( - sess: &'a Session, - definitions: &'a Definitions, - cstore: &'a dyn CrateStore, - source_span: &'a IndexVec, - ) -> Self { + pub fn new(sess: &'a Session, definitions: &'a Definitions, untracked: &'a Untracked) -> Self { let hash_spans_initial = !sess.opts.unstable_opts.incremental_ignore_spans; StableHashingContext { body_resolver: BodyResolver::Forbidden, definitions, - cstore, - source_span, + untracked, incremental_ignore_spans: sess.opts.unstable_opts.incremental_ignore_spans, caching_source_map: None, raw_source_map: sess.source_map(), @@ -100,7 +92,7 @@ impl<'a> StableHashingContext<'a> { if let Some(def_id) = def_id.as_local() { self.local_def_path_hash(def_id) } else { - self.cstore.def_path_hash(def_id) + self.untracked.cstore.def_path_hash(def_id) } } @@ -156,7 +148,7 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> { #[inline] fn def_span(&self, def_id: LocalDefId) -> Span { - *self.source_span.get(def_id).unwrap_or(&DUMMY_SP) + *self.untracked.source_span.get(def_id).unwrap_or(&DUMMY_SP) } #[inline] diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 7227c02e22f38..f0c82ca7497aa 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -153,7 +153,7 @@ impl<'a> Resolver<'a> { if !candidates.is_empty() { show_candidates( &self.session, - &self.source_span, + &self.untracked.source_span, &mut err, span, &candidates, @@ -682,7 +682,7 @@ impl<'a> Resolver<'a> { } show_candidates( &self.session, - &self.source_span, + &self.untracked.source_span, &mut err, Some(span), &import_suggestions, @@ -1335,7 +1335,7 @@ impl<'a> Resolver<'a> { self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); show_candidates( &self.session, - &self.source_span, + &self.untracked.source_span, err, None, &import_suggestions, diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 33d02db186ae5..f9b7f2c1cbcba 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -107,7 +107,10 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { r.effective_visibilities.update_eff_vis( r.local_def_id(node_id), eff_vis, - ResolverTree(&r.definitions, &r.cstore), + ResolverTree( + &r.definitions, + &r.untracked.cstore.as_any().downcast_ref().unwrap(), + ), ) } } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index b100a8c17cf39..4d896b055268e 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -541,7 +541,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { if let Some(candidate) = &err.candidate { import_candidates( self.r.session, - &self.r.source_span, + &self.r.untracked.source_span, &mut diag, Some(err.span), &candidate, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 2154017fcb3f9..6da3473e42502 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -46,7 +46,7 @@ use rustc_middle::span_bug; use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; -use rustc_session::cstore::{CrateStore, MetadataLoaderDyn}; +use rustc_session::cstore::{CrateStore, MetadataLoaderDyn, Untracked}; use rustc_session::lint::LintBuffer; use rustc_session::Session; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; @@ -869,8 +869,6 @@ pub struct Resolver<'a> { definitions: Definitions, /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. expn_that_defined: FxHashMap, - /// Reference span for definitions. - source_span: IndexVec, graph_root: Module<'a>, @@ -956,7 +954,7 @@ pub struct Resolver<'a> { local_crate_name: Symbol, metadata_loader: Box, - cstore: CStore, + untracked: Untracked, used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, @@ -1132,7 +1130,7 @@ impl DefIdTree for ResolverTree<'_> { impl<'a, 'b> DefIdTree for &'a Resolver<'b> { #[inline] fn opt_parent(self, id: DefId) -> Option { - ResolverTree(&self.definitions, &self.cstore).opt_parent(id) + ResolverTree(&self.definitions, self.cstore()).opt_parent(id) } } @@ -1171,7 +1169,7 @@ impl Resolver<'_> { // A relative span's parent must be an absolute span. debug_assert_eq!(span.data_untracked().parent, None); - let _id = self.source_span.push(span); + let _id = self.untracked.source_span.push(span); debug_assert_eq!(_id, def_id); // Some things for which we allocate `LocalDefId`s don't correspond to @@ -1263,7 +1261,6 @@ impl<'a> Resolver<'a> { definitions, expn_that_defined: Default::default(), - source_span, // The outermost module has def ID 0; this is not reflected in the // AST. @@ -1317,7 +1314,7 @@ impl<'a> Resolver<'a> { metadata_loader, local_crate_name: crate_name, used_extern_options: Default::default(), - cstore: CStore::new(session), + untracked: Untracked { cstore: Box::new(CStore::new(session)), source_span }, macro_names: FxHashSet::default(), builtin_macros: Default::default(), builtin_macro_kinds: Default::default(), @@ -1409,8 +1406,6 @@ impl<'a> Resolver<'a> { pub fn into_outputs(self) -> 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.cstore); - let source_span = self.source_span; let expn_that_defined = self.expn_that_defined; let visibilities = self.visibilities; let has_pub_restricted = self.has_pub_restricted; @@ -1422,9 +1417,8 @@ impl<'a> Resolver<'a> { let main_def = self.main_def; let confused_type_with_std_module = self.confused_type_with_std_module; let effective_visibilities = self.effective_visibilities; + let untracked = self.untracked; let global_ctxt = ResolverGlobalCtxt { - cstore, - source_span, expn_that_defined, visibilities, has_pub_restricted, @@ -1459,16 +1453,15 @@ impl<'a> Resolver<'a> { builtin_macro_kinds: self.builtin_macro_kinds, lifetime_elision_allowed: self.lifetime_elision_allowed, }; - ResolverOutputs { definitions, global_ctxt, ast_lowering } + ResolverOutputs { definitions, global_ctxt, ast_lowering, untracked } } pub fn clone_outputs(&self) -> ResolverOutputs { 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()); + let untracked = Untracked { cstore, source_span: self.untracked.source_span.clone() }; let global_ctxt = ResolverGlobalCtxt { - cstore, - source_span: self.source_span.clone(), expn_that_defined: self.expn_that_defined.clone(), visibilities: self.visibilities.clone(), has_pub_restricted: self.has_pub_restricted, @@ -1503,11 +1496,11 @@ impl<'a> Resolver<'a> { builtin_macro_kinds: self.builtin_macro_kinds.clone(), lifetime_elision_allowed: self.lifetime_elision_allowed.clone(), }; - ResolverOutputs { definitions, global_ctxt, ast_lowering } + ResolverOutputs { definitions, global_ctxt, ast_lowering, untracked } } fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new(self.session, &self.definitions, &self.cstore, &self.source_span) + StableHashingContext::new(self.session, &self.definitions, &self.untracked) } pub fn crate_loader(&mut self) -> CrateLoader<'_> { @@ -1515,14 +1508,14 @@ impl<'a> Resolver<'a> { &self.session, &*self.metadata_loader, self.local_crate_name, - &mut self.cstore, + &mut *self.untracked.cstore.untracked_as_any().downcast_mut().unwrap(), &self.definitions, &mut self.used_extern_options, ) } pub fn cstore(&self) -> &CStore { - &self.cstore + self.untracked.cstore.as_any().downcast_ref().unwrap() } fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc { @@ -1958,7 +1951,7 @@ impl<'a> Resolver<'a> { /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. #[inline] pub fn opt_span(&self, def_id: DefId) -> Option { - def_id.as_local().map(|def_id| self.source_span[def_id]) + def_id.as_local().map(|def_id| self.untracked.source_span[def_id]) } /// Retrieves the name of the given `DefId`. diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 32f66ef92c31a..b5b1602c5e0d3 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -455,7 +455,7 @@ impl<'a> ResolverExpand for Resolver<'a> { } fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span { - self.cstore.get_proc_macro_quoted_span_untracked(krate, id, self.session) + self.cstore().get_proc_macro_quoted_span_untracked(krate, id, self.session) } fn declare_proc_macro(&mut self, id: NodeId) { diff --git a/compiler/rustc_session/Cargo.toml b/compiler/rustc_session/Cargo.toml index cbbba2252bf60..d8db86c5f6216 100644 --- a/compiler/rustc_session/Cargo.toml +++ b/compiler/rustc_session/Cargo.toml @@ -13,6 +13,7 @@ rustc_hir = { path = "../rustc_hir" } rustc_target = { path = "../rustc_target" } rustc_serialize = { path = "../rustc_serialize" } rustc_data_structures = { path = "../rustc_data_structures" } +rustc_index = { path = "../rustc_index" } rustc_span = { path = "../rustc_span" } rustc_fs_util = { path = "../rustc_fs_util" } rustc_ast = { path = "../rustc_ast" } diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index 7d4a1e212a417..c54565b0f2987 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -7,8 +7,9 @@ use crate::utils::NativeLibKind; use crate::Session; use rustc_ast as ast; use rustc_data_structures::sync::{self, MetadataRef}; -use rustc_hir::def_id::{CrateNum, DefId, StableCrateId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; +use rustc_index::vec::IndexVec; use rustc_span::hygiene::{ExpnHash, ExpnId}; use rustc_span::symbol::Symbol; use rustc_span::Span; @@ -217,6 +218,7 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync; /// during resolve) pub trait CrateStore: std::fmt::Debug { fn as_any(&self) -> &dyn Any; + fn untracked_as_any(&mut self) -> &mut dyn Any; // Foreign definitions. // This information is safe to access, since it's hashed as part of the DefPathHash, which incr. @@ -249,3 +251,10 @@ pub trait CrateStore: std::fmt::Debug { } pub type CrateStoreDyn = dyn CrateStore + sync::Sync; + +#[derive(Debug)] +pub struct Untracked { + pub cstore: Box, + /// Reference span for definitions. + pub source_span: IndexVec, +} From 75ff5c7dd3c50371f77ae29d43f87343d44b3829 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 8 Dec 2022 08:52:07 +0000 Subject: [PATCH 4/4] Fold `Definitions` into the untracked data --- compiler/rustc_interface/src/passes.rs | 2 - compiler/rustc_metadata/src/creader.rs | 6 +-- compiler/rustc_middle/src/ty/context.rs | 45 ++++++------------- compiler/rustc_middle/src/ty/mod.rs | 2 - compiler/rustc_query_system/src/ich/hcx.rs | 8 ++-- .../rustc_resolve/src/build_reduced_graph.rs | 3 +- compiler/rustc_resolve/src/diagnostics.rs | 3 +- .../src/effective_visibilities.rs | 5 +-- compiler/rustc_resolve/src/lib.rs | 40 +++++++++-------- compiler/rustc_session/src/cstore.rs | 5 ++- 10 files changed, 49 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 4748aea54e34d..89d9450cf4e89 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -805,7 +805,6 @@ pub fn create_global_ctxt<'tcx>( }); let ty::ResolverOutputs { - definitions, global_ctxt: untracked_resolutions, ast_lowering: untracked_resolver_for_lowering, untracked, @@ -818,7 +817,6 @@ pub fn create_global_ctxt<'tcx>( lint_store, arena, hir_arena, - definitions, untracked_resolutions, untracked, krate, diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index d77630deb7477..01d7f3e03c50a 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -13,7 +13,7 @@ use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::{self as ast, *}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::svh::Svh; -use rustc_data_structures::sync::Lrc; +use rustc_data_structures::sync::{Lrc, ReadGuard}; use rustc_expand::base::SyntaxExtension; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; @@ -69,7 +69,7 @@ pub struct CrateLoader<'a> { // Immutable configuration. sess: &'a Session, metadata_loader: &'a MetadataLoaderDyn, - definitions: &'a Definitions, + definitions: ReadGuard<'a, Definitions>, local_crate_name: Symbol, // Mutable output. cstore: &'a mut CStore, @@ -267,7 +267,7 @@ impl<'a> CrateLoader<'a> { metadata_loader: &'a MetadataLoaderDyn, local_crate_name: Symbol, cstore: &'a mut CStore, - definitions: &'a Definitions, + definitions: ReadGuard<'a, Definitions>, used_extern_options: &'a mut FxHashSet, ) -> Self { CrateLoader { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 47e6f9eaf546c..7d4971d1e9e62 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -35,7 +35,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::steal::Steal; -use rustc_data_structures::sync::{self, Lock, Lrc, ReadGuard, RwLock, WorkerLocal}; +use rustc_data_structures::sync::{self, Lock, Lrc, ReadGuard, WorkerLocal}; use rustc_data_structures::unord::UnordSet; use rustc_data_structures::vec_map::VecMap; use rustc_errors::{ @@ -182,18 +182,12 @@ impl<'tcx> CtxtInterners<'tcx> { /// Interns a type. #[allow(rustc::usage_of_ty_tykind)] #[inline(never)] - fn intern_ty( - &self, - kind: TyKind<'tcx>, - sess: &Session, - definitions: &rustc_hir::definitions::Definitions, - untracked: &Untracked, - ) -> Ty<'tcx> { + fn intern_ty(&self, kind: TyKind<'tcx>, sess: &Session, untracked: &Untracked) -> Ty<'tcx> { Ty(Interned::new_unchecked( self.type_ .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_kind(&kind); - let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind); + let stable_hash = self.stable_hash(&flags, sess, untracked, &kind); InternedInSet(self.arena.alloc(WithCachedTypeInfo { internee: kind, @@ -210,7 +204,6 @@ impl<'tcx> CtxtInterners<'tcx> { &self, flags: &ty::flags::FlagComputation, sess: &'a Session, - definitions: &'a rustc_hir::definitions::Definitions, untracked: &'a Untracked, val: &T, ) -> Fingerprint { @@ -220,7 +213,7 @@ impl<'tcx> CtxtInterners<'tcx> { Fingerprint::ZERO } else { let mut hasher = StableHasher::new(); - let mut hcx = StableHashingContext::new(sess, definitions, untracked); + let mut hcx = StableHashingContext::new(sess, untracked); val.hash_stable(&mut hcx, &mut hasher); hasher.finish() } @@ -231,7 +224,6 @@ impl<'tcx> CtxtInterners<'tcx> { &self, kind: Binder<'tcx, PredicateKind<'tcx>>, sess: &Session, - definitions: &rustc_hir::definitions::Definitions, untracked: &Untracked, ) -> Predicate<'tcx> { Predicate(Interned::new_unchecked( @@ -239,7 +231,7 @@ impl<'tcx> CtxtInterners<'tcx> { .intern(kind, |kind| { let flags = super::flags::FlagComputation::for_predicate(kind); - let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind); + let stable_hash = self.stable_hash(&flags, sess, untracked, &kind); InternedInSet(self.arena.alloc(WithCachedTypeInfo { internee: kind, @@ -957,10 +949,9 @@ impl<'tcx> CommonTypes<'tcx> { fn new( interners: &CtxtInterners<'tcx>, sess: &Session, - definitions: &rustc_hir::definitions::Definitions, untracked: &Untracked, ) -> CommonTypes<'tcx> { - let mk = |ty| interners.intern_ty(ty, sess, definitions, untracked); + let mk = |ty| interners.intern_ty(ty, sess, untracked); CommonTypes { unit: mk(Tuple(List::empty())), @@ -1106,8 +1097,6 @@ pub struct GlobalCtxt<'tcx> { /// Common consts, pre-interned for your convenience. pub consts: CommonConsts<'tcx>, - definitions: RwLock, - untracked: Untracked, /// Output of the resolver. pub(crate) untracked_resolutions: ty::ResolverGlobalCtxt, @@ -1273,7 +1262,6 @@ impl<'tcx> TyCtxt<'tcx> { lint_store: Lrc, arena: &'tcx WorkerLocal>, hir_arena: &'tcx WorkerLocal>, - definitions: Definitions, untracked_resolutions: ty::ResolverGlobalCtxt, untracked: Untracked, krate: Lrc, @@ -1288,7 +1276,7 @@ impl<'tcx> TyCtxt<'tcx> { s.emit_fatal(err); }); let interners = CtxtInterners::new(arena); - let common_types = CommonTypes::new(&interners, s, &definitions, &untracked); + let common_types = CommonTypes::new(&interners, s, &untracked); let common_lifetimes = CommonLifetimes::new(&interners); let common_consts = CommonConsts::new(&interners, &common_types); @@ -1299,7 +1287,6 @@ impl<'tcx> TyCtxt<'tcx> { hir_arena, interners, dep_graph, - definitions: RwLock::new(definitions), prof: s.prof.clone(), types: common_types, lifetimes: common_lifetimes, @@ -1477,7 +1464,7 @@ 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.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id() + self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id() } else { // If this is a DefPathHash from an upstream crate, let the CrateStore map // it to a DefId. @@ -1537,7 +1524,7 @@ impl<'tcx> TyCtxtAt<'tcx> { // This is fine because: // - those queries are `eval_always` so we won't miss their result changing; // - this write will have happened before these queries are called. - let key = self.definitions.write().create_def(parent, data); + let key = self.untracked.definitions.write().create_def(parent, data); let feed = TyCtxtFeed { tcx: self.tcx, key }; feed.def_span(self.span); @@ -1551,7 +1538,7 @@ impl<'tcx> TyCtxt<'tcx> { // definitions change. self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE); - let definitions = &self.definitions; + let definitions = &self.untracked.definitions; std::iter::from_generator(|| { let mut i = 0; @@ -1575,7 +1562,7 @@ impl<'tcx> TyCtxt<'tcx> { // Leak a read lock once we start iterating on definitions, to prevent adding new ones // while iterating. If some query needs to add definitions, it should be `ensure`d above. - let definitions = self.definitions.leak(); + let definitions = self.untracked.definitions.leak(); definitions.def_path_table() } @@ -1587,7 +1574,7 @@ impl<'tcx> TyCtxt<'tcx> { self.ensure().hir_crate(()); // Leak a read lock once we start iterating on definitions, to prevent adding new ones // while iterating. If some query needs to add definitions, it should be `ensure`d above. - let definitions = self.definitions.leak(); + let definitions = self.untracked.definitions.leak(); definitions.def_path_hash_to_def_index_map() } @@ -1601,7 +1588,7 @@ impl<'tcx> TyCtxt<'tcx> { /// system if the result is otherwise tracked through queries #[inline] pub fn definitions_untracked(self) -> ReadGuard<'tcx, Definitions> { - self.definitions.read() + self.untracked.definitions.read() } /// Note that this is *untracked* and should only be used within the query @@ -1616,9 +1603,7 @@ impl<'tcx> TyCtxt<'tcx> { self, f: impl FnOnce(StableHashingContext<'_>) -> R, ) -> R { - let definitions = self.definitions_untracked(); - let hcx = StableHashingContext::new(self.sess, &*definitions, &self.untracked); - f(hcx) + f(StableHashingContext::new(self.sess, &self.untracked)) } pub fn serialize_query_result_cache(self, encoder: FileEncoder) -> FileEncodeResult { @@ -2412,7 +2397,6 @@ impl<'tcx> TyCtxt<'tcx> { self.interners.intern_ty( st, self.sess, - &self.definitions.read(), // This is only used to create a stable hashing context. &self.untracked, ) @@ -2423,7 +2407,6 @@ impl<'tcx> TyCtxt<'tcx> { self.interners.intern_predicate( binder, self.sess, - &self.definitions.read(), // This is only used to create a stable hashing context. &self.untracked, ) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 44740b56a8635..659d99f025da0 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -40,7 +40,6 @@ use rustc_data_structures::tagged_ptr::CopyTaggedPtr; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, LifetimeRes, Res}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap}; -use rustc_hir::definitions::Definitions; use rustc_hir::Node; use rustc_index::vec::IndexVec; use rustc_macros::HashStable; @@ -150,7 +149,6 @@ mod sty; pub type RegisteredTools = FxHashSet; pub struct ResolverOutputs { - pub definitions: Definitions, pub global_ctxt: ResolverGlobalCtxt, pub ast_lowering: ResolverAstLowering, pub untracked: Untracked, diff --git a/compiler/rustc_query_system/src/ich/hcx.rs b/compiler/rustc_query_system/src/ich/hcx.rs index 1f55c254acc93..163da59edd5c7 100644 --- a/compiler/rustc_query_system/src/ich/hcx.rs +++ b/compiler/rustc_query_system/src/ich/hcx.rs @@ -6,7 +6,7 @@ use rustc_data_structures::stable_hasher::{HashStable, HashingControls, StableHa use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_hir::definitions::{DefPathHash, Definitions}; +use rustc_hir::definitions::DefPathHash; use rustc_session::cstore::Untracked; use rustc_session::Session; use rustc_span::source_map::SourceMap; @@ -19,7 +19,6 @@ use rustc_span::{BytePos, CachingSourceMapView, SourceFile, Span, SpanData, DUMM /// things (e.g., each `DefId`/`DefPath` is only hashed once). #[derive(Clone)] pub struct StableHashingContext<'a> { - definitions: &'a Definitions, untracked: &'a Untracked, // The value of `-Z incremental-ignore-spans`. // This field should only be used by `unstable_opts_incremental_ignore_span` @@ -47,12 +46,11 @@ pub(super) enum BodyResolver<'tcx> { impl<'a> StableHashingContext<'a> { #[inline] - pub fn new(sess: &'a Session, definitions: &'a Definitions, untracked: &'a Untracked) -> Self { + pub fn new(sess: &'a Session, untracked: &'a Untracked) -> Self { let hash_spans_initial = !sess.opts.unstable_opts.incremental_ignore_spans; StableHashingContext { body_resolver: BodyResolver::Forbidden, - definitions, untracked, incremental_ignore_spans: sess.opts.unstable_opts.incremental_ignore_spans, caching_source_map: None, @@ -98,7 +96,7 @@ impl<'a> StableHashingContext<'a> { #[inline] pub fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash { - self.definitions.def_path_hash(def_id) + self.untracked.definitions.read().def_path_hash(def_id) } #[inline] diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index cb93b2599af2c..f4a6a08df1c85 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -836,7 +836,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } else if orig_name == Some(kw::SelfLower) { Some(self.r.graph_root) } else { - self.r.crate_loader().process_extern_crate(item, local_def_id).map(|crate_id| { + let crate_id = self.r.crate_loader().process_extern_crate(item, local_def_id); + crate_id.map(|crate_id| { self.r.extern_crate_map.insert(local_def_id, crate_id); self.r.expect_module(crate_id.as_def_id()) }) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f0c82ca7497aa..37771693417b3 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1298,7 +1298,8 @@ impl<'a> Resolver<'a> { // otherwise cause duplicate suggestions. continue; } - if let Some(crate_id) = self.crate_loader().maybe_process_path_extern(ident.name) { + let crate_id = self.crate_loader().maybe_process_path_extern(ident.name); + if let Some(crate_id) = crate_id { let crate_root = self.expect_module(crate_id.as_def_id()); suggestions.extend(self.lookup_import_candidates_from_module( lookup_ident, diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index f9b7f2c1cbcba..b8efa3f8b2743 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -107,10 +107,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { r.effective_visibilities.update_eff_vis( r.local_def_id(node_id), eff_vis, - ResolverTree( - &r.definitions, - &r.untracked.cstore.as_any().downcast_ref().unwrap(), - ), + ResolverTree(&r.untracked), ) } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 6da3473e42502..24e4b5bdd3f50 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -29,7 +29,7 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID}; use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::intern::Interned; -use rustc_data_structures::sync::Lrc; +use rustc_data_structures::sync::{Lrc, RwLock}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::*; @@ -866,7 +866,6 @@ struct MacroData { pub struct Resolver<'a> { session: &'a Session, - definitions: Definitions, /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. expn_that_defined: FxHashMap, @@ -1113,15 +1112,15 @@ impl<'a> AsMut> for Resolver<'a> { /// A minimal subset of resolver that can implemenent `DefIdTree`, sometimes /// required to satisfy borrow checker by avoiding borrowing the whole resolver. #[derive(Clone, Copy)] -struct ResolverTree<'a>(&'a Definitions, &'a CStore); +struct ResolverTree<'a>(&'a Untracked); impl DefIdTree for ResolverTree<'_> { #[inline] fn opt_parent(self, id: DefId) -> Option { - let ResolverTree(definitions, cstore) = self; + let ResolverTree(Untracked { definitions, cstore, .. }) = self; match id.as_local() { - Some(id) => definitions.def_key(id).parent, - None => cstore.def_key(id).parent, + Some(id) => definitions.read().def_key(id).parent, + None => cstore.as_any().downcast_ref::().unwrap().def_key(id).parent, } .map(|index| DefId { index, ..id }) } @@ -1130,7 +1129,7 @@ impl DefIdTree for ResolverTree<'_> { impl<'a, 'b> DefIdTree for &'a Resolver<'b> { #[inline] fn opt_parent(self, id: DefId) -> Option { - ResolverTree(&self.definitions, self.cstore()).opt_parent(id) + ResolverTree(&self.untracked).opt_parent(id) } } @@ -1157,10 +1156,10 @@ impl Resolver<'_> { "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", node_id, data, - self.definitions.def_key(self.node_id_to_def_id[&node_id]), + self.untracked.definitions.read().def_key(self.node_id_to_def_id[&node_id]), ); - let def_id = self.definitions.create_def(parent, data); + let def_id = self.untracked.definitions.write().create_def(parent, data); // Create the definition. if expn_id != ExpnId::root() { @@ -1259,7 +1258,6 @@ impl<'a> Resolver<'a> { let mut resolver = Resolver { session, - definitions, expn_that_defined: Default::default(), // The outermost module has def ID 0; this is not reflected in the @@ -1314,7 +1312,11 @@ impl<'a> Resolver<'a> { metadata_loader, local_crate_name: crate_name, used_extern_options: Default::default(), - untracked: Untracked { cstore: Box::new(CStore::new(session)), source_span }, + untracked: Untracked { + cstore: Box::new(CStore::new(session)), + source_span, + definitions: RwLock::new(definitions), + }, macro_names: FxHashSet::default(), builtin_macros: Default::default(), builtin_macro_kinds: Default::default(), @@ -1405,7 +1407,6 @@ impl<'a> Resolver<'a> { pub fn into_outputs(self) -> ResolverOutputs { let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); - let definitions = self.definitions; let expn_that_defined = self.expn_that_defined; let visibilities = self.visibilities; let has_pub_restricted = self.has_pub_restricted; @@ -1453,14 +1454,15 @@ impl<'a> Resolver<'a> { builtin_macro_kinds: self.builtin_macro_kinds, lifetime_elision_allowed: self.lifetime_elision_allowed, }; - ResolverOutputs { definitions, global_ctxt, ast_lowering, untracked } + ResolverOutputs { global_ctxt, ast_lowering, untracked } } pub fn clone_outputs(&self) -> ResolverOutputs { let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect(); - let definitions = self.definitions.clone(); + let definitions = self.untracked.definitions.clone(); let cstore = Box::new(self.cstore().clone()); - let untracked = Untracked { cstore, source_span: self.untracked.source_span.clone() }; + let untracked = + Untracked { cstore, source_span: self.untracked.source_span.clone(), definitions }; let global_ctxt = ResolverGlobalCtxt { expn_that_defined: self.expn_that_defined.clone(), visibilities: self.visibilities.clone(), @@ -1496,11 +1498,11 @@ impl<'a> Resolver<'a> { builtin_macro_kinds: self.builtin_macro_kinds.clone(), lifetime_elision_allowed: self.lifetime_elision_allowed.clone(), }; - ResolverOutputs { definitions, global_ctxt, ast_lowering, untracked } + ResolverOutputs { global_ctxt, ast_lowering, untracked } } fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new(self.session, &self.definitions, &self.untracked) + StableHashingContext::new(self.session, &self.untracked) } pub fn crate_loader(&mut self) -> CrateLoader<'_> { @@ -1509,7 +1511,7 @@ impl<'a> Resolver<'a> { &*self.metadata_loader, self.local_crate_name, &mut *self.untracked.cstore.untracked_as_any().downcast_mut().unwrap(), - &self.definitions, + self.untracked.definitions.read(), &mut self.used_extern_options, ) } @@ -1958,7 +1960,7 @@ impl<'a> Resolver<'a> { #[inline] pub fn opt_name(&self, def_id: DefId) -> Option { let def_key = match def_id.as_local() { - Some(def_id) => self.definitions.def_key(def_id), + Some(def_id) => self.untracked.definitions.read().def_key(def_id), None => self.cstore().def_key(def_id), }; def_key.get_opt_name() diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index c54565b0f2987..7f926f7d8bc4a 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -6,9 +6,9 @@ use crate::search_paths::PathKind; use crate::utils::NativeLibKind; use crate::Session; use rustc_ast as ast; -use rustc_data_structures::sync::{self, MetadataRef}; +use rustc_data_structures::sync::{self, MetadataRef, RwLock}; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE}; -use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; +use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions}; use rustc_index::vec::IndexVec; use rustc_span::hygiene::{ExpnHash, ExpnId}; use rustc_span::symbol::Symbol; @@ -257,4 +257,5 @@ pub struct Untracked { pub cstore: Box, /// Reference span for definitions. pub source_span: IndexVec, + pub definitions: RwLock, }