From 0e60df9ed1439cb9d7bcc1a09bf2fc87d03393b1 Mon Sep 17 00:00:00 2001 From: Oleksii Lozovskyi Date: Sat, 24 Sep 2022 20:02:44 +0900 Subject: Parse "-Z instrument-xray" codegen option Recognize all bells and whistles that LLVM's XRay pass is capable of. The always/never settings are a bit dumb without attributes but they're still there. The default instruction count is chosen by the compiler, not LLVM pass. We'll do it later. --- compiler/rustc_interface/src/tests.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index 52a4e0e7418..5daefadabba 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -5,6 +5,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig}; use rustc_session::config::rustc_optgroups; use rustc_session::config::Input; +use rustc_session::config::InstrumentXRay; use rustc_session::config::TraitSolver; use rustc_session::config::{build_configuration, build_session_options, to_crate_config}; use rustc_session::config::{ @@ -755,6 +756,7 @@ fn test_unstable_options_tracking_hash() { tracked!(inline_mir_threshold, Some(123)); tracked!(instrument_coverage, Some(InstrumentCoverage::All)); tracked!(instrument_mcount, true); + tracked!(instrument_xray, Some(InstrumentXRay::default())); tracked!(link_only, true); tracked!(llvm_plugins, vec![String::from("plugin_name")]); tracked!(location_detail, LocationDetail { file: true, line: false, column: false }); -- cgit 1.4.1-3-g733a5 From f7b3e39502783c82d4a8d9e02f59aa4268d15dbf Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 Feb 2023 16:11:40 +1100 Subject: Simplify `tls::enter_context`. --- compiler/rustc_interface/src/callbacks.rs | 2 +- compiler/rustc_interface/src/passes.rs | 2 +- compiler/rustc_middle/src/dep_graph/mod.rs | 2 +- compiler/rustc_middle/src/ty/context/tls.rs | 4 ++-- compiler/rustc_query_impl/src/plumbing.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/callbacks.rs b/compiler/rustc_interface/src/callbacks.rs index ee0552d77ce..bc6d7c20997 100644 --- a/compiler/rustc_interface/src/callbacks.rs +++ b/compiler/rustc_interface/src/callbacks.rs @@ -38,7 +38,7 @@ fn track_diagnostic(diagnostic: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnost // Diagnostics are tracked, we can ignore the dependency. let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() }; - return tls::enter_context(&icx, move |_| (*f)(diagnostic)); + return tls::enter_context(&icx, move || (*f)(diagnostic)); } // In any other case, invoke diagnostics anyway. diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 2a373ebc132..304c3257456 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -748,7 +748,7 @@ impl<'tcx> QueryContext<'tcx> { F: FnOnce(TyCtxt<'tcx>) -> R, { let icx = ty::tls::ImplicitCtxt::new(self.gcx); - ty::tls::enter_context(&icx, |_| f(icx.tcx)) + ty::tls::enter_context(&icx, || f(icx.tcx)) } } diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index 2e62bebc852..2e82efba192 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -55,7 +55,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind { ty::tls::with_context(|icx| { let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() }; - ty::tls::enter_context(&icx, |_| op()) + ty::tls::enter_context(&icx, op) }) } diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs index 71b025dc1be..4d1ddf0c7f1 100644 --- a/compiler/rustc_middle/src/ty/context/tls.rs +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -110,9 +110,9 @@ unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> { #[inline] pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R where - F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, + F: FnOnce() -> R, { - tlv::with_tlv(erase(context), || f(&context)) + tlv::with_tlv(erase(context), f) } /// Allows access to the current `ImplicitCtxt` in a closure if one is available. diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 4dea03c1ef6..49309db564e 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -124,7 +124,7 @@ impl QueryContext for QueryCtxt<'_> { }; // Use the `ImplicitCtxt` while we execute the query. - tls::enter_context(&new_icx, |_| { + tls::enter_context(&new_icx, || { rustc_data_structures::stack::ensure_sufficient_stack(compute) }) }) -- cgit 1.4.1-3-g733a5 From 243944c6535867f2d4e3bc44f4a8b0e300dc83b9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 7 Feb 2023 16:59:50 +1100 Subject: Remove `QueryContext`. There is a type `QueryCtxt`, which impls the trait `QueryContext`. Confusingly, there is another type `QueryContext`. The latter is (like `TyCtxt`) just a pointer to a `GlobalContext`. It's not used much, e.g. its `impl` block has a single method. This commit removes `QueryContext`, replacing its use with direct `GlobalCtxt` use. --- compiler/rustc_interface/src/passes.rs | 26 +++++--------------------- compiler/rustc_interface/src/queries.rs | 27 ++++++++++++++------------- compiler/rustc_middle/src/ty/context.rs | 12 ++++++++++++ src/librustdoc/lib.rs | 4 ++-- 4 files changed, 33 insertions(+), 36 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 304c3257456..33ebbb411ce 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -738,30 +738,16 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock = LazyLock: extern_providers }); -pub struct QueryContext<'tcx> { - gcx: &'tcx GlobalCtxt<'tcx>, -} - -impl<'tcx> QueryContext<'tcx> { - pub fn enter(&mut self, f: F) -> R - where - F: FnOnce(TyCtxt<'tcx>) -> R, - { - let icx = ty::tls::ImplicitCtxt::new(self.gcx); - ty::tls::enter_context(&icx, || f(icx.tcx)) - } -} - pub fn create_global_ctxt<'tcx>( compiler: &'tcx Compiler, lint_store: Lrc, dep_graph: DepGraph, untracked: Untracked, queries: &'tcx OnceCell>, - global_ctxt: &'tcx OnceCell>, + gcx_cell: &'tcx OnceCell>, arena: &'tcx WorkerLocal>, hir_arena: &'tcx WorkerLocal>, -) -> QueryContext<'tcx> { +) -> &'tcx GlobalCtxt<'tcx> { // We're constructing the HIR here; we don't care what we will // read, since we haven't even constructed the *input* to // incr. comp. yet. @@ -785,8 +771,8 @@ pub fn create_global_ctxt<'tcx>( TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache) }); - let gcx = sess.time("setup_global_ctxt", || { - global_ctxt.get_or_init(move || { + sess.time("setup_global_ctxt", || { + gcx_cell.get_or_init(move || { TyCtxt::create_global_ctxt( sess, lint_store, @@ -799,9 +785,7 @@ pub fn create_global_ctxt<'tcx>( rustc_query_impl::query_callbacks(arena), ) }) - }); - - QueryContext { gcx } + }) } /// Runs the resolution, type-checking, region checking and other diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 4b0180741c1..6512695873e 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -1,6 +1,6 @@ use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation}; use crate::interface::{Compiler, Result}; -use crate::passes::{self, BoxedResolver, QueryContext}; +use crate::passes::{self, BoxedResolver}; use rustc_ast as ast; use rustc_codegen_ssa::traits::CodegenBackend; @@ -64,7 +64,7 @@ impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> { } } -impl<'a, 'tcx> QueryResult<'a, QueryContext<'tcx>> { +impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> { pub fn enter(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T { (*self.0).get_mut().enter(f) } @@ -78,7 +78,7 @@ impl Default for Query { pub struct Queries<'tcx> { compiler: &'tcx Compiler, - gcx: OnceCell>, + gcx_cell: OnceCell>, queries: OnceCell>, arena: WorkerLocal>, @@ -90,7 +90,8 @@ pub struct Queries<'tcx> { register_plugins: Query<(ast::Crate, Lrc)>, expansion: Query<(Lrc, Rc>, Lrc)>, dep_graph: Query, - global_ctxt: Query>, + // This just points to what's in `gcx_cell`. + gcx: Query<&'tcx GlobalCtxt<'tcx>>, ongoing_codegen: Query>, } @@ -98,7 +99,7 @@ impl<'tcx> Queries<'tcx> { pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> { Queries { compiler, - gcx: OnceCell::new(), + gcx_cell: OnceCell::new(), queries: OnceCell::new(), arena: WorkerLocal::new(|_| Arena::default()), hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()), @@ -108,7 +109,7 @@ impl<'tcx> Queries<'tcx> { register_plugins: Default::default(), expansion: Default::default(), dep_graph: Default::default(), - global_ctxt: Default::default(), + gcx: Default::default(), ongoing_codegen: Default::default(), } } @@ -207,8 +208,8 @@ impl<'tcx> Queries<'tcx> { }) } - pub fn global_ctxt(&'tcx self) -> Result>> { - self.global_ctxt.compute(|| { + pub fn global_ctxt(&'tcx self) -> Result>> { + self.gcx.compute(|| { let crate_name = *self.crate_name()?.borrow(); let (krate, resolver, lint_store) = self.expansion()?.steal(); @@ -218,18 +219,18 @@ impl<'tcx> Queries<'tcx> { ast_lowering: untracked_resolver_for_lowering, } = BoxedResolver::to_resolver_outputs(resolver); - let mut qcx = passes::create_global_ctxt( + let gcx = passes::create_global_ctxt( self.compiler, lint_store, self.dep_graph()?.steal(), untracked, &self.queries, - &self.gcx, + &self.gcx_cell, &self.arena, &self.hir_arena, ); - qcx.enter(|tcx| { + gcx.enter(|tcx| { let feed = tcx.feed_unit_query(); feed.resolver_for_lowering( tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))), @@ -239,7 +240,7 @@ impl<'tcx> Queries<'tcx> { let feed = tcx.feed_local_crate(); feed.crate_name(crate_name); }); - Ok(qcx) + Ok(gcx) }) } @@ -387,7 +388,7 @@ impl Compiler { // NOTE: intentionally does not compute the global context if it hasn't been built yet, // since that likely means there was a parse error. - if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() { + if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() { let gcx = gcx.get_mut(); // We assume that no queries are run past here. If there are new queries // after this point, they'll show up as "" in self-profiling data. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 9205a8a0ffe..d07d9190e01 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -468,6 +468,18 @@ pub struct GlobalCtxt<'tcx> { pub(crate) alloc_map: Lock>, } +impl<'tcx> GlobalCtxt<'tcx> { + /// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of + /// `f`. + pub fn enter<'a: 'tcx, F, R>(&'a self, f: F) -> R + where + F: FnOnce(TyCtxt<'tcx>) -> R, + { + let icx = tls::ImplicitCtxt::new(self); + tls::enter_context(&icx, || f(icx.tcx)) + } +} + impl<'tcx> TyCtxt<'tcx> { /// Expects a body and returns its codegen attributes. /// diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 64108c88285..90d6388b70c 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -814,9 +814,9 @@ fn main_args(at_args: &[String]) -> MainResult { sess.fatal("Compilation failed, aborting rustdoc"); } - let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess); + let mut gcx = abort_on_err(queries.global_ctxt(), sess); - global_ctxt.enter(|tcx| { + gcx.enter(|tcx| { let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || { core::run_global_ctxt( tcx, -- cgit 1.4.1-3-g733a5 From fd73d01c98adb2adb570ab300ee0d5c4dc0eafca Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 7 Feb 2023 17:34:40 +0400 Subject: rustc_resolve: Remove `Resolver::clone_output` And remove `Clone` impls and `Lrc`s that are no longer necessary --- compiler/rustc_hir/src/definitions.rs | 2 +- compiler/rustc_hir/src/hir.rs | 2 +- compiler/rustc_interface/src/passes.rs | 23 ++++------------ compiler/rustc_interface/src/queries.rs | 10 +++---- compiler/rustc_metadata/src/creader.rs | 11 ++++---- compiler/rustc_resolve/src/lib.rs | 49 +-------------------------------- 6 files changed, 18 insertions(+), 79 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 21cf214e47c..cd3c620cbb7 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -92,7 +92,7 @@ impl DefPathTable { /// The definition table containing node definitions. /// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s. /// It also stores mappings to convert `LocalDefId`s to/from `HirId`s. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct Definitions { table: DefPathTable, next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index a063307af0c..7cb3b6e1525 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3460,7 +3460,7 @@ pub struct Upvar { // The TraitCandidate's import_ids is empty if the trait is defined in the same module, and // has length > 0 if the trait is found through an chain of imports, starting with the // import/use statement in the scope where the trait is used. -#[derive(Encodable, Decodable, Clone, Debug, HashStable_Generic)] +#[derive(Encodable, Decodable, Debug, HashStable_Generic)] pub struct TraitCandidate { pub def_id: DefId, pub import_ids: SmallVec<[LocalDefId; 1]>, diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 33ebbb411ce..c8d8afae39e 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -35,13 +35,11 @@ use rustc_target::spec::PanicStrategy; use rustc_trait_selection::traits; use std::any::Any; -use std::cell::RefCell; use std::ffi::OsString; use std::io::{self, BufWriter, Write}; use std::marker::PhantomPinned; use std::path::{Path, PathBuf}; use std::pin::Pin; -use std::rc::Rc; use std::sync::{Arc, LazyLock}; use std::{env, fs, iter}; @@ -131,21 +129,12 @@ mod boxed_resolver { f((&mut *resolver).as_mut().unwrap()) } - pub fn to_resolver_outputs(resolver: Rc>) -> ty::ResolverOutputs { - match Rc::try_unwrap(resolver) { - Ok(resolver) => { - let mut resolver = resolver.into_inner(); - // SAFETY: The resolver doesn't need to be pinned. - let mut resolver = unsafe { - resolver - .0 - .as_mut() - .map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver) - }; - resolver.take().unwrap().into_outputs() - } - Err(resolver) => resolver.borrow_mut().access(|resolver| resolver.clone_outputs()), - } + pub fn into_outputs(mut self) -> ty::ResolverOutputs { + // SAFETY: The resolver doesn't need to be pinned. + let mut resolver = unsafe { + self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver) + }; + resolver.take().unwrap().into_outputs() } } } diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 6512695873e..67886b6b989 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -21,7 +21,6 @@ use rustc_span::symbol::sym; use rustc_span::Symbol; use std::any::Any; use std::cell::{RefCell, RefMut}; -use std::rc::Rc; use std::sync::Arc; /// Represent the result of a query. @@ -88,7 +87,7 @@ pub struct Queries<'tcx> { parse: Query, crate_name: Query, register_plugins: Query<(ast::Crate, Lrc)>, - expansion: Query<(Lrc, Rc>, Lrc)>, + expansion: Query<(Lrc, BoxedResolver, Lrc)>, dep_graph: Query, // This just points to what's in `gcx_cell`. gcx: Query<&'tcx GlobalCtxt<'tcx>>, @@ -171,8 +170,7 @@ impl<'tcx> Queries<'tcx> { pub fn expansion( &self, - ) -> Result, Rc>, Lrc)>> - { + ) -> Result, BoxedResolver, Lrc)>> { trace!("expansion"); self.expansion.compute(|| { let crate_name = *self.crate_name()?.borrow(); @@ -188,7 +186,7 @@ impl<'tcx> Queries<'tcx> { let krate = resolver.access(|resolver| { passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver) })?; - Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store)) + Ok((Lrc::new(krate), resolver, lint_store)) }) } @@ -217,7 +215,7 @@ impl<'tcx> Queries<'tcx> { untracked, global_ctxt: untracked_resolutions, ast_lowering: untracked_resolver_for_lowering, - } = BoxedResolver::to_resolver_outputs(resolver); + } = resolver.into_outputs(); let gcx = passes::create_global_ctxt( self.compiler, diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index bf8b8aa2ce4..c357f294279 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -8,7 +8,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, ReadGuard}; +use rustc_data_structures::sync::ReadGuard; use rustc_expand::base::SyntaxExtension; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; @@ -30,11 +30,10 @@ use proc_macro::bridge::client::ProcMacro; use std::ops::Fn; use std::path::Path; use std::time::Duration; -use std::{cmp, env}; +use std::{cmp, env, iter}; -#[derive(Clone)] pub struct CStore { - metas: IndexVec>>, + metas: IndexVec>>, injected_panic_runtime: Option, /// This crate needs an allocator and either provides it itself, or finds it in a dependency. /// If the above is true, then this field denotes the kind of the found allocator. @@ -153,7 +152,7 @@ impl CStore { fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) { assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry"); - self.metas[cnum] = Some(Lrc::new(data)); + self.metas[cnum] = Some(Box::new(data)); } pub(crate) fn iter_crate_data(&self) -> impl Iterator { @@ -245,7 +244,7 @@ impl CStore { // 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), + metas: IndexVec::from_iter(iter::once(None)), injected_panic_runtime: None, allocator_kind: None, alloc_error_handler_kind: None, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index f7987e84371..3c70e9c93e3 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -378,7 +378,7 @@ impl ModuleOrUniformRoot<'_> { } } -#[derive(Clone, Debug)] +#[derive(Debug)] enum PathResult<'a> { Module(ModuleOrUniformRoot<'a>), NonModule(PartialRes), @@ -1472,53 +1472,6 @@ impl<'a> Resolver<'a> { 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.untracked.definitions.clone(); - let cstore = Box::new(self.cstore().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(), - has_pub_restricted: self.has_pub_restricted, - extern_crate_map: self.extern_crate_map.clone(), - reexport_map: self.reexport_map.clone(), - glob_map: self.glob_map.clone(), - maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(), - maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(), - extern_prelude: self - .extern_prelude - .iter() - .map(|(ident, entry)| (ident.name, entry.introduced_by_item)) - .collect(), - main_def: self.main_def, - trait_impls: self.trait_impls.clone(), - proc_macros, - confused_type_with_std_module: self.confused_type_with_std_module.clone(), - registered_tools: self.registered_tools.clone(), - effective_visibilities: self.effective_visibilities.clone(), - doc_link_resolutions: self.doc_link_resolutions.clone(), - doc_link_traits_in_scope: self.doc_link_traits_in_scope.clone(), - all_macro_rules: self.all_macro_rules.clone(), - }; - let ast_lowering = ty::ResolverAstLowering { - legacy_const_generic_args: self.legacy_const_generic_args.clone(), - partial_res_map: self.partial_res_map.clone(), - import_res_map: self.import_res_map.clone(), - label_res_map: self.label_res_map.clone(), - lifetimes_res_map: self.lifetimes_res_map.clone(), - extra_lifetime_params_map: self.extra_lifetime_params_map.clone(), - next_node_id: self.next_node_id, - node_id_to_def_id: self.node_id_to_def_id.clone(), - def_id_to_node_id: self.def_id_to_node_id.clone(), - trait_map: self.trait_map.clone(), - builtin_macro_kinds: self.builtin_macro_kinds.clone(), - lifetime_elision_allowed: self.lifetime_elision_allowed.clone(), - }; - ResolverOutputs { global_ctxt, ast_lowering, untracked } - } - fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { StableHashingContext::new(self.session, &self.untracked) } -- cgit 1.4.1-3-g733a5 From 43a5cc383d9cf96825561b646e5f4686ae45c6c1 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 8 Dec 2022 12:59:02 +0000 Subject: Separate the lifetime of the session and the arena in the resolver --- compiler/rustc_interface/src/passes.rs | 14 +++++++--- compiler/rustc_resolve/src/build_reduced_graph.rs | 14 +++++----- compiler/rustc_resolve/src/check_unused.rs | 10 ++++---- compiler/rustc_resolve/src/def_collector.rs | 10 ++++---- compiler/rustc_resolve/src/diagnostics.rs | 4 +-- .../rustc_resolve/src/effective_visibilities.rs | 19 ++++++++------ compiler/rustc_resolve/src/ident.rs | 2 +- compiler/rustc_resolve/src/imports.rs | 10 ++++---- compiler/rustc_resolve/src/late.rs | 28 ++++++++++---------- compiler/rustc_resolve/src/late/diagnostics.rs | 4 +-- compiler/rustc_resolve/src/lib.rs | 30 +++++++++++----------- compiler/rustc_resolve/src/macros.rs | 4 +-- 12 files changed, 79 insertions(+), 70 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index c8d8afae39e..e85cc7ad36d 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -82,7 +82,7 @@ mod boxed_resolver { struct BoxedResolverInner { session: Lrc, resolver_arenas: Option>, - resolver: Option>, + resolver: Option>, _pin: PhantomPinned, } @@ -98,7 +98,10 @@ mod boxed_resolver { impl BoxedResolver { pub(super) fn new( session: Lrc, - make_resolver: impl for<'a> FnOnce(&'a Session, &'a ResolverArenas<'a>) -> Resolver<'a>, + make_resolver: impl for<'a, 'tcx> FnOnce( + &'tcx Session, + &'a ResolverArenas<'a>, + ) -> Resolver<'a, 'tcx>, ) -> BoxedResolver { let mut boxed_resolver = Box::new(BoxedResolverInner { session, @@ -121,7 +124,10 @@ mod boxed_resolver { } } - pub fn access FnOnce(&mut Resolver<'a>) -> R, R>(&mut self, f: F) -> R { + pub fn access FnOnce(&mut Resolver<'a, 'tcx>) -> R, R>( + &mut self, + f: F, + ) -> R { // SAFETY: The resolver doesn't need to be pinned. let mut resolver = unsafe { self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver) @@ -256,7 +262,7 @@ pub fn configure_and_expand( lint_store: &LintStore, mut krate: ast::Crate, crate_name: Symbol, - resolver: &mut Resolver<'_>, + resolver: &mut Resolver<'_, '_>, ) -> Result { trace!("configure_and_expand"); pre_expansion_lint(sess, lint_store, resolver.registered_tools(), &krate, crate_name); diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 44f3bf1be05..6d3518d53f7 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -65,7 +65,7 @@ impl<'a, Id: Into> ToNameBinding<'a> for (Res, ty::Visibility, Span, } } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined; /// otherwise, reports an error. pub(crate) fn define(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T) @@ -214,18 +214,18 @@ impl<'a> Resolver<'a> { } } -struct BuildReducedGraphVisitor<'a, 'b> { - r: &'b mut Resolver<'a>, +struct BuildReducedGraphVisitor<'a, 'b, 'tcx> { + r: &'b mut Resolver<'a, 'tcx>, parent_scope: ParentScope<'a>, } -impl<'a> AsMut> for BuildReducedGraphVisitor<'a, '_> { - fn as_mut(&mut self) -> &mut Resolver<'a> { +impl<'a, 'tcx> AsMut> for BuildReducedGraphVisitor<'a, '_, 'tcx> { + fn as_mut(&mut self) -> &mut Resolver<'a, 'tcx> { self.r } } -impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { +impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility { self.try_resolve_visibility(vis, true).unwrap_or_else(|err| { self.r.report_vis_error(err); @@ -1315,7 +1315,7 @@ macro_rules! method { }; } -impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { +impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> { method!(visit_expr: ast::Expr, ast::ExprKind::MacCall, walk_expr); method!(visit_pat: ast::Pat, ast::PatKind::MacCall, walk_pat); method!(visit_ty: ast::Ty, ast::TyKind::MacCall, walk_ty); diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index eae4c9992eb..294fd0a736f 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -49,8 +49,8 @@ impl<'a> UnusedImport<'a> { } } -struct UnusedImportCheckVisitor<'a, 'b> { - r: &'a mut Resolver<'b>, +struct UnusedImportCheckVisitor<'a, 'b, 'tcx> { + r: &'a mut Resolver<'b, 'tcx>, /// All the (so far) unused imports, grouped path list unused_imports: FxIndexMap>, base_use_tree: Option<&'a ast::UseTree>, @@ -58,7 +58,7 @@ struct UnusedImportCheckVisitor<'a, 'b> { item_span: Span, } -impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { +impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> { // We have information about whether `use` (import) items are actually // used now. If an import is not used at all, we signal a lint error. fn check_import(&mut self, id: ast::NodeId) { @@ -94,7 +94,7 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { } } -impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> { +impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> { fn visit_item(&mut self, item: &'a ast::Item) { self.item_span = item.span_with_attributes(); @@ -222,7 +222,7 @@ fn calc_unused_spans( } } -impl Resolver<'_> { +impl Resolver<'_, '_> { pub(crate) fn check_unused(&mut self, krate: &ast::Crate) { for import in self.potentially_unused_imports.iter() { match import.kind { diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 2764a6c28a5..e7ff236f846 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -9,7 +9,7 @@ use rustc_span::symbol::sym; use rustc_span::Span; pub(crate) fn collect_definitions( - resolver: &mut Resolver<'_>, + resolver: &mut Resolver<'_, '_>, fragment: &AstFragment, expansion: LocalExpnId, ) { @@ -18,14 +18,14 @@ pub(crate) fn collect_definitions( } /// Creates `DefId`s for nodes in the AST. -struct DefCollector<'a, 'b> { - resolver: &'a mut Resolver<'b>, +struct DefCollector<'a, 'b, 'tcx> { + resolver: &'a mut Resolver<'b, 'tcx>, parent_def: LocalDefId, impl_trait_context: ImplTraitContext, expansion: LocalExpnId, } -impl<'a, 'b> DefCollector<'a, 'b> { +impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId { let parent_def = self.parent_def; debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def); @@ -81,7 +81,7 @@ impl<'a, 'b> DefCollector<'a, 'b> { } } -impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { +impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { fn visit_item(&mut self, i: &'a Item) { debug!("visit_item: {:?}", i); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index a08ae0f184b..934d60589d4 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -114,7 +114,7 @@ fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span { sm.span_until_whitespace(impl_span) } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { pub(crate) fn report_errors(&mut self, krate: &Crate) { self.report_with_use_injections(krate); @@ -1883,7 +1883,7 @@ impl<'a> Resolver<'a> { } } -impl<'a, 'b> ImportResolver<'a, 'b> { +impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> { /// Adds suggestions for a path that cannot be resolved. pub(crate) fn make_path_suggestion( &mut self, diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs index ab68f25a886..0079c3e526d 100644 --- a/compiler/rustc_resolve/src/effective_visibilities.rs +++ b/compiler/rustc_resolve/src/effective_visibilities.rs @@ -29,8 +29,8 @@ impl ParentId<'_> { } } -pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a> { - r: &'r mut Resolver<'a>, +pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> { + r: &'r mut Resolver<'a, 'tcx>, def_effective_visibilities: EffectiveVisibilities, /// While walking import chains we need to track effective visibilities per-binding, and def id /// keys in `Resolver::effective_visibilities` are not enough for that, because multiple @@ -41,7 +41,7 @@ pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a> { changed: bool, } -impl Resolver<'_> { +impl Resolver<'_, '_> { fn nearest_normal_mod(&mut self, def_id: LocalDefId) -> LocalDefId { self.get_nearest_non_block_module(def_id.to_def_id()).nearest_parent_mod().expect_local() } @@ -67,18 +67,21 @@ impl Resolver<'_> { } } -impl<'a, 'b> IntoDefIdTree for &'b mut Resolver<'a> { - type Tree = &'b Resolver<'a>; +impl<'a, 'b, 'tcx> IntoDefIdTree for &'b mut Resolver<'a, 'tcx> { + type Tree = &'b Resolver<'a, 'tcx>; fn tree(self) -> Self::Tree { self } } -impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { +impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> { /// Fills the `Resolver::effective_visibilities` table with public & exported items /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we /// need access to a TyCtxt for that. - pub(crate) fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) { + pub(crate) fn compute_effective_visibilities<'c>( + r: &'r mut Resolver<'a, 'tcx>, + krate: &'c Crate, + ) { let mut visitor = EffectiveVisibilitiesVisitor { r, def_effective_visibilities: Default::default(), @@ -192,7 +195,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> { } } -impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> { +impl<'r, 'ast, 'tcx> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r, 'tcx> { fn visit_item(&mut self, item: &'ast ast::Item) { let def_id = self.r.local_def_id(item.id); // Update effective visibilities of nested items. diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index d03ccf256fa..61a48b109b2 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -28,7 +28,7 @@ use RibKind::*; type Visibility = ty::Visibility; -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { /// A generic scope visitor. /// Visits scopes in order to resolve some identifier in them or perform other actions. /// If the callback returns `Some` result, we stop visiting scopes and return it. diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 8d1c789dea7..da3e5095e53 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -225,7 +225,7 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi } } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { /// Given a binding and an import that resolves to it, /// return the corresponding binding defined by the import. pub(crate) fn import( @@ -333,7 +333,7 @@ impl<'a> Resolver<'a> { // If the resolution becomes a success, define it in the module's glob importers. fn update_resolution(&mut self, module: Module<'a>, key: BindingKey, f: F) -> T where - F: FnOnce(&mut Resolver<'a>, &mut NameResolution<'a>) -> T, + F: FnOnce(&mut Resolver<'a, 'tcx>, &mut NameResolution<'a>) -> T, { // Ensure that `resolution` isn't borrowed when defining in the module's glob importers, // during which the resolution might end up getting re-defined via a glob cycle. @@ -405,11 +405,11 @@ struct UnresolvedImportError { candidates: Option>, } -pub(crate) struct ImportResolver<'a, 'b> { - pub r: &'a mut Resolver<'b>, +pub(crate) struct ImportResolver<'a, 'b, 'tcx> { + pub r: &'a mut Resolver<'b, 'tcx>, } -impl<'a, 'b> ImportResolver<'a, 'b> { +impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> { // Import resolution // // This is a fixed-point algorithm. We resolve imports until our efforts diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index d4c056f12f6..d3bcbbabf55 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -505,7 +505,7 @@ enum MaybeExported<'a> { } impl MaybeExported<'_> { - fn eval(self, r: &Resolver<'_>) -> bool { + fn eval(self, r: &Resolver<'_, '_>) -> bool { let def_id = match self { MaybeExported::Ok(node_id) => Some(r.local_def_id(node_id)), MaybeExported::Impl(Some(trait_def_id)) | MaybeExported::ImplItem(Ok(trait_def_id)) => { @@ -584,8 +584,8 @@ struct DiagnosticMetadata<'ast> { current_elision_failures: Vec, } -struct LateResolutionVisitor<'a, 'b, 'ast> { - r: &'b mut Resolver<'a>, +struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { + r: &'b mut Resolver<'a, 'tcx>, /// The module that represents the current item scope. parent_scope: ParentScope<'a>, @@ -628,7 +628,7 @@ struct LateResolutionVisitor<'a, 'b, 'ast> { } /// Walks the whole crate in DFS order, visiting each item, resolving names as it goes. -impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { +impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast, 'tcx> { fn visit_attribute(&mut self, _: &'ast Attribute) { // We do not want to resolve expressions that appear in attributes, // as they do not correspond to actual code. @@ -1199,8 +1199,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { } } -impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { - fn new(resolver: &'b mut Resolver<'a>) -> LateResolutionVisitor<'a, 'b, 'ast> { +impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { + fn new(resolver: &'b mut Resolver<'a, 'tcx>) -> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { // During late resolution we only track the module component of the parent scope, // although it may be useful to track other components as well for diagnostics. let graph_root = resolver.graph_root; @@ -2029,13 +2029,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { /// List all the lifetimes that appear in the provided type. fn find_lifetime_for_self(&self, ty: &'ast Ty) -> Set1 { - struct SelfVisitor<'r, 'a> { - r: &'r Resolver<'a>, + struct SelfVisitor<'r, 'a, 'tcx> { + r: &'r Resolver<'a, 'tcx>, impl_self: Option, lifetime: Set1, } - impl SelfVisitor<'_, '_> { + impl SelfVisitor<'_, '_, '_> { // Look for `self: &'a Self` - also desugared from `&'a self`, // and if that matches, use it for elision and return early. fn is_self_ty(&self, ty: &Ty) -> bool { @@ -2053,7 +2053,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { } } - impl<'a> Visitor<'a> for SelfVisitor<'_, '_> { + impl<'a> Visitor<'a> for SelfVisitor<'_, '_, '_> { fn visit_ty(&mut self, ty: &'a Ty) { trace!("SelfVisitor considering ty={:?}", ty); if let TyKind::Ref(lt, ref mt) = ty.kind && self.is_self_ty(&mt.ty) { @@ -4288,13 +4288,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { } } -struct LifetimeCountVisitor<'a, 'b> { - r: &'b mut Resolver<'a>, +struct LifetimeCountVisitor<'a, 'b, 'tcx> { + r: &'b mut Resolver<'a, 'tcx>, } /// Walks the whole crate in DFS order, visiting each item, counting the declared number of /// lifetime generic parameters. -impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> { +impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_, '_> { fn visit_item(&mut self, item: &'ast Item) { match &item.kind { ItemKind::TyAlias(box TyAlias { ref generics, .. }) @@ -4328,7 +4328,7 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> { } } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) { visit::walk_crate(&mut LifetimeCountVisitor { r: self }, krate); let mut late_resolution_visitor = LateResolutionVisitor::new(self); diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index c6d27ec69c5..a3195a64366 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -166,7 +166,7 @@ impl TypoCandidate { } } -impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { +impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { fn def_span(&self, def_id: DefId) -> Option { match def_id.krate { LOCAL_CRATE => self.r.opt_span(def_id), @@ -318,7 +318,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { span: Span, source: PathSource<'_>, res: Option, - ) -> (DiagnosticBuilder<'a, ErrorGuaranteed>, Vec) { + ) -> (DiagnosticBuilder<'tcx, ErrorGuaranteed>, Vec) { debug!(?res, ?source); let base_error = self.make_base_error(path, span, source, res); let code = source.error_code(res.is_some()); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 3c70e9c93e3..3db3b76fc26 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -147,7 +147,7 @@ struct ParentScope<'a> { impl<'a> ParentScope<'a> { /// Creates a parent scope with the passed argument used as the module scope component, /// and other scope components set to default empty values. - fn module(module: Module<'a>, resolver: &Resolver<'a>) -> ParentScope<'a> { + fn module(module: Module<'a>, resolver: &Resolver<'a, '_>) -> ParentScope<'a> { ParentScope { module, expansion: LocalExpnId::ROOT, @@ -528,9 +528,9 @@ impl<'a> ModuleData<'a> { } } - fn for_each_child(&'a self, resolver: &mut R, mut f: F) + fn for_each_child<'tcx, R, F>(&'a self, resolver: &mut R, mut f: F) where - R: AsMut>, + R: AsMut>, F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>), { for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() { @@ -541,9 +541,9 @@ impl<'a> ModuleData<'a> { } /// This modifies `self` in place. The traits will be stored in `self.traits`. - fn ensure_traits(&'a self, resolver: &mut R) + fn ensure_traits<'tcx, R>(&'a self, resolver: &mut R) where - R: AsMut>, + R: AsMut>, { let mut traits = self.traits.borrow_mut(); if traits.is_none() { @@ -864,8 +864,8 @@ struct MacroData { /// The main resolver class. /// /// This is the visitor that walks the whole crate. -pub struct Resolver<'a> { - session: &'a Session, +pub struct Resolver<'a, 'tcx> { + session: &'tcx Session, /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. expn_that_defined: FxHashMap, @@ -949,7 +949,7 @@ pub struct Resolver<'a> { /// Ambiguity errors are delayed for deduplication. ambiguity_errors: Vec>, /// `use` injections are delayed for better placement and deduplication. - use_injections: Vec>, + use_injections: Vec>, /// Crate-local macro expanded `macro_export` referred to by a module-relative path. macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>, @@ -1111,8 +1111,8 @@ impl<'a> ResolverArenas<'a> { } } -impl<'a> AsMut> for Resolver<'a> { - fn as_mut(&mut self) -> &mut Resolver<'a> { +impl<'a, 'tcx> AsMut> for Resolver<'a, 'tcx> { + fn as_mut(&mut self) -> &mut Resolver<'a, 'tcx> { self } } @@ -1134,14 +1134,14 @@ impl DefIdTree for ResolverTree<'_> { } } -impl<'a, 'b> DefIdTree for &'a Resolver<'b> { +impl<'a, 'b, 'tcx> DefIdTree for &'a Resolver<'b, 'tcx> { #[inline] fn opt_parent(self, id: DefId) -> Option { ResolverTree(&self.untracked).opt_parent(id) } } -impl<'a> Resolver<'a> { +impl<'tcx> Resolver<'_, 'tcx> { fn opt_local_def_id(&self, node: NodeId) -> Option { self.node_id_to_def_id.get(&node).copied() } @@ -1200,14 +1200,14 @@ impl<'a> Resolver<'a> { } } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { pub fn new( - session: &'a Session, + session: &'tcx Session, krate: &Crate, crate_name: Symbol, metadata_loader: Box, arenas: &'a ResolverArenas<'a>, - ) -> Resolver<'a> { + ) -> Resolver<'a, 'tcx> { let root_def_id = CRATE_DEF_ID.to_def_id(); let mut module_map = FxHashMap::default(); let graph_root = arenas.new_module( diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 96ad6e96fac..1c220a81792 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -160,7 +160,7 @@ fn soft_custom_inner_attributes_gate(path: &ast::Path, invoc: &Invocation) -> bo false } -impl<'a> ResolverExpand for Resolver<'a> { +impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> { fn next_node_id(&mut self) -> NodeId { self.next_node_id() } @@ -467,7 +467,7 @@ impl<'a> ResolverExpand for Resolver<'a> { } } -impl<'a> Resolver<'a> { +impl<'a, 'tcx> Resolver<'a, 'tcx> { /// Resolve macro path with error reporting and recovery. /// Uses dummy syntax extensions for unresolved macros or macros with unexpected resolutions /// for better error recovery. -- cgit 1.4.1-3-g733a5 From 21f4c0723ed871472b050b6031b7c9f5e7e0970d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 13 Dec 2022 09:46:37 +0000 Subject: Remove BoxedResolver --- compiler/rustc_interface/src/interface.rs | 1 - compiler/rustc_interface/src/passes.rs | 90 +------------------------------ compiler/rustc_interface/src/queries.rs | 29 +++++----- 3 files changed, 18 insertions(+), 102 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 7a5e45ada3f..d504aea77d0 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -1,4 +1,3 @@ -pub use crate::passes::BoxedResolver; use crate::util; use rustc_ast::token; diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index e85cc7ad36d..6a94d19001e 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -23,9 +23,9 @@ use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_a use rustc_passes::{self, hir_stats, layout_test}; use rustc_plugin_impl as plugin; use rustc_query_impl::{OnDiskCache, Queries as TcxQueries}; -use rustc_resolve::{Resolver, ResolverArenas}; +use rustc_resolve::Resolver; use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType}; -use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn, Untracked}; +use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, Untracked}; use rustc_session::output::filename_for_input; use rustc_session::search_paths::PathKind; use rustc_session::{Limit, Session}; @@ -37,9 +37,7 @@ use rustc_trait_selection::traits; use std::any::Any; use std::ffi::OsString; use std::io::{self, BufWriter, Write}; -use std::marker::PhantomPinned; use std::path::{Path, PathBuf}; -use std::pin::Pin; use std::sync::{Arc, LazyLock}; use std::{env, fs, iter}; @@ -73,90 +71,6 @@ fn count_nodes(krate: &ast::Crate) -> usize { counter.count } -pub use boxed_resolver::BoxedResolver; -mod boxed_resolver { - use super::*; - - pub struct BoxedResolver(Pin>); - - struct BoxedResolverInner { - session: Lrc, - resolver_arenas: Option>, - resolver: Option>, - _pin: PhantomPinned, - } - - // Note: Drop order is important to prevent dangling references. Resolver must be dropped first, - // then resolver_arenas and session. - impl Drop for BoxedResolverInner { - fn drop(&mut self) { - self.resolver.take(); - self.resolver_arenas.take(); - } - } - - impl BoxedResolver { - pub(super) fn new( - session: Lrc, - make_resolver: impl for<'a, 'tcx> FnOnce( - &'tcx Session, - &'a ResolverArenas<'a>, - ) -> Resolver<'a, 'tcx>, - ) -> BoxedResolver { - let mut boxed_resolver = Box::new(BoxedResolverInner { - session, - resolver_arenas: Some(Resolver::arenas()), - resolver: None, - _pin: PhantomPinned, - }); - // SAFETY: `make_resolver` takes a resolver arena with an arbitrary lifetime and - // returns a resolver with the same lifetime as the arena. We ensure that the arena - // outlives the resolver in the drop impl and elsewhere so these transmutes are sound. - unsafe { - let resolver = make_resolver( - std::mem::transmute::<&Session, &Session>(&boxed_resolver.session), - std::mem::transmute::<&ResolverArenas<'_>, &ResolverArenas<'_>>( - boxed_resolver.resolver_arenas.as_ref().unwrap(), - ), - ); - boxed_resolver.resolver = Some(resolver); - BoxedResolver(Pin::new_unchecked(boxed_resolver)) - } - } - - pub fn access FnOnce(&mut Resolver<'a, 'tcx>) -> R, R>( - &mut self, - f: F, - ) -> R { - // SAFETY: The resolver doesn't need to be pinned. - let mut resolver = unsafe { - self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver) - }; - f((&mut *resolver).as_mut().unwrap()) - } - - pub fn into_outputs(mut self) -> ty::ResolverOutputs { - // SAFETY: The resolver doesn't need to be pinned. - let mut resolver = unsafe { - self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver) - }; - resolver.take().unwrap().into_outputs() - } - } -} - -pub fn create_resolver( - sess: Lrc, - metadata_loader: Box, - krate: &ast::Crate, - crate_name: Symbol, -) -> BoxedResolver { - trace!("create_resolver"); - BoxedResolver::new(sess, move |sess, resolver_arenas| { - Resolver::new(sess, krate, crate_name, metadata_loader, resolver_arenas) - }) -} - pub fn register_plugins<'a>( sess: &'a Session, metadata_loader: &'a dyn MetadataLoader, diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 67886b6b989..752c1ec7538 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -1,6 +1,6 @@ use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation}; use crate::interface::{Compiler, Result}; -use crate::passes::{self, BoxedResolver}; +use crate::passes; use rustc_ast as ast; use rustc_codegen_ssa::traits::CodegenBackend; @@ -13,8 +13,9 @@ use rustc_incremental::DepGraphFuture; use rustc_lint::LintStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; -use rustc_middle::ty::{self, GlobalCtxt, TyCtxt}; +use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt}; use rustc_query_impl::Queries as TcxQueries; +use rustc_resolve::Resolver; use rustc_session::config::{self, OutputFilenames, OutputType}; use rustc_session::{output::find_crate_name, Session}; use rustc_span::symbol::sym; @@ -87,7 +88,7 @@ pub struct Queries<'tcx> { parse: Query, crate_name: Query, register_plugins: Query<(ast::Crate, Lrc)>, - expansion: Query<(Lrc, BoxedResolver, Lrc)>, + expansion: Query<(Lrc, ResolverOutputs, Lrc)>, dep_graph: Query, // This just points to what's in `gcx_cell`. gcx: Query<&'tcx GlobalCtxt<'tcx>>, @@ -170,23 +171,25 @@ impl<'tcx> Queries<'tcx> { pub fn expansion( &self, - ) -> Result, BoxedResolver, Lrc)>> { + ) -> Result, ResolverOutputs, Lrc)>> { trace!("expansion"); self.expansion.compute(|| { let crate_name = *self.crate_name()?.borrow(); let (krate, lint_store) = self.register_plugins()?.steal(); let _timer = self.session().timer("configure_and_expand"); let sess = self.session(); - let mut resolver = passes::create_resolver( - sess.clone(), - self.codegen_backend().metadata_loader(), + + let arenas = Resolver::arenas(); + let mut resolver = Resolver::new( + sess, &krate, crate_name, + self.codegen_backend().metadata_loader(), + &arenas, ); - let krate = resolver.access(|resolver| { - passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver) - })?; - Ok((Lrc::new(krate), resolver, lint_store)) + let krate = + passes::configure_and_expand(sess, &lint_store, krate, crate_name, &mut resolver)?; + Ok((Lrc::new(krate), resolver.into_outputs(), lint_store)) }) } @@ -209,13 +212,13 @@ impl<'tcx> Queries<'tcx> { pub fn global_ctxt(&'tcx self) -> Result>> { self.gcx.compute(|| { let crate_name = *self.crate_name()?.borrow(); - let (krate, resolver, lint_store) = self.expansion()?.steal(); + let (krate, resolver_outputs, lint_store) = self.expansion()?.steal(); let ty::ResolverOutputs { untracked, global_ctxt: untracked_resolutions, ast_lowering: untracked_resolver_for_lowering, - } = resolver.into_outputs(); + } = resolver_outputs; let gcx = passes::create_global_ctxt( self.compiler, -- cgit 1.4.1-3-g733a5 From d15663814b8ec3b60da9e1c733c90e478ebb3ed0 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 14 Feb 2023 09:38:49 +0000 Subject: Inline the expansion query --- compiler/rustc_interface/src/queries.rs | 51 +++++++++++++++------------------ 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 752c1ec7538..fcd399c0540 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -13,7 +13,7 @@ use rustc_incremental::DepGraphFuture; use rustc_lint::LintStore; use rustc_middle::arena::Arena; use rustc_middle::dep_graph::DepGraph; -use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt}; +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}; @@ -88,7 +88,6 @@ pub struct Queries<'tcx> { parse: Query, crate_name: Query, register_plugins: Query<(ast::Crate, Lrc)>, - expansion: Query<(Lrc, ResolverOutputs, Lrc)>, dep_graph: Query, // This just points to what's in `gcx_cell`. gcx: Query<&'tcx GlobalCtxt<'tcx>>, @@ -107,7 +106,6 @@ impl<'tcx> Queries<'tcx> { parse: Default::default(), crate_name: Default::default(), register_plugins: Default::default(), - expansion: Default::default(), dep_graph: Default::default(), gcx: Default::default(), ongoing_codegen: Default::default(), @@ -169,30 +167,6 @@ impl<'tcx> Queries<'tcx> { }) } - pub fn expansion( - &self, - ) -> Result, ResolverOutputs, Lrc)>> { - trace!("expansion"); - self.expansion.compute(|| { - let crate_name = *self.crate_name()?.borrow(); - let (krate, lint_store) = self.register_plugins()?.steal(); - 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)?; - Ok((Lrc::new(krate), resolver.into_outputs(), lint_store)) - }) - } - fn dep_graph(&self) -> Result> { self.dep_graph.compute(|| { let sess = self.session(); @@ -212,7 +186,28 @@ impl<'tcx> Queries<'tcx> { pub fn global_ctxt(&'tcx self) -> Result>> { self.gcx.compute(|| { let crate_name = *self.crate_name()?.borrow(); - let (krate, resolver_outputs, lint_store) = self.expansion()?.steal(); + let (krate, resolver_outputs, lint_store) = { + let (krate, lint_store) = self.register_plugins()?.steal(); + 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(), lint_store) + }; let ty::ResolverOutputs { untracked, -- cgit 1.4.1-3-g733a5 From 241c6a4a616010382aefe297c69a26fb5a22dd06 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 14 Feb 2023 09:50:13 +0000 Subject: Simplify expansion logic --- compiler/rustc_interface/src/queries.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'compiler/rustc_interface/src') diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index fcd399c0540..d727efdafc2 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -186,8 +186,8 @@ impl<'tcx> Queries<'tcx> { pub fn global_ctxt(&'tcx self) -> Result>> { self.gcx.compute(|| { let crate_name = *self.crate_name()?.borrow(); - let (krate, resolver_outputs, lint_store) = { - let (krate, lint_store) = self.register_plugins()?.steal(); + let (krate, lint_store) = self.register_plugins()?.steal(); + let (krate, resolver_outputs) = { let _timer = self.session().timer("configure_and_expand"); let sess = self.session(); @@ -206,7 +206,7 @@ impl<'tcx> Queries<'tcx> { crate_name, &mut resolver, )?; - (Lrc::new(krate), resolver.into_outputs(), lint_store) + (Lrc::new(krate), resolver.into_outputs()) }; let ty::ResolverOutputs { -- cgit 1.4.1-3-g733a5