From a0b96fdb50b1a57447e840f2a904aad9912cd3c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?John=20K=C3=A5re=20Alsaker?= Date: Tue, 7 Feb 2023 12:23:31 +0100 Subject: [PATCH] Change `arena_cache` to not alter the declared query result --- compiler/rustc_middle/src/query/mod.rs | 109 +++++++++++++------------ compiler/rustc_middle/src/ty/query.rs | 39 +++++---- 2 files changed, 78 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 7aa9282b9be..0a16ede6499 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -54,14 +54,14 @@ /// This is because the `hir_crate` query gives you access to all other items. /// To avoid this fate, do not call `tcx.hir().krate()`; instead, /// prefer wrappers like `tcx.visit_all_items_in_krate()`. - query hir_crate(key: ()) -> Crate<'tcx> { + query hir_crate(key: ()) -> &'tcx Crate<'tcx> { arena_cache eval_always desc { "getting the crate HIR" } } /// All items in the crate. - query hir_crate_items(_: ()) -> rustc_middle::hir::ModuleItems { + query hir_crate_items(_: ()) -> &'tcx rustc_middle::hir::ModuleItems { arena_cache eval_always desc { "getting HIR crate items" } @@ -71,7 +71,7 @@ /// /// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`. /// Avoid calling this query directly. - query hir_module_items(key: LocalDefId) -> rustc_middle::hir::ModuleItems { + query hir_module_items(key: LocalDefId) -> &'tcx rustc_middle::hir::ModuleItems { arena_cache desc { |tcx| "getting HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) } cache_on_disk_if { true } @@ -183,7 +183,7 @@ separate_provide_extern } - query unsizing_params_for_adt(key: DefId) -> rustc_index::bit_set::BitSet + query unsizing_params_for_adt(key: DefId) -> &'tcx rustc_index::bit_set::BitSet { arena_cache desc { |tcx| @@ -218,7 +218,7 @@ /// Maps from the `DefId` of an item (trait/struct/enum/fn) to its /// associated generics. - query generics_of(key: DefId) -> ty::Generics { + query generics_of(key: DefId) -> &'tcx ty::Generics { desc { |tcx| "computing generics of `{}`", tcx.def_path_str(key) } arena_cache cache_on_disk_if { key.is_local() } @@ -295,19 +295,19 @@ /// These are assembled from the following places: /// - `extern` blocks (depending on their `link` attributes) /// - the `libs` (`-l`) option - query native_libraries(_: CrateNum) -> Vec { + query native_libraries(_: CrateNum) -> &'tcx Vec { arena_cache desc { "looking up the native libraries of a linked crate" } separate_provide_extern } - query shallow_lint_levels_on(key: hir::OwnerId) -> rustc_middle::lint::ShallowLintLevelMap { + query shallow_lint_levels_on(key: hir::OwnerId) -> &'tcx rustc_middle::lint::ShallowLintLevelMap { eval_always // fetches `resolutions` arena_cache desc { |tcx| "looking up lint levels for `{}`", tcx.def_path_str(key.to_def_id()) } } - query lint_expectations(_: ()) -> Vec<(LintExpectationId, LintExpectation)> { + query lint_expectations(_: ()) -> &'tcx Vec<(LintExpectationId, LintExpectation)> { arena_cache desc { "computing `#[expect]`ed lints in this crate" } } @@ -347,7 +347,7 @@ } /// Set of param indexes for type params that are in the type's representation - query params_in_repr(key: DefId) -> rustc_index::bit_set::BitSet { + query params_in_repr(key: DefId) -> &'tcx rustc_index::bit_set::BitSet { desc { "finding type parameters in the representation" } arena_cache no_hash @@ -364,14 +364,14 @@ } /// Create a THIR tree for debugging. - query thir_tree(key: ty::WithOptConstParam) -> String { + query thir_tree(key: ty::WithOptConstParam) -> &'tcx String { no_hash arena_cache desc { |tcx| "constructing THIR tree for `{}`", tcx.def_path_str(key.did.to_def_id()) } } /// Create a list-like THIR representation for debugging. - query thir_flat(key: ty::WithOptConstParam) -> String { + query thir_flat(key: ty::WithOptConstParam) -> &'tcx String { no_hash arena_cache desc { |tcx| "constructing flat THIR representation for `{}`", tcx.def_path_str(key.did.to_def_id()) } @@ -380,7 +380,7 @@ /// Set of all the `DefId`s in this crate that have MIR associated with /// them. This includes all the body owners, but also things like struct /// constructors. - query mir_keys(_: ()) -> rustc_data_structures::fx::FxIndexSet { + query mir_keys(_: ()) -> &'tcx rustc_data_structures::fx::FxIndexSet { arena_cache desc { "getting a list of all mir_keys" } } @@ -478,7 +478,7 @@ query symbols_for_closure_captures( key: (LocalDefId, LocalDefId) - ) -> Vec { + ) -> &'tcx Vec { arena_cache desc { |tcx| "finding symbols for captures of closure `{}` in `{}`", @@ -487,7 +487,7 @@ } } - query mir_generator_witnesses(key: DefId) -> mir::GeneratorLayout<'tcx> { + query mir_generator_witnesses(key: DefId) -> &'tcx mir::GeneratorLayout<'tcx> { arena_cache desc { |tcx| "generator witness types for `{}`", tcx.def_path_str(key) } cache_on_disk_if { key.is_local() } @@ -508,14 +508,14 @@ /// Returns coverage summary info for a function, after executing the `InstrumentCoverage` /// MIR pass (assuming the -Cinstrument-coverage option is enabled). - query coverageinfo(key: ty::InstanceDef<'tcx>) -> mir::CoverageInfo { + query coverageinfo(key: ty::InstanceDef<'tcx>) -> &'tcx mir::CoverageInfo { desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key.def_id()) } arena_cache } /// Returns the `CodeRegions` for a function that has instrumented coverage, in case the /// function was optimized out before codegen, and before being added to the Coverage Map. - query covered_code_regions(key: DefId) -> Vec<&'tcx mir::coverage::CodeRegion> { + query covered_code_regions(key: DefId) -> &'tcx Vec<&'tcx mir::coverage::CodeRegion> { desc { |tcx| "retrieving the covered `CodeRegion`s, if instrumented, for `{}`", tcx.def_path_str(key) @@ -557,7 +557,7 @@ desc { "erasing regions from `{}`", ty } } - query wasm_import_module_map(_: CrateNum) -> FxHashMap { + query wasm_import_module_map(_: CrateNum) -> &'tcx FxHashMap { arena_cache desc { "getting wasm import module map" } } @@ -632,7 +632,7 @@ desc { |tcx| "computing the bounds for type parameter `{}`", tcx.hir().ty_param_name(key.1) } } - query trait_def(key: DefId) -> ty::TraitDef { + query trait_def(key: DefId) -> &'tcx ty::TraitDef { desc { |tcx| "computing trait definition for `{}`", tcx.def_path_str(key) } arena_cache cache_on_disk_if { key.is_local() } @@ -703,7 +703,7 @@ } /// Gets a map with the variance of every item; use `item_variance` instead. - query crate_variances(_: ()) -> ty::CrateVariancesMap<'tcx> { + query crate_variances(_: ()) -> &'tcx ty::CrateVariancesMap<'tcx> { arena_cache desc { "computing the variances for items in this crate" } } @@ -716,7 +716,7 @@ } /// Maps from thee `DefId` of a type to its (inferred) outlives. - query inferred_outlives_crate(_: ()) -> ty::CratePredicatesMap<'tcx> { + query inferred_outlives_crate(_: ()) -> &'tcx ty::CratePredicatesMap<'tcx> { arena_cache desc { "computing the inferred outlives predicates for items in this crate" } } @@ -729,7 +729,7 @@ } /// Maps from a trait item to the trait item "descriptor". - query associated_item(key: DefId) -> ty::AssocItem { + query associated_item(key: DefId) -> &'tcx ty::AssocItem { desc { |tcx| "computing associated item data for `{}`", tcx.def_path_str(key) } arena_cache cache_on_disk_if { key.is_local() } @@ -737,7 +737,7 @@ } /// Collects the associated items defined on a trait or impl. - query associated_items(key: DefId) -> ty::AssocItems<'tcx> { + query associated_items(key: DefId) -> &'tcx ty::AssocItems<'tcx> { arena_cache desc { |tcx| "collecting associated items of `{}`", tcx.def_path_str(key) } } @@ -763,7 +763,7 @@ /// /// The map returned for `tcx.impl_item_implementor_ids(impl_id)` would be ///`{ trait_f: impl_f, trait_g: impl_g }` - query impl_item_implementor_ids(impl_id: DefId) -> FxHashMap { + query impl_item_implementor_ids(impl_id: DefId) -> &'tcx FxHashMap { arena_cache desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) } } @@ -884,7 +884,7 @@ /// /// The second return value maps from ADTs to ignored derived traits (e.g. Debug and Clone) and /// their respective impl (i.e., part of the derive macro) - query live_symbols_and_ignored_derived_traits(_: ()) -> ( + query live_symbols_and_ignored_derived_traits(_: ()) -> &'tcx ( FxHashSet, FxHashMap> ) { @@ -964,7 +964,7 @@ /// Gets a complete map from all types to their inherent impls. /// Not meant to be used directly outside of coherence. - query crate_inherent_impls(k: ()) -> CrateInherentImpls { + query crate_inherent_impls(k: ()) -> &'tcx CrateInherentImpls { arena_cache desc { "finding all inherent impls defined in crate" } } @@ -1099,7 +1099,7 @@ desc { "checking for private elements in public interfaces" } } - query reachable_set(_: ()) -> FxHashSet { + query reachable_set(_: ()) -> &'tcx FxHashSet { arena_cache desc { "reachability" } } @@ -1111,7 +1111,7 @@ } /// Generates a MIR body for the shim. - query mir_shims(key: ty::InstanceDef<'tcx>) -> mir::Body<'tcx> { + query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::Body<'tcx> { arena_cache desc { |tcx| "generating MIR shim for `{}`", tcx.def_path_str(key.def_id()) } } @@ -1191,7 +1191,7 @@ separate_provide_extern } - query codegen_fn_attrs(def_id: DefId) -> CodegenFnAttrs { + query codegen_fn_attrs(def_id: DefId) -> &'tcx CodegenFnAttrs { desc { |tcx| "computing codegen attributes of `{}`", tcx.def_path_str(def_id) } arena_cache cache_on_disk_if { def_id.is_local() } @@ -1209,7 +1209,7 @@ } /// Gets the rendered value of the specified constant or associated constant. /// Used by rustdoc. - query rendered_const(def_id: DefId) -> String { + query rendered_const(def_id: DefId) -> &'tcx String { arena_cache desc { |tcx| "rendering constant initializer of `{}`", tcx.def_path_str(def_id) } cache_on_disk_if { def_id.is_local() } @@ -1268,12 +1268,12 @@ } /// Given a trait `trait_id`, return all known `impl` blocks. - query trait_impls_of(trait_id: DefId) -> ty::trait_def::TraitImpls { + query trait_impls_of(trait_id: DefId) -> &'tcx ty::trait_def::TraitImpls { arena_cache desc { |tcx| "finding trait impls of `{}`", tcx.def_path_str(trait_id) } } - query specialization_graph_of(trait_id: DefId) -> specialization_graph::Graph { + query specialization_graph_of(trait_id: DefId) -> &'tcx specialization_graph::Graph { arena_cache desc { |tcx| "building specialization graph of trait `{}`", tcx.def_path_str(trait_id) } cache_on_disk_if { true } @@ -1403,7 +1403,7 @@ separate_provide_extern } - query dependency_formats(_: ()) -> Lrc { + query dependency_formats(_: ()) -> &'tcx Lrc { arena_cache desc { "getting the linkage format of all dependencies" } } @@ -1503,7 +1503,7 @@ // Does not include external symbols that don't have a corresponding DefId, // like the compiler-generated `main` function and so on. query reachable_non_generics(_: CrateNum) - -> DefIdMap { + -> &'tcx DefIdMap { arena_cache desc { "looking up the exported symbols of a crate" } separate_provide_extern @@ -1526,7 +1526,7 @@ /// added or removed in any upstream crate. Instead use the narrower /// `upstream_monomorphizations_for`, `upstream_drop_glue_for`, or, even /// better, `Instance::upstream_monomorphization()`. - query upstream_monomorphizations(_: ()) -> DefIdMap, CrateNum>> { + query upstream_monomorphizations(_: ()) -> &'tcx DefIdMap, CrateNum>> { arena_cache desc { "collecting available upstream monomorphizations" } } @@ -1568,7 +1568,7 @@ } /// Returns a list of all `extern` blocks of a crate. - query foreign_modules(_: CrateNum) -> FxHashMap { + query foreign_modules(_: CrateNum) -> &'tcx FxHashMap { arena_cache desc { "looking up the foreign modules of a linked crate" } separate_provide_extern @@ -1602,7 +1602,7 @@ /// Gets the extra data to put in each output filename for a crate. /// For example, compiling the `foo` crate with `extra-filename=-a` creates a `libfoo-b.rlib` file. - query extra_filename(_: CrateNum) -> String { + query extra_filename(_: CrateNum) -> &'tcx String { arena_cache eval_always desc { "looking up the extra filename for a crate" } @@ -1610,7 +1610,7 @@ } /// Gets the paths where the crate came from in the file system. - query crate_extern_paths(_: CrateNum) -> Vec { + query crate_extern_paths(_: CrateNum) -> &'tcx Vec { arena_cache eval_always desc { "looking up the paths for extern crates" } @@ -1641,7 +1641,7 @@ /// Does lifetime resolution on items. Importantly, we can't resolve /// lifetimes directly on things like trait methods, because of trait params. /// See `rustc_resolve::late::lifetimes for details. - query resolve_lifetimes(_: hir::OwnerId) -> ResolveLifetimes { + query resolve_lifetimes(_: hir::OwnerId) -> &'tcx ResolveLifetimes { arena_cache desc { "resolving lifetimes" } } @@ -1712,7 +1712,7 @@ desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id.to_def_id()) } } - query lib_features(_: ()) -> LibFeatures { + query lib_features(_: ()) -> &'tcx LibFeatures { arena_cache desc { "calculating the lib features map" } } @@ -1720,7 +1720,7 @@ desc { "calculating the lib features defined in a crate" } separate_provide_extern } - query stability_implications(_: CrateNum) -> FxHashMap { + query stability_implications(_: CrateNum) -> &'tcx FxHashMap { arena_cache desc { "calculating the implications between `#[unstable]` features defined in a crate" } separate_provide_extern @@ -1731,14 +1731,14 @@ separate_provide_extern } /// Returns the lang items defined in another crate by loading it from metadata. - query get_lang_items(_: ()) -> LanguageItems { + query get_lang_items(_: ()) -> &'tcx LanguageItems { arena_cache eval_always desc { "calculating the lang items map" } } /// Returns all diagnostic items defined in all crates. - query all_diagnostic_items(_: ()) -> rustc_hir::diagnostic_items::DiagnosticItems { + query all_diagnostic_items(_: ()) -> &'tcx rustc_hir::diagnostic_items::DiagnosticItems { arena_cache eval_always desc { "calculating the diagnostic items map" } @@ -1751,7 +1751,7 @@ } /// Returns the diagnostic items defined in a crate. - query diagnostic_items(_: CrateNum) -> rustc_hir::diagnostic_items::DiagnosticItems { + query diagnostic_items(_: CrateNum) -> &'tcx rustc_hir::diagnostic_items::DiagnosticItems { arena_cache desc { "calculating the diagnostic items map in a crate" } separate_provide_extern @@ -1761,11 +1761,11 @@ desc { "calculating the missing lang items in a crate" } separate_provide_extern } - query visible_parent_map(_: ()) -> DefIdMap { + query visible_parent_map(_: ()) -> &'tcx DefIdMap { arena_cache desc { "calculating the visible parent map" } } - query trimmed_def_paths(_: ()) -> FxHashMap { + query trimmed_def_paths(_: ()) -> &'tcx FxHashMap { arena_cache desc { "calculating trimmed def paths" } } @@ -1774,14 +1774,14 @@ desc { "seeing if we're missing an `extern crate` item for this crate" } separate_provide_extern } - query used_crate_source(_: CrateNum) -> Lrc { + query used_crate_source(_: CrateNum) -> &'tcx Lrc { arena_cache eval_always desc { "looking at the source for a crate" } separate_provide_extern } /// Returns the debugger visualizers defined for this crate. - query debugger_visualizers(_: CrateNum) -> Vec { + query debugger_visualizers(_: CrateNum) -> &'tcx Vec { arena_cache desc { "looking up the debugger visualizers for this crate" } separate_provide_extern @@ -1819,7 +1819,7 @@ desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) } } - query stability_index(_: ()) -> stability::Index { + query stability_index(_: ()) -> &'tcx stability::Index { arena_cache eval_always desc { "calculating the stability index for the local crate" } @@ -1883,7 +1883,7 @@ /// /// This query returns an `&Arc` because codegen backends need the value even after the `TyCtxt` /// has been destroyed. - query output_filenames(_: ()) -> Arc { + query output_filenames(_: ()) -> &'tcx Arc { feedable desc { "getting output filenames" } arena_cache @@ -2056,7 +2056,7 @@ remap_env_constness } - query supported_target_features(_: CrateNum) -> FxHashMap> { + query supported_target_features(_: CrateNum) -> &'tcx FxHashMap> { arena_cache eval_always desc { "looking up supported target features" } @@ -2115,23 +2115,24 @@ /// span) for an *existing* error. Therefore, it is best-effort, and may never handle /// all of the cases that the normal `ty::Ty`-based wfcheck does. This is fine, /// because the `ty::Ty`-based wfcheck is always run. - query diagnostic_hir_wf_check(key: (ty::Predicate<'tcx>, traits::WellFormedLoc)) -> Option> { + query diagnostic_hir_wf_check( + key: (ty::Predicate<'tcx>, traits::WellFormedLoc) + ) -> &'tcx Option> { arena_cache eval_always no_hash desc { "performing HIR wf-checking for predicate `{:?}` at item `{:?}`", key.0, key.1 } } - /// The list of backend features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`, /// `--target` and similar). - query global_backend_features(_: ()) -> Vec { + query global_backend_features(_: ()) -> &'tcx Vec { arena_cache eval_always desc { "computing the backend features for CLI flags" } } - query generator_diagnostic_data(key: DefId) -> Option> { + query generator_diagnostic_data(key: DefId) -> &'tcx Option> { arena_cache desc { |tcx| "looking up generator diagnostic data of `{}`", tcx.def_path_str(key) } separate_provide_extern diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 7151b79c5ab..933aaadd62e 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -112,15 +112,15 @@ macro_rules! query_helper_param_ty { ($K:ty) => { $K }; } -macro_rules! query_storage { - ([][$K:ty, $V:ty]) => { - <<$K as Key>::CacheSelector as CacheSelector<'tcx, $V>>::Cache +macro_rules! query_if_arena { + ([] $arena:ty, $no_arena:ty) => { + $no_arena }; - ([(arena_cache) $($rest:tt)*][$K:ty, $V:ty]) => { - <<$K as Key>::CacheSelector as CacheSelector<'tcx, $V>>::ArenaCache + ([(arena_cache) $($rest:tt)*] $arena:ty, $no_arena:ty) => { + $arena }; - ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => { - query_storage!([$($modifiers)*][$($args)*]) + ([$other:tt $($modifiers:tt)*]$($args:tt)*) => { + query_if_arena!([$($modifiers)*]$($args)*) }; } @@ -184,23 +184,30 @@ pub mod query_keys { $(pub type $name<'tcx> = $($K)*;)* } - #[allow(nonstandard_style, unused_lifetimes)] + #[allow(nonstandard_style, unused_lifetimes, unused_parens)] pub mod query_values { use super::*; - $(pub type $name<'tcx> = $V;)* + $(pub type $name<'tcx> = query_if_arena!([$($modifiers)*] <$V as Deref>::Target, $V);)* } - #[allow(nonstandard_style, unused_lifetimes)] + #[allow(nonstandard_style, unused_lifetimes, unused_parens)] pub mod query_storage { use super::*; - $(pub type $name<'tcx> = query_storage!([$($modifiers)*][$($K)*, $V]);)* + $( + pub type $name<'tcx> = query_if_arena!([$($modifiers)*] + <<$($K)* as Key>::CacheSelector + as CacheSelector<'tcx, <$V as Deref>::Target>>::ArenaCache, + <<$($K)* as Key>::CacheSelector as CacheSelector<'tcx, $V>>::Cache + ); + )* } + #[allow(nonstandard_style, unused_lifetimes)] pub mod query_stored { use super::*; - $(pub type $name<'tcx> = as QueryStorage>::Stored;)* + $(pub type $name<'tcx> = $V;)* } #[derive(Default)] @@ -226,7 +233,7 @@ impl<'tcx> TyCtxt<'tcx> { $($(#[$attr])* #[inline(always)] #[must_use] - pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<'tcx> + pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { self.at(DUMMY_SP).$name(key) })* @@ -235,7 +242,7 @@ pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<' impl<'tcx> TyCtxtAt<'tcx> { $($(#[$attr])* #[inline(always)] - pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<'tcx> + pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V { let key = key.into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); @@ -306,7 +313,7 @@ fn $name( span: Span, key: query_keys::$name<'tcx>, mode: QueryMode, - ) -> Option>;)* + ) -> Option<$V>;)* } }; } @@ -328,7 +335,7 @@ macro_rules! define_feedable { $(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> { $(#[$attr])* #[inline(always)] - pub fn $name(self, value: $V) -> query_stored::$name<'tcx> { + pub fn $name(self, value: query_values::$name<'tcx>) -> $V { let key = self.key().into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); -- 2.44.0