From ade3dceb38c6e41e3b8623e252d9413052e3a0af Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 8 Dec 2022 10:53:20 +0000 Subject: Make untracked.cstore lockable so that resolution can still write to it when using TyCtxt --- compiler/rustc_interface/src/passes.rs | 2 +- compiler/rustc_interface/src/queries.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 6a94d19001e..43882e10303 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -597,7 +597,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { } } - write_out_deps(sess, tcx.cstore_untracked(), &outputs, &output_paths); + write_out_deps(sess, &*tcx.cstore_untracked(), &outputs, &output_paths); let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo) && sess.opts.output_types.len() == 1; diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index d727efdafc2..bc7c78a3108 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -215,6 +215,9 @@ impl<'tcx> Queries<'tcx> { ast_lowering: untracked_resolver_for_lowering, } = resolver_outputs; + // Make sure we don't mutate the cstore from here on. + std::mem::forget(untracked.cstore.read()); + let gcx = passes::create_global_ctxt( self.compiler, lint_store, -- cgit 1.4.1-3-g733a5 From 8f132d85498e12e0a248f29644d74fcac15f0d96 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 15 Feb 2023 17:19:38 +0000 Subject: Run the resolver after TyCtxt construction --- Cargo.lock | 1 + compiler/rustc_interface/Cargo.toml | 1 + compiler/rustc_interface/src/queries.rs | 91 +++++++++++++++++++-------------- compiler/rustc_middle/src/ty/context.rs | 4 ++ compiler/rustc_middle/src/ty/mod.rs | 2 - compiler/rustc_resolve/src/lib.rs | 27 ++++------ 6 files changed, 69 insertions(+), 57 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/Cargo.lock b/Cargo.lock index d02cab38ae8..6c17edd3020 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4180,6 +4180,7 @@ dependencies = [ "rustc_hir_analysis", "rustc_hir_typeck", "rustc_incremental", + "rustc_index", "rustc_lint", "rustc_macros", "rustc_metadata", diff --git a/compiler/rustc_interface/Cargo.toml b/compiler/rustc_interface/Cargo.toml index 955ab3c4680..ac6e8fca695 100644 --- a/compiler/rustc_interface/Cargo.toml +++ b/compiler/rustc_interface/Cargo.toml @@ -24,6 +24,7 @@ rustc_middle = { path = "../rustc_middle" } rustc_ast_lowering = { path = "../rustc_ast_lowering" } rustc_ast_passes = { path = "../rustc_ast_passes" } rustc_incremental = { path = "../rustc_incremental" } +rustc_index = { path = "../rustc_index" } rustc_traits = { path = "../rustc_traits" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_codegen_ssa = { path = "../rustc_codegen_ssa" } diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index bc7c78a3108..dcce5e53fb2 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -7,16 +7,20 @@ use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::CodegenResults; use rustc_data_structures::steal::Steal; use rustc_data_structures::svh::Svh; -use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; -use rustc_hir::def_id::LOCAL_CRATE; +use rustc_data_structures::sync::{Lrc, OnceCell, RwLock, WorkerLocal}; +use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::definitions::Definitions; use rustc_incremental::DepGraphFuture; +use rustc_index::vec::IndexVec; use rustc_lint::LintStore; +use rustc_metadata::creader::CStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; use rustc_middle::ty::{self, GlobalCtxt, TyCtxt}; use rustc_query_impl::Queries as TcxQueries; use rustc_resolve::Resolver; use rustc_session::config::{self, OutputFilenames, OutputType}; +use rustc_session::cstore::Untracked; use rustc_session::{output::find_crate_name, Session}; use rustc_span::symbol::sym; use rustc_span::Symbol; @@ -187,40 +191,20 @@ impl<'tcx> Queries<'tcx> { self.gcx.compute(|| { let crate_name = *self.crate_name()?.borrow(); let (krate, lint_store) = self.register_plugins()?.steal(); - let (krate, resolver_outputs) = { - let _timer = self.session().timer("configure_and_expand"); - let sess = self.session(); - - let arenas = Resolver::arenas(); - let mut resolver = Resolver::new( - sess, - &krate, - crate_name, - self.codegen_backend().metadata_loader(), - &arenas, - ); - let krate = passes::configure_and_expand( - sess, - &lint_store, - krate, - crate_name, - &mut resolver, - )?; - (Lrc::new(krate), resolver.into_outputs()) - }; - - let ty::ResolverOutputs { - untracked, - global_ctxt: untracked_resolutions, - ast_lowering: untracked_resolver_for_lowering, - } = resolver_outputs; - // Make sure we don't mutate the cstore from here on. - std::mem::forget(untracked.cstore.read()); + let sess = self.session(); - let gcx = passes::create_global_ctxt( + let cstore = RwLock::new(Box::new(CStore::new(sess)) as _); + let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id())); + let mut source_span = IndexVec::default(); + let _id = source_span.push(krate.spans.inner_span); + debug_assert_eq!(_id, CRATE_DEF_ID); + let source_span = RwLock::new(source_span); + let untracked = Untracked { cstore, source_span, definitions }; + + let qcx = passes::create_global_ctxt( self.compiler, - lint_store, + lint_store.clone(), self.dep_graph()?.steal(), untracked, &self.queries, @@ -229,17 +213,48 @@ impl<'tcx> Queries<'tcx> { &self.hir_arena, ); - gcx.enter(|tcx| { + qcx.enter(|tcx| { + let feed = tcx.feed_local_crate(); + feed.crate_name(crate_name); + let (krate, resolver_outputs) = { + let _timer = sess.timer("configure_and_expand"); + + let arenas = Resolver::arenas(); + let mut resolver = Resolver::new( + sess, + &krate, + crate_name, + self.codegen_backend().metadata_loader(), + &arenas, + tcx.untracked(), + ); + let krate = passes::configure_and_expand( + sess, + &lint_store, + krate, + crate_name, + &mut resolver, + )?; + + // Make sure we don't mutate the cstore from here on. + tcx.untracked().cstore.leak(); + (Lrc::new(krate), resolver.into_outputs()) + }; + + let ty::ResolverOutputs { + global_ctxt: untracked_resolutions, + ast_lowering: untracked_resolver_for_lowering, + } = resolver_outputs; + let feed = tcx.feed_unit_query(); feed.resolver_for_lowering( tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))), ); feed.resolutions(tcx.arena.alloc(untracked_resolutions)); feed.features_query(tcx.sess.features_untracked()); - let feed = tcx.feed_local_crate(); - feed.crate_name(crate_name); - }); - Ok(gcx) + Ok(()) + })?; + Ok(qcx) }) } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 5fcbf87a982..cf4836ded47 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1015,6 +1015,10 @@ impl<'tcx> TyCtxt<'tcx> { ReadGuard::map(self.untracked.cstore.read(), |c| &**c) } + /// Give out access to the untracked data without any sanity checks. + pub fn untracked(self) -> &'tcx Untracked { + &self.untracked + } /// Note that this is *untracked* and should only be used within the query /// system if the result is otherwise tracked through queries #[inline] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 1dc27ce8dae..f61fe707ac9 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -43,7 +43,6 @@ use rustc_index::vec::IndexVec; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; use rustc_serialize::{Decodable, Encodable}; -use rustc_session::cstore::Untracked; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ExpnId, ExpnKind, Span}; @@ -157,7 +156,6 @@ pub type RegisteredTools = FxHashSet; pub struct ResolverOutputs { pub global_ctxt: ResolverGlobalCtxt, pub ast_lowering: ResolverAstLowering, - pub untracked: Untracked, } #[derive(Debug)] diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 7a8e594a8f9..f8bfb113d83 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -27,14 +27,14 @@ 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, MappedReadGuard, ReadGuard, RwLock}; +use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId}; use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; -use rustc_hir::definitions::{DefPathData, Definitions}; +use rustc_hir::definitions::DefPathData; use rustc_hir::TraitCandidate; use rustc_index::vec::IndexVec; use rustc_metadata::creader::{CStore, CrateLoader}; @@ -962,7 +962,7 @@ pub struct Resolver<'a, 'tcx> { local_crate_name: Symbol, metadata_loader: Box, - untracked: Untracked, + untracked: &'tcx Untracked, used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, @@ -1211,6 +1211,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { crate_name: Symbol, metadata_loader: Box, arenas: &'a ResolverArenas<'a>, + untracked: &'tcx Untracked, ) -> Resolver<'a, 'tcx> { let tcx = TyCtxt { sess: session }; @@ -1233,8 +1234,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { &mut FxHashMap::default(), ); - let definitions = Definitions::new(session.local_stable_crate_id()); - let mut visibilities = FxHashMap::default(); visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public); @@ -1246,10 +1245,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let mut invocation_parents = FxHashMap::default(); invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential)); - let mut source_span = IndexVec::default(); - let _id = source_span.push(krate.spans.inner_span); - debug_assert_eq!(_id, CRATE_DEF_ID); - let mut extern_prelude: FxHashMap> = session .opts .externs @@ -1327,11 +1322,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { metadata_loader, local_crate_name: crate_name, used_extern_options: Default::default(), - untracked: Untracked { - cstore: RwLock::new(Box::new(CStore::new(session))), - source_span: RwLock::new(source_span), - definitions: RwLock::new(definitions), - }, + untracked, macro_names: FxHashSet::default(), builtin_macros: Default::default(), builtin_macro_kinds: Default::default(), @@ -1436,7 +1427,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { 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 { expn_that_defined, visibilities, @@ -1475,11 +1465,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { builtin_macro_kinds: self.builtin_macro_kinds, lifetime_elision_allowed: self.lifetime_elision_allowed, }; - ResolverOutputs { global_ctxt, ast_lowering, untracked } + ResolverOutputs { global_ctxt, ast_lowering } } fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new(self.tcx.sess, &self.untracked) + StableHashingContext::new(self.tcx.sess, self.untracked) } fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T { @@ -1543,6 +1533,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { .sess .time("resolve_postprocess", || self.crate_loader(|c| c.postprocess(krate))); }); + + // Make sure we don't mutate the cstore from here on. + self.untracked.cstore.leak(); } fn traits_in_scope( -- cgit 1.4.1-3-g733a5 From 4953d70e2fb5f023682cbc7c9e4aa0c5c8619664 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 9 Dec 2022 22:53:31 +0000 Subject: Stuff a TyCtxt into the Resolver --- compiler/rustc_interface/src/queries.rs | 3 +- compiler/rustc_resolve/src/diagnostics.rs | 6 +-- .../rustc_resolve/src/effective_visibilities.rs | 2 +- compiler/rustc_resolve/src/imports.rs | 4 +- compiler/rustc_resolve/src/lib.rs | 58 ++++++++++------------ 5 files changed, 32 insertions(+), 41 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index dcce5e53fb2..7689e3cec06 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -221,12 +221,11 @@ impl<'tcx> Queries<'tcx> { let arenas = Resolver::arenas(); let mut resolver = Resolver::new( - sess, + tcx, &krate, crate_name, self.codegen_backend().metadata_loader(), &arenas, - tcx.untracked(), ); let krate = passes::configure_and_expand( sess, diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 6bb6e92da9e..431a92ecc69 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -155,7 +155,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { if !candidates.is_empty() { show_candidates( &self.tcx.sess, - &self.untracked.source_span.read(), + &self.tcx.untracked().source_span.read(), &mut err, span, &candidates, @@ -688,7 +688,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } show_candidates( &self.tcx.sess, - &self.untracked.source_span.read(), + &self.tcx.untracked().source_span.read(), &mut err, Some(span), &import_suggestions, @@ -1353,7 +1353,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); show_candidates( &self.tcx.sess, - &self.untracked.source_span.read(), + &self.tcx.untracked().source_span.read(), err, None, &import_suggestions, diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index 0079c3e526d..945758878a5 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -110,7 +110,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> { r.effective_visibilities.update_eff_vis( r.local_def_id(node_id), eff_vis, - ResolverTree(&r.untracked), + ResolverTree(&r.tcx.untracked()), ) } } diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 6171dd657bf..56a64dab80e 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -549,7 +549,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> { match &import.kind { ImportKind::Single { nested: false, source, target, .. } => import_candidates( self.r.tcx.sess, - &self.r.untracked.source_span.read(), + &self.r.tcx.untracked().source_span.read(), &mut diag, Some(err.span), &candidates, @@ -562,7 +562,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> { ImportKind::Single { nested: true, source, target, .. } => { import_candidates( self.r.tcx.sess, - &self.r.untracked.source_span.read(), + &self.r.tcx.untracked().source_span.read(), &mut diag, None, &candidates, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index f8bfb113d83..dc055355e16 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -27,7 +27,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, MappedReadGuard, ReadGuard}; +use rustc_data_structures::sync::{Lrc, MappedReadGuard}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::{self, *}; @@ -41,12 +41,11 @@ use rustc_metadata::creader::{CStore, CrateLoader}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::privacy::EffectiveVisibilities; use rustc_middle::span_bug; -use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools}; +use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, TyCtxt}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; 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}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; @@ -861,10 +860,6 @@ struct MacroData { macro_rules: bool, } -struct TyCtxt<'tcx> { - sess: &'tcx Session, -} - /// The main resolver class. /// /// This is the visitor that walks the whole crate. @@ -962,7 +957,6 @@ pub struct Resolver<'a, 'tcx> { local_crate_name: Symbol, metadata_loader: Box, - untracked: &'tcx Untracked, used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, @@ -1141,7 +1135,7 @@ impl DefIdTree for ResolverTree<'_> { impl<'a, 'b, 'tcx> DefIdTree for &'a Resolver<'b, 'tcx> { #[inline] fn opt_parent(self, id: DefId) -> Option { - ResolverTree(&self.untracked).opt_parent(id) + ResolverTree(&self.tcx.untracked()).opt_parent(id) } } @@ -1168,10 +1162,11 @@ impl<'tcx> Resolver<'_, 'tcx> { "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", node_id, data, - self.untracked.definitions.read().def_key(self.node_id_to_def_id[&node_id]), + self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id]), ); - let def_id = self.untracked.definitions.write().create_def(parent, data); + // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()` + let def_id = self.tcx.untracked().definitions.write().create_def(parent, data); // Create the definition. if expn_id != ExpnId::root() { @@ -1180,7 +1175,7 @@ impl<'tcx> Resolver<'_, 'tcx> { // A relative span's parent must be an absolute span. debug_assert_eq!(span.data_untracked().parent, None); - let _id = self.untracked.source_span.write().push(span); + let _id = self.tcx.untracked().source_span.write().push(span); debug_assert_eq!(_id, def_id); // Some things for which we allocate `LocalDefId`s don't correspond to @@ -1206,15 +1201,12 @@ impl<'tcx> Resolver<'_, 'tcx> { impl<'a, 'tcx> Resolver<'a, 'tcx> { pub fn new( - session: &'tcx Session, + tcx: TyCtxt<'tcx>, krate: &Crate, crate_name: Symbol, metadata_loader: Box, arenas: &'a ResolverArenas<'a>, - untracked: &'tcx Untracked, ) -> Resolver<'a, 'tcx> { - let tcx = TyCtxt { sess: session }; - let root_def_id = CRATE_DEF_ID.to_def_id(); let mut module_map = FxHashMap::default(); let graph_root = arenas.new_module( @@ -1222,7 +1214,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty), ExpnId::root(), krate.spans.inner_span, - session.contains_name(&krate.attrs, sym::no_implicit_prelude), + tcx.sess.contains_name(&krate.attrs, sym::no_implicit_prelude), &mut module_map, ); let empty_module = arenas.new_module( @@ -1245,7 +1237,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let mut invocation_parents = FxHashMap::default(); invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential)); - let mut extern_prelude: FxHashMap> = session + let mut extern_prelude: FxHashMap> = tcx + .sess .opts .externs .iter() @@ -1253,16 +1246,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { .map(|(name, _)| (Ident::from_str(name), Default::default())) .collect(); - if !session.contains_name(&krate.attrs, sym::no_core) { + if !tcx.sess.contains_name(&krate.attrs, sym::no_core) { extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default()); - if !session.contains_name(&krate.attrs, sym::no_std) { + if !tcx.sess.contains_name(&krate.attrs, sym::no_std) { extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default()); } } - let registered_tools = macros::registered_tools(session, &krate.attrs); + let registered_tools = macros::registered_tools(tcx.sess, &krate.attrs); - let features = session.features_untracked(); + let features = tcx.sess.features_untracked(); let mut resolver = Resolver { tcx, @@ -1322,16 +1315,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { metadata_loader, local_crate_name: crate_name, used_extern_options: Default::default(), - untracked, macro_names: FxHashSet::default(), builtin_macros: Default::default(), builtin_macro_kinds: Default::default(), registered_tools, macro_use_prelude: FxHashMap::default(), macro_map: FxHashMap::default(), - dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(session.edition())), - dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(session.edition())), - non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(session.edition())), + dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(tcx.sess.edition())), + dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(tcx.sess.edition())), + non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(tcx.sess.edition())), invocation_parent_scopes: Default::default(), output_macro_rules_scopes: Default::default(), macro_rules_scopes: Default::default(), @@ -1469,7 +1461,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { - StableHashingContext::new(self.tcx.sess, self.untracked) + StableHashingContext::new(self.tcx.sess, self.tcx.untracked()) } fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T { @@ -1477,14 +1469,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { &self.tcx.sess, &*self.metadata_loader, self.local_crate_name, - &mut *self.untracked.cstore.write().untracked_as_any().downcast_mut().unwrap(), - self.untracked.definitions.read(), + &mut *self.tcx.untracked().cstore.write().untracked_as_any().downcast_mut().unwrap(), + self.tcx.definitions_untracked(), &mut self.used_extern_options, )) } fn cstore(&self) -> MappedReadGuard<'_, CStore> { - ReadGuard::map(self.untracked.cstore.read(), |r| r.as_any().downcast_ref().unwrap()) + CStore::from_tcx(self.tcx) } fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc { @@ -1535,7 +1527,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { }); // Make sure we don't mutate the cstore from here on. - self.untracked.cstore.leak(); + self.tcx.untracked().cstore.leak(); } fn traits_in_scope( @@ -1925,14 +1917,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. #[inline] fn opt_span(&self, def_id: DefId) -> Option { - def_id.as_local().map(|def_id| self.untracked.source_span.read()[def_id]) + def_id.as_local().map(|def_id| self.tcx.source_span(def_id)) } /// Retrieves the name of the given `DefId`. #[inline] fn opt_name(&self, def_id: DefId) -> Option { let def_key = match def_id.as_local() { - Some(def_id) => self.untracked.definitions.read().def_key(def_id), + Some(def_id) => self.tcx.definitions_untracked().def_key(def_id), None => self.cstore().def_key(def_id), }; def_key.get_opt_name() -- cgit 1.4.1-3-g733a5 From 63c8d000904fcb09cae22fba1768b955e354ebb8 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 16 Feb 2023 11:36:44 +0000 Subject: Use tcx queries instead of passing the values to `configure_and_expand`. --- compiler/rustc_interface/src/passes.rs | 11 ++++++----- compiler/rustc_interface/src/queries.rs | 10 ++-------- 2 files changed, 8 insertions(+), 13 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 43882e10303..85f39e2aaac 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -12,7 +12,7 @@ use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; use rustc_errors::{ErrorGuaranteed, PResult}; use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand}; use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE}; -use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore}; +use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore}; use rustc_metadata::creader::CStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; @@ -171,14 +171,15 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> { /// syntax expansion, secondary `cfg` expansion, synthesis of a test /// harness if one is to be provided, injection of a dependency on the /// standard library and prelude, and name resolution. +#[instrument(level = "trace", skip(tcx, krate, resolver))] pub fn configure_and_expand( - sess: &Session, - lint_store: &LintStore, + tcx: TyCtxt<'_>, mut krate: ast::Crate, - crate_name: Symbol, resolver: &mut Resolver<'_, '_>, ) -> Result { - trace!("configure_and_expand"); + let sess = tcx.sess; + let lint_store = unerased_lint_store(tcx); + let crate_name = tcx.crate_name(LOCAL_CRATE); pre_expansion_lint(sess, lint_store, resolver.registered_tools(), &krate, crate_name); rustc_builtin_macros::register_builtin_macros(resolver); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 7689e3cec06..e2c05350e09 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -204,7 +204,7 @@ impl<'tcx> Queries<'tcx> { let qcx = passes::create_global_ctxt( self.compiler, - lint_store.clone(), + lint_store, self.dep_graph()?.steal(), untracked, &self.queries, @@ -227,13 +227,7 @@ impl<'tcx> Queries<'tcx> { self.codegen_backend().metadata_loader(), &arenas, ); - let krate = passes::configure_and_expand( - sess, - &lint_store, - krate, - crate_name, - &mut resolver, - )?; + let krate = passes::configure_and_expand(tcx, krate, &mut resolver)?; // Make sure we don't mutate the cstore from here on. tcx.untracked().cstore.leak(); -- cgit 1.4.1-3-g733a5 From 37e2f4f48743b3d89cd016e50c1d29c9ace1dd06 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 16 Feb 2023 14:03:31 +0000 Subject: Make `configure_and_expand` "infalllible" by just aborting the compilation if it fails instead of bubbling out an error --- compiler/rustc_interface/src/passes.rs | 23 +++++++++++----------- compiler/rustc_interface/src/queries.rs | 5 ++--- tests/rustdoc-ui/bounded-hr-lifetime.stderr | 4 +--- tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr | 4 +--- ...ature-gate-rustdoc_missing_doc_code_examples.rs | 4 +++- ...e-gate-rustdoc_missing_doc_code_examples.stderr | 14 ++++++++++--- tests/rustdoc-ui/impl-fn-nesting.stderr | 4 +--- .../intra-doc/unresolved-import-recovery.stderr | 4 +--- tests/rustdoc-ui/issue-61732.stderr | 4 +--- tests/rustdoc-ui/unknown-renamed-lints.stderr | 4 +--- 10 files changed, 33 insertions(+), 37 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 85f39e2aaac..5c1e676c51d 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -9,7 +9,7 @@ use rustc_borrowck as mir_borrowck; use rustc_codegen_ssa::traits::CodegenBackend; use rustc_data_structures::parallel; use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; -use rustc_errors::{ErrorGuaranteed, PResult}; +use rustc_errors::PResult; use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand}; use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE}; use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore}; @@ -176,7 +176,7 @@ pub fn configure_and_expand( tcx: TyCtxt<'_>, mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>, -) -> Result { +) -> ast::Crate { let sess = tcx.sess; let lint_store = unerased_lint_store(tcx); let crate_name = tcx.crate_name(LOCAL_CRATE); @@ -250,20 +250,19 @@ pub fn configure_and_expand( ecx.check_unused_macros(); }); - let recursion_limit_hit = ecx.reduced_recursion_limit.is_some(); + // If we hit a recursion limit, exit early to avoid later passes getting overwhelmed + // with a large AST + if ecx.reduced_recursion_limit.is_some() { + sess.abort_if_errors(); + unreachable!(); + } if cfg!(windows) { env::set_var("PATH", &old_path); } - if recursion_limit_hit { - // If we hit a recursion limit, exit early to avoid later passes getting overwhelmed - // with a large AST - Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()) - } else { - Ok(krate) - } - })?; + krate + }); sess.time("maybe_building_test_harness", || { rustc_builtin_macros::test_harness::inject(sess, resolver, &mut krate) @@ -366,7 +365,7 @@ pub fn configure_and_expand( ) }); - Ok(krate) + krate } // Returns all the paths that correspond to generated files. diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index e2c05350e09..b3c4e5a09d5 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -227,7 +227,7 @@ impl<'tcx> Queries<'tcx> { self.codegen_backend().metadata_loader(), &arenas, ); - let krate = passes::configure_and_expand(tcx, krate, &mut resolver)?; + let krate = passes::configure_and_expand(tcx, krate, &mut resolver); // Make sure we don't mutate the cstore from here on. tcx.untracked().cstore.leak(); @@ -245,8 +245,7 @@ impl<'tcx> Queries<'tcx> { ); feed.resolutions(tcx.arena.alloc(untracked_resolutions)); feed.features_query(tcx.sess.features_untracked()); - Ok(()) - })?; + }); Ok(qcx) }) } diff --git a/tests/rustdoc-ui/bounded-hr-lifetime.stderr b/tests/rustdoc-ui/bounded-hr-lifetime.stderr index d8fcd6cb4b1..580f70c9742 100644 --- a/tests/rustdoc-ui/bounded-hr-lifetime.stderr +++ b/tests/rustdoc-ui/bounded-hr-lifetime.stderr @@ -4,7 +4,5 @@ error: lifetime bounds cannot be used in this context LL | for<'a: 'b + 'c> &'a (): std::fmt::Debug, | ^^ ^^ -error: Compilation failed, aborting rustdoc - -error: aborting due to 2 previous errors +error: aborting due to previous error diff --git a/tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr b/tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr index ba42c7bbb05..0864159c8e2 100644 --- a/tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr +++ b/tests/rustdoc-ui/feature-gate-doc_cfg_hide.stderr @@ -7,8 +7,6 @@ LL | #![doc(cfg_hide(test))] = note: see issue #43781 for more information = help: add `#![feature(doc_cfg_hide)]` to the crate attributes to enable -error: Compilation failed, aborting rustdoc - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0658`. diff --git a/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.rs b/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.rs index daba6986864..c34ea0567a9 100644 --- a/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.rs +++ b/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.rs @@ -1,10 +1,12 @@ #![deny(unknown_lints)] //~^ NOTE defined here - #![allow(rustdoc::missing_doc_code_examples)] //~^ ERROR unknown lint //~| ERROR unknown lint +//~| ERROR unknown lint +//~| NOTE lint is unstable //~| NOTE lint is unstable //~| NOTE lint is unstable //~| NOTE see issue //~| NOTE see issue +//~| NOTE see issue diff --git a/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.stderr b/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.stderr index cbe9a3d14af..326dcfe3bde 100644 --- a/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.stderr +++ b/tests/rustdoc-ui/feature-gate-rustdoc_missing_doc_code_examples.stderr @@ -1,5 +1,5 @@ error: unknown lint: `rustdoc::missing_doc_code_examples` - --> $DIR/feature-gate-rustdoc_missing_doc_code_examples.rs:4:1 + --> $DIR/feature-gate-rustdoc_missing_doc_code_examples.rs:3:1 | LL | #![allow(rustdoc::missing_doc_code_examples)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -14,7 +14,7 @@ LL | #![deny(unknown_lints)] | ^^^^^^^^^^^^^ error: unknown lint: `rustdoc::missing_doc_code_examples` - --> $DIR/feature-gate-rustdoc_missing_doc_code_examples.rs:4:1 + --> $DIR/feature-gate-rustdoc_missing_doc_code_examples.rs:3:1 | LL | #![allow(rustdoc::missing_doc_code_examples)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -23,7 +23,15 @@ LL | #![allow(rustdoc::missing_doc_code_examples)] = note: see issue #101730 for more information = help: add `#![feature(rustdoc_missing_doc_code_examples)]` to the crate attributes to enable -error: Compilation failed, aborting rustdoc +error: unknown lint: `rustdoc::missing_doc_code_examples` + --> $DIR/feature-gate-rustdoc_missing_doc_code_examples.rs:3:1 + | +LL | #![allow(rustdoc::missing_doc_code_examples)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the `rustdoc::missing_doc_code_examples` lint is unstable + = note: see issue #101730 for more information + = help: add `#![feature(rustdoc_missing_doc_code_examples)]` to the crate attributes to enable error: aborting due to 3 previous errors diff --git a/tests/rustdoc-ui/impl-fn-nesting.stderr b/tests/rustdoc-ui/impl-fn-nesting.stderr index 608749af895..75e6b4ed217 100644 --- a/tests/rustdoc-ui/impl-fn-nesting.stderr +++ b/tests/rustdoc-ui/impl-fn-nesting.stderr @@ -58,9 +58,7 @@ error[E0412]: cannot find type `UnknownType` in this scope LL | pub fn doubly_nested(c: UnknownType) { | ^^^^^^^^^^^ not found in this scope -error: Compilation failed, aborting rustdoc - -error: aborting due to 11 previous errors +error: aborting due to 10 previous errors Some errors have detailed explanations: E0405, E0412. For more information about an error, try `rustc --explain E0405`. diff --git a/tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr b/tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr index b54f8200666..14f56061852 100644 --- a/tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr +++ b/tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr @@ -6,8 +6,6 @@ LL | use unresolved_crate::module::Name; | = help: consider adding `extern crate unresolved_crate` to use the `unresolved_crate` crate -error: Compilation failed, aborting rustdoc - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0433`. diff --git a/tests/rustdoc-ui/issue-61732.stderr b/tests/rustdoc-ui/issue-61732.stderr index 38fadaa4435..d16ec6a853a 100644 --- a/tests/rustdoc-ui/issue-61732.stderr +++ b/tests/rustdoc-ui/issue-61732.stderr @@ -6,8 +6,6 @@ LL | pub(in crate::r#mod) fn main() {} | = help: consider adding `extern crate r#mod` to use the `r#mod` crate -error: Compilation failed, aborting rustdoc - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0433`. diff --git a/tests/rustdoc-ui/unknown-renamed-lints.stderr b/tests/rustdoc-ui/unknown-renamed-lints.stderr index b105f47d751..bf529b9f8e2 100644 --- a/tests/rustdoc-ui/unknown-renamed-lints.stderr +++ b/tests/rustdoc-ui/unknown-renamed-lints.stderr @@ -58,7 +58,5 @@ error: unknown lint: `rustdoc::intra_doc_link_resolution_failure` LL | #![deny(rustdoc::intra_doc_link_resolution_failure)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: Compilation failed, aborting rustdoc - -error: aborting due to 9 previous errors +error: aborting due to 8 previous errors -- cgit 1.4.1-3-g733a5 From c3522d063712abb90ff839254a4b269ede4f1fdd Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 16 Feb 2023 14:07:42 +0000 Subject: Move the resolver into a query --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 1 + compiler/rustc_driver_impl/src/lib.rs | 2 +- compiler/rustc_interface/src/passes.rs | 32 ++++++++++++++++++++++++- compiler/rustc_interface/src/queries.rs | 31 ++++-------------------- compiler/rustc_middle/src/arena.rs | 2 ++ compiler/rustc_middle/src/query/mod.rs | 14 ++++++++++- compiler/rustc_session/src/cstore.rs | 2 +- tests/ui/panics/default-backtrace-ice.stderr | 1 + 8 files changed, 54 insertions(+), 31 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 7d3c14fec5f..019ec0758d6 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -33,6 +33,7 @@ use rustc_target::spec::{RelocModel, Target}; ///
dylib
///
The metadata can be found in the `.rustc` section of the shared library.
/// +#[derive(Debug)] pub struct DefaultMetadataLoader; fn load_metadata_with( diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 1a4fe07b476..d7e9e00f3b6 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -320,7 +320,7 @@ fn run_compiler( } // Make sure name resolution and macro expansion is run. - queries.global_ctxt()?; + queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering(())); if callbacks.after_expansion(compiler, queries) == Compilation::Stop { return early_exit(); diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 5c1e676c51d..c3a9e55224f 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -8,6 +8,7 @@ use rustc_ast::{self as ast, visit}; use rustc_borrowck as mir_borrowck; use rustc_codegen_ssa::traits::CodegenBackend; use rustc_data_structures::parallel; +use rustc_data_structures::steal::Steal; use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; use rustc_errors::PResult; use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand}; @@ -172,7 +173,7 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> { /// harness if one is to be provided, injection of a dependency on the /// standard library and prelude, and name resolution. #[instrument(level = "trace", skip(tcx, krate, resolver))] -pub fn configure_and_expand( +fn configure_and_expand( tcx: TyCtxt<'_>, mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>, @@ -564,6 +565,34 @@ fn write_out_deps( } } +fn resolver_for_lowering<'tcx>( + tcx: TyCtxt<'tcx>, + (): (), +) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc)> { + let arenas = Resolver::arenas(); + let krate = tcx.crate_for_resolver(()).steal(); + let mut resolver = Resolver::new( + tcx, + &krate, + tcx.crate_name(LOCAL_CRATE), + tcx.metadata_loader(()).steal(), + &arenas, + ); + let krate = configure_and_expand(tcx, krate, &mut resolver); + + // Make sure we don't mutate the cstore from here on. + tcx.untracked().cstore.leak(); + + let ty::ResolverOutputs { + global_ctxt: untracked_resolutions, + ast_lowering: untracked_resolver_for_lowering, + } = resolver.into_outputs(); + + let feed = tcx.feed_unit_query(); + feed.resolutions(tcx.arena.alloc(untracked_resolutions)); + tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))) +} + fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { let sess = tcx.sess; let _timer = sess.timer("prepare_outputs"); @@ -618,6 +647,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock = LazyLock::new(|| { providers.analysis = analysis; providers.hir_crate = rustc_ast_lowering::lower_to_hir; providers.output_filenames = output_filenames; + providers.resolver_for_lowering = resolver_for_lowering; proc_macro_decls::provide(providers); rustc_const_eval::provide(providers); rustc_middle::hir::provide(providers); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index b3c4e5a09d5..c957578b59e 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -16,9 +16,8 @@ use rustc_lint::LintStore; use rustc_metadata::creader::CStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; -use rustc_middle::ty::{self, GlobalCtxt, TyCtxt}; +use rustc_middle::ty::{GlobalCtxt, TyCtxt}; use rustc_query_impl::Queries as TcxQueries; -use rustc_resolve::Resolver; use rustc_session::config::{self, OutputFilenames, OutputType}; use rustc_session::cstore::Untracked; use rustc_session::{output::find_crate_name, Session}; @@ -216,34 +215,12 @@ impl<'tcx> Queries<'tcx> { qcx.enter(|tcx| { let feed = tcx.feed_local_crate(); feed.crate_name(crate_name); - let (krate, resolver_outputs) = { - let _timer = sess.timer("configure_and_expand"); - - let arenas = Resolver::arenas(); - let mut resolver = Resolver::new( - tcx, - &krate, - crate_name, - self.codegen_backend().metadata_loader(), - &arenas, - ); - let krate = passes::configure_and_expand(tcx, krate, &mut resolver); - - // Make sure we don't mutate the cstore from here on. - tcx.untracked().cstore.leak(); - (Lrc::new(krate), resolver.into_outputs()) - }; - - let ty::ResolverOutputs { - global_ctxt: untracked_resolutions, - ast_lowering: untracked_resolver_for_lowering, - } = resolver_outputs; let feed = tcx.feed_unit_query(); - feed.resolver_for_lowering( - tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))), + feed.crate_for_resolver(tcx.arena.alloc(Steal::new(krate))); + feed.metadata_loader( + tcx.arena.alloc(Steal::new(self.codegen_backend().metadata_loader())), ); - feed.resolutions(tcx.arena.alloc(untracked_resolutions)); feed.features_query(tcx.sess.features_untracked()); }); Ok(qcx) diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 38a559d892a..d4019b5bf17 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -35,6 +35,8 @@ macro_rules! arena_types { rustc_data_structures::sync::Lrc, )>, [] output_filenames: std::sync::Arc, + [] metadata_loader: rustc_data_structures::steal::Steal>, + [] crate_for_resolver: rustc_data_structures::steal::Steal, [] resolutions: rustc_middle::ty::ResolverGlobalCtxt, [decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, [decode] code_region: rustc_middle::mir::coverage::CodeRegion, diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index cf4d9b4b005..3b559c7f8ad 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -33,7 +33,7 @@ rustc_queries! { } query resolver_for_lowering(_: ()) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc)> { - feedable + eval_always no_hash desc { "getting the resolver for lowering" } } @@ -2077,6 +2077,18 @@ rustc_queries! { desc { "looking up enabled feature gates" } } + query metadata_loader((): ()) -> &'tcx Steal> { + feedable + no_hash + desc { "raw operations for metadata file access" } + } + + query crate_for_resolver((): ()) -> &'tcx Steal { + feedable + no_hash + desc { "the ast before macro expansion and name resolution" } + } + /// Attempt to resolve the given `DefId` to an `Instance`, for the /// given generics args (`SubstsRef`), returning one of: /// * `Ok(Some(instance))` on success diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index 796ceba7ad3..97aa930b5ec 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -200,7 +200,7 @@ pub enum ExternCrateSource { /// At the time of this writing, there is only one backend and one way to store /// metadata in library -- this trait just serves to decouple rustc_metadata from /// the archive reader, which depends on LLVM. -pub trait MetadataLoader { +pub trait MetadataLoader: std::fmt::Debug { fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result; fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result; } diff --git a/tests/ui/panics/default-backtrace-ice.stderr b/tests/ui/panics/default-backtrace-ice.stderr index 7bf08bee922..4bd4780e25f 100644 --- a/tests/ui/panics/default-backtrace-ice.stderr +++ b/tests/ui/panics/default-backtrace-ice.stderr @@ -13,4 +13,5 @@ error: the compiler unexpectedly panicked. this is a bug. query stack during panic: +#0 [resolver_for_lowering] getting the resolver for lowering end of query stack -- cgit 1.4.1-3-g733a5 From acbcfaaf7b1da91197f58d17687399931bd5653a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 16 Feb 2023 14:45:26 +0000 Subject: Stop passing in values that one can also get from the tcx lazily --- compiler/rustc_interface/src/passes.rs | 8 +------- compiler/rustc_metadata/src/creader.rs | 29 +++++++++++++++++------------ compiler/rustc_resolve/src/lib.rs | 20 +++++--------------- 3 files changed, 23 insertions(+), 34 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index c3a9e55224f..fd322cb35a3 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -571,13 +571,7 @@ fn resolver_for_lowering<'tcx>( ) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc)> { let arenas = Resolver::arenas(); let krate = tcx.crate_for_resolver(()).steal(); - let mut resolver = Resolver::new( - tcx, - &krate, - tcx.crate_name(LOCAL_CRATE), - tcx.metadata_loader(()).steal(), - &arenas, - ); + let mut resolver = Resolver::new(tcx, &krate, &arenas); let krate = configure_and_expand(tcx, krate, &mut resolver); // Make sure we don't mutate the cstore from here on. diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 586b8c93e1c..b05626311e8 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -15,8 +15,8 @@ use rustc_hir::definitions::Definitions; use rustc_index::vec::IndexVec; use rustc_middle::ty::TyCtxt; use rustc_session::config::{self, CrateType, ExternLocation}; +use rustc_session::cstore::ExternCrateSource; use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate}; -use rustc_session::cstore::{ExternCrateSource, MetadataLoaderDyn}; use rustc_session::lint; use rustc_session::output::validate_crate_name; use rustc_session::search_paths::PathKind; @@ -60,16 +60,22 @@ impl std::fmt::Debug for CStore { } } -pub struct CrateLoader<'a> { +pub struct CrateLoader<'a, 'tcx: 'a> { // Immutable configuration. - sess: &'a Session, - metadata_loader: &'a MetadataLoaderDyn, - local_crate_name: Symbol, + tcx: TyCtxt<'tcx>, // Mutable output. cstore: &'a mut CStore, used_extern_options: &'a mut FxHashSet, } +impl<'a, 'tcx> std::ops::Deref for CrateLoader<'a, 'tcx> { + type Target = TyCtxt<'tcx>; + + fn deref(&self) -> &Self::Target { + &self.tcx + } +} + pub enum LoadedMacro { MacroDef(ast::Item, Edition), ProcMacro(SyntaxExtension), @@ -254,15 +260,13 @@ impl CStore { } } -impl<'a> CrateLoader<'a> { +impl<'a, 'tcx> CrateLoader<'a, 'tcx> { pub fn new( - sess: &'a Session, - metadata_loader: &'a MetadataLoaderDyn, - local_crate_name: Symbol, + tcx: TyCtxt<'tcx>, cstore: &'a mut CStore, used_extern_options: &'a mut FxHashSet, ) -> Self { - CrateLoader { sess, metadata_loader, local_crate_name, cstore, used_extern_options } + CrateLoader { tcx, cstore, used_extern_options } } pub fn cstore(&self) -> &CStore { &self.cstore @@ -553,9 +557,10 @@ impl<'a> CrateLoader<'a> { (LoadResult::Previous(cnum), None) } else { info!("falling back to a load"); + let metadata_loader = self.tcx.metadata_loader(()).borrow(); let mut locator = CrateLocator::new( self.sess, - &*self.metadata_loader, + &**metadata_loader, name, hash, extra_filename, @@ -960,7 +965,7 @@ impl<'a> CrateLoader<'a> { &format!( "external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`", name, - self.local_crate_name, + self.tcx.crate_name(LOCAL_CRATE), name), ); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 27b45181c9d..4d2b547d605 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -44,7 +44,7 @@ use rustc_middle::span_bug; use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, TyCtxt}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; -use rustc_session::cstore::{CrateStore, MetadataLoaderDyn, Untracked}; +use rustc_session::cstore::{CrateStore, Untracked}; use rustc_session::lint::LintBuffer; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; use rustc_span::source_map::Spanned; @@ -955,8 +955,6 @@ pub struct Resolver<'a, 'tcx> { arenas: &'a ResolverArenas<'a>, dummy_binding: &'a NameBinding<'a>, - local_crate_name: Symbol, - metadata_loader: Box, used_extern_options: FxHashSet, macro_names: FxHashSet, builtin_macros: FxHashMap, @@ -1203,8 +1201,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { pub fn new( tcx: TyCtxt<'tcx>, krate: &Crate, - crate_name: Symbol, - metadata_loader: Box, arenas: &'a ResolverArenas<'a>, ) -> Resolver<'a, 'tcx> { let root_def_id = CRATE_DEF_ID.to_def_id(); @@ -1312,8 +1308,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { vis: ty::Visibility::Public, }), - metadata_loader, - local_crate_name: crate_name, used_extern_options: Default::default(), macro_names: FxHashSet::default(), builtin_macros: Default::default(), @@ -1464,14 +1458,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { StableHashingContext::new(self.tcx.sess, self.tcx.untracked()) } - fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T { - f(&mut CrateLoader::new( - &self.tcx.sess, - &*self.metadata_loader, - self.local_crate_name, - &mut *self.tcx.untracked().cstore.write().untracked_as_any().downcast_mut().unwrap(), - &mut self.used_extern_options, - )) + fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T { + let mut cstore = self.tcx.untracked().cstore.write(); + let cstore = cstore.untracked_as_any().downcast_mut().unwrap(); + f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options)) } fn cstore(&self) -> MappedReadGuard<'_, CStore> { -- cgit 1.4.1-3-g733a5 From 1ab14ea7c261388e863e15b2688f16e704d76c47 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 16 Feb 2023 14:51:51 +0000 Subject: Remove some unnecessary tcx-passing --- compiler/rustc_interface/src/passes.rs | 11 ++++------- compiler/rustc_resolve/src/lib.rs | 4 ++++ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index fd322cb35a3..aa59654099a 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -172,12 +172,9 @@ impl LintStoreExpand for LintStoreExpandImpl<'_> { /// syntax expansion, secondary `cfg` expansion, synthesis of a test /// harness if one is to be provided, injection of a dependency on the /// standard library and prelude, and name resolution. -#[instrument(level = "trace", skip(tcx, krate, resolver))] -fn configure_and_expand( - tcx: TyCtxt<'_>, - mut krate: ast::Crate, - resolver: &mut Resolver<'_, '_>, -) -> ast::Crate { +#[instrument(level = "trace", skip(krate, resolver))] +fn configure_and_expand(mut krate: ast::Crate, resolver: &mut Resolver<'_, '_>) -> ast::Crate { + let tcx = resolver.tcx(); let sess = tcx.sess; let lint_store = unerased_lint_store(tcx); let crate_name = tcx.crate_name(LOCAL_CRATE); @@ -572,7 +569,7 @@ fn resolver_for_lowering<'tcx>( let arenas = Resolver::arenas(); let krate = tcx.crate_for_resolver(()).steal(); let mut resolver = Resolver::new(tcx, &krate, &arenas); - let krate = configure_and_expand(tcx, krate, &mut resolver); + let krate = configure_and_expand(krate, &mut resolver); // Make sure we don't mutate the cstore from here on. tcx.untracked().cstore.leak(); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 4d2b547d605..bf0eadeda21 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1195,6 +1195,10 @@ impl<'tcx> Resolver<'_, 'tcx> { self.cstore().item_generics_num_lifetimes(def_id, self.tcx.sess) } } + + pub fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } } impl<'a, 'tcx> Resolver<'a, 'tcx> { -- cgit 1.4.1-3-g733a5