From 227d91248988f24c14f9d440ec0171c844bd89e0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 19 Feb 2022 15:36:11 +0100 Subject: [PATCH 1/9] Stop interning stability. --- .../src/rmeta/decoder/cstore_impl.rs | 8 ++------ compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_middle/src/middle/stability.rs | 14 +++++++------- compiler/rustc_middle/src/query/mod.rs | 6 +++--- compiler/rustc_middle/src/ty/context.rs | 19 +------------------ compiler/rustc_passes/src/stability.rs | 18 ++++++++---------- src/librustdoc/clean/types.rs | 4 ++-- 7 files changed, 24 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 7708b5193f450..8b462ca09a531 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -129,12 +129,8 @@ provide! { <'tcx> tcx, def_id, other, cdata, opt_def_kind => { Some(cdata.def_kind(def_id.index)) } def_span => { cdata.get_span(def_id.index, &tcx.sess) } def_ident_span => { cdata.opt_item_ident(def_id.index, &tcx.sess).map(|ident| ident.span) } - lookup_stability => { - cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s)) - } - lookup_const_stability => { - cdata.get_const_stability(def_id.index).map(|s| tcx.intern_const_stability(s)) - } + lookup_stability => { cdata.get_stability(def_id.index) } + lookup_const_stability => { cdata.get_const_stability(def_id.index) } lookup_deprecation_entry => { cdata.get_deprecation(def_id.index).map(DeprecationEntry::external) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 4dea04e62ff07..3c4b1899343f4 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1661,7 +1661,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let hir = tcx.hir(); let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; - let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied(); + let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)); let macros = self.lazy(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)); let spans = self.tcx.sess.parse_sess.proc_macro_quoted_spans(); diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index fedf456ccc88f..54d53a810b11b 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -57,11 +57,11 @@ impl DeprecationEntry { /// A stability index, giving the stability level for items and methods. #[derive(HashStable, Debug)] -pub struct Index<'tcx> { +pub struct Index { /// This is mostly a cache, except the stabilities of local items /// are filled by the annotator. - pub stab_map: FxHashMap, - pub const_stab_map: FxHashMap, + pub stab_map: FxHashMap, + pub const_stab_map: FxHashMap, pub depr_map: FxHashMap, /// Maps for each crate whether it is part of the staged API. @@ -71,12 +71,12 @@ pub struct Index<'tcx> { pub active_features: FxHashSet, } -impl<'tcx> Index<'tcx> { - pub fn local_stability(&self, def_id: LocalDefId) -> Option<&'tcx Stability> { +impl Index { + pub fn local_stability(&self, def_id: LocalDefId) -> Option { self.stab_map.get(&def_id).copied() } - pub fn local_const_stability(&self, def_id: LocalDefId) -> Option<&'tcx ConstStability> { + pub fn local_const_stability(&self, def_id: LocalDefId) -> Option { self.const_stab_map.get(&def_id).copied() } @@ -416,7 +416,7 @@ impl<'tcx> TyCtxt<'tcx> { } match stability { - Some(&Stability { + Some(Stability { level: attr::Unstable { reason, issue, is_soft }, feature, .. }) => { if span.allows_unstable(feature) { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 43cfe6f3b8a7a..46103b77adee8 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1010,12 +1010,12 @@ rustc_queries! { separate_provide_extern } - query lookup_stability(def_id: DefId) -> Option<&'tcx attr::Stability> { + query lookup_stability(def_id: DefId) -> Option { desc { |tcx| "looking up stability of `{}`", tcx.def_path_str(def_id) } separate_provide_extern } - query lookup_const_stability(def_id: DefId) -> Option<&'tcx attr::ConstStability> { + query lookup_const_stability(def_id: DefId) -> Option { desc { |tcx| "looking up const stability of `{}`", tcx.def_path_str(def_id) } separate_provide_extern } @@ -1626,7 +1626,7 @@ rustc_queries! { desc { |tcx| "names_imported_by_glob_use for `{}`", tcx.def_path_str(def_id.to_def_id()) } } - query stability_index(_: ()) -> stability::Index<'tcx> { + query stability_index(_: ()) -> stability::Index { storage(ArenaCacheSelector<'tcx>) eval_always desc { "calculating the stability index for the local crate" } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 41145d250173f..33db83f052484 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -24,7 +24,6 @@ use crate::ty::{ RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, }; use rustc_ast as ast; -use rustc_attr as attr; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::intern::Interned; use rustc_data_structures::memmap::Mmap; @@ -116,12 +115,6 @@ pub struct CtxtInterners<'tcx> { bound_variable_kinds: InternedSet<'tcx, List>, layout: InternedSet<'tcx, Layout>, adt_def: InternedSet<'tcx, AdtDef>, - - /// `#[stable]` and `#[unstable]` attributes - stability: InternedSet<'tcx, attr::Stability>, - - /// `#[rustc_const_stable]` and `#[rustc_const_unstable]` attributes - const_stability: InternedSet<'tcx, attr::ConstStability>, } impl<'tcx> CtxtInterners<'tcx> { @@ -143,8 +136,6 @@ impl<'tcx> CtxtInterners<'tcx> { bound_variable_kinds: Default::default(), layout: Default::default(), adt_def: Default::default(), - stability: Default::default(), - const_stability: Default::default(), } } @@ -1271,7 +1262,7 @@ impl<'tcx> TyCtxt<'tcx> { self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did) } - pub fn stability(self) -> &'tcx stability::Index<'tcx> { + pub fn stability(self) -> &'tcx stability::Index { self.stability_index(()) } @@ -1981,12 +1972,6 @@ impl<'tcx> TyCtxt<'tcx> { writeln!(fmt, "InternalSubsts interner: #{}", self.0.interners.substs.len())?; writeln!(fmt, "Region interner: #{}", self.0.interners.region.len())?; - writeln!(fmt, "Stability interner: #{}", self.0.interners.stability.len())?; - writeln!( - fmt, - "Const Stability interner: #{}", - self.0.interners.const_stability.len() - )?; writeln!( fmt, "Const Allocation interner: #{}", @@ -2174,8 +2159,6 @@ direct_interners_old! { const_allocation: intern_const_alloc(Allocation), layout: intern_layout(Layout), adt_def: intern_adt_def(AdtDef), - stability: intern_stability(attr::Stability), - const_stability: intern_const_stability(attr::ConstStability), } macro_rules! slice_interners { diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 136059677c5ae..591f32c0a10db 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -87,9 +87,9 @@ impl InheritStability { // A private tree-walker for producing an Index. struct Annotator<'a, 'tcx> { tcx: TyCtxt<'tcx>, - index: &'a mut Index<'tcx>, - parent_stab: Option<&'tcx Stability>, - parent_const_stab: Option<&'tcx ConstStability>, + index: &'a mut Index, + parent_stab: Option, + parent_const_stab: Option, parent_depr: Option, in_trait_impl: bool, } @@ -171,7 +171,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> { let mut const_span = None; let const_stab = const_stab.map(|(const_stab, const_span_node)| { - let const_stab = self.tcx.intern_const_stability(const_stab); self.index.const_stab_map.insert(def_id, const_stab); const_span = Some(const_span_node); const_stab @@ -228,7 +227,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> { } debug!("annotate: found {:?}", stab); - let stab = self.tcx.intern_stability(stab); // Check if deprecated_since < stable_since. If it is, // this is *almost surely* an accident. @@ -299,8 +297,8 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> { fn recurse_with_stability_attrs( &mut self, depr: Option, - stab: Option<&'tcx Stability>, - const_stab: Option<&'tcx ConstStability>, + stab: Option, + const_stab: Option, f: impl FnOnce(&mut Self), ) { // These will be `Some` if this item changes the corresponding stability attribute. @@ -655,7 +653,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { // stable (assuming they have not inherited instability from their parent). } -fn stability_index<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> { +fn stability_index(tcx: TyCtxt<'_>, (): ()) -> Index { let is_staged_api = tcx.sess.opts.debugging_opts.force_unstable_if_unmarked || tcx.features().staged_api; let mut staged_api = FxHashMap::default(); @@ -698,14 +696,14 @@ fn stability_index<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> { let reason = "this crate is being loaded from the sysroot, an \ unstable location; did you mean to load this crate \ from crates.io via `Cargo.toml` instead?"; - let stability = tcx.intern_stability(Stability { + let stability = Stability { level: attr::StabilityLevel::Unstable { reason: Some(Symbol::intern(reason)), issue: NonZeroU32::new(27812), is_soft: false, }, feature: sym::rustc_private, - }); + }; annotator.parent_stab = Some(stability); } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 74184427dd573..467a6940628e7 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -381,12 +381,12 @@ crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span { } impl Item { - crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> { + crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option { self.def_id.as_def_id().and_then(|did| tcx.lookup_stability(did)) } crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option { - self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)).map(|cs| *cs) + self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did)) } crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option { From f07f1bfc606874d045714b7d957027c64377d64c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 17 Feb 2022 21:33:23 +0100 Subject: [PATCH 2/9] Encode metadata using queries. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 263 ++++++++---------- .../src/rmeta/decoder/cstore_impl.rs | 114 ++++---- compiler/rustc_metadata/src/rmeta/encoder.rs | 44 +-- compiler/rustc_metadata/src/rmeta/mod.rs | 44 +-- 4 files changed, 222 insertions(+), 243 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 66968c9ba54ab..cf32f1dd6abb6 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -11,7 +11,6 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell}; use rustc_data_structures::unhash::UnhashMap; -use rustc_errors::ErrorReported; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive}; use rustc_hir as hir; @@ -21,10 +20,12 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; use rustc_hir::diagnostic_items::DiagnosticItems; use rustc_hir::lang_items; use rustc_index::vec::{Idx, IndexVec}; +use rustc_middle::arena::ArenaAllocatable; use rustc_middle::metadata::ModChild; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; +use rustc_middle::middle::stability::DeprecationEntry; +use rustc_middle::mir; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; -use rustc_middle::mir::{self, Body, Promoted}; use rustc_middle::thir; use rustc_middle::ty::codec::TyDecoder; use rustc_middle::ty::fast_reject::SimplifiedType; @@ -278,6 +279,99 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable>> Lazy<[T]> { } } +trait LazyQueryDecodable<'a, 'tcx, T> { + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + err: impl FnOnce() -> !, + ) -> T; +} + +impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option> +where + T: Decodable>, +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + err: impl FnOnce() -> !, + ) -> T { + if let Some(l) = self { l.decode((cdata, tcx)) } else { err() } + } +} + +impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx T> for Option> +where + T: Decodable>, + T: ArenaAllocatable<'tcx>, +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + err: impl FnOnce() -> !, + ) -> &'tcx T { + if let Some(l) = self { tcx.arena.alloc(l.decode((cdata, tcx))) } else { err() } + } +} + +impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, Option> for Option> +where + T: Decodable>, +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + _err: impl FnOnce() -> !, + ) -> Option { + self.map(|l| l.decode((cdata, tcx))) + } +} + +impl<'a, 'tcx, T, E> LazyQueryDecodable<'a, 'tcx, Result, E>> for Option> +where + T: Decodable>, +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + _err: impl FnOnce() -> !, + ) -> Result, E> { + Ok(self.map(|l| l.decode((cdata, tcx)))) + } +} + +impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx [T]> for Option> +where + T: Decodable> + Copy, +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + _err: impl FnOnce() -> !, + ) -> &'tcx [T] { + if let Some(l) = self { tcx.arena.alloc_from_iter(l.decode((cdata, tcx))) } else { &[] } + } +} + +impl<'a, 'tcx> LazyQueryDecodable<'a, 'tcx, Option> + for Option> +{ + fn decode_query( + self, + cdata: CrateMetadataRef<'a>, + tcx: TyCtxt<'tcx>, + _err: impl FnOnce() -> !, + ) -> Option { + self.map(|l| l.decode((cdata, tcx))).map(DeprecationEntry::external) + } +} + impl<'a, 'tcx> DecodeContext<'a, 'tcx> { #[inline] fn tcx(&self) -> TyCtxt<'tcx> { @@ -716,7 +810,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option { let name = self.def_key(item_index).disambiguated_data.data.get_opt_name()?; - let span = match self.root.tables.ident_span.get(self, item_index) { + let span = match self.root.tables.def_ident_span.get(self, item_index) { Some(lazy_span) => lazy_span.decode((self, sess)), None => { // FIXME: this weird case of a name with no span is specific to `extern crate` @@ -750,20 +844,22 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } fn def_kind(self, item_id: DefIndex) -> DefKind { - self.root.tables.def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(|| { - bug!( - "CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}", - item_id, - self.root.name, - self.cnum, - ) - }) + self.root.tables.opt_def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else( + || { + bug!( + "CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}", + item_id, + self.root.name, + self.cnum, + ) + }, + ) } fn get_span(self, index: DefIndex, sess: &Session) -> Span { self.root .tables - .span + .def_span .get(self, index) .unwrap_or_else(|| panic!("Missing span for {:?}", index)) .decode((self, sess)) @@ -908,71 +1004,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { tcx.alloc_adt_def(did, adt_kind, variants, repr) } - fn get_explicit_predicates( - self, - item_id: DefIndex, - tcx: TyCtxt<'tcx>, - ) -> ty::GenericPredicates<'tcx> { - self.root.tables.explicit_predicates.get(self, item_id).unwrap().decode((self, tcx)) - } - - fn get_inferred_outlives( - self, - item_id: DefIndex, - tcx: TyCtxt<'tcx>, - ) -> &'tcx [(ty::Predicate<'tcx>, Span)] { - self.root - .tables - .inferred_outlives - .get(self, item_id) - .map(|predicates| tcx.arena.alloc_from_iter(predicates.decode((self, tcx)))) - .unwrap_or_default() - } - - fn get_super_predicates( - self, - item_id: DefIndex, - tcx: TyCtxt<'tcx>, - ) -> ty::GenericPredicates<'tcx> { - self.root.tables.super_predicates.get(self, item_id).unwrap().decode((self, tcx)) - } - - fn get_explicit_item_bounds( - self, - item_id: DefIndex, - tcx: TyCtxt<'tcx>, - ) -> &'tcx [(ty::Predicate<'tcx>, Span)] { - self.root - .tables - .explicit_item_bounds - .get(self, item_id) - .map(|bounds| tcx.arena.alloc_from_iter(bounds.decode((self, tcx)))) - .unwrap_or_default() - } - fn get_generics(self, item_id: DefIndex, sess: &Session) -> ty::Generics { - self.root.tables.generics.get(self, item_id).unwrap().decode((self, sess)) - } - - fn get_type(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { - self.root - .tables - .ty - .get(self, id) - .unwrap_or_else(|| panic!("Not a type: {:?}", id)) - .decode((self, tcx)) - } - - fn get_stability(self, id: DefIndex) -> Option { - self.root.tables.stability.get(self, id).map(|stab| stab.decode(self)) - } - - fn get_const_stability(self, id: DefIndex) -> Option { - self.root.tables.const_stability.get(self, id).map(|stab| stab.decode(self)) - } - - fn get_deprecation(self, id: DefIndex) -> Option { - self.root.tables.deprecation.get(self, id).map(|depr| depr.decode(self)) + self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess)) } fn get_visibility(self, id: DefIndex) -> ty::Visibility { @@ -1010,22 +1043,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { self.get_impl_data(id).coerce_unsized_info } - fn get_impl_trait(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option> { - self.root.tables.impl_trait_ref.get(self, id).map(|tr| tr.decode((self, tcx))) - } - fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId { self.root.tables.expn_that_defined.get(self, id).unwrap().decode((self, sess)) } - fn get_const_param_default( - self, - tcx: TyCtxt<'tcx>, - id: DefIndex, - ) -> rustc_middle::ty::Const<'tcx> { - self.root.tables.const_defaults.get(self, id).unwrap().decode((self, tcx)) - } - /// Iterates over all the stability attributes in the given crate. fn get_lib_features(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option)] { tcx.arena.alloc_from_iter(self.root.lib_features.decode(self)) @@ -1163,7 +1184,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } fn is_item_mir_available(self, id: DefIndex) -> bool { - self.root.tables.mir.get(self, id).is_some() + self.root.tables.optimized_mir.get(self, id).is_some() } fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId { @@ -1175,60 +1196,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn get_optimized_mir(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> { - self.root - .tables - .mir - .get(self, id) - .unwrap_or_else(|| { - bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id)) - }) - .decode((self, tcx)) - } - - fn get_mir_for_ctfe(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> { - self.root - .tables - .mir_for_ctfe - .get(self, id) - .unwrap_or_else(|| { - bug!("get_mir_for_ctfe: missing MIR for `{:?}`", self.local_def_id(id)) - }) - .decode((self, tcx)) - } - - fn get_thir_abstract_const( - self, - tcx: TyCtxt<'tcx>, - id: DefIndex, - ) -> Result]>, ErrorReported> { - self.root - .tables - .thir_abstract_consts - .get(self, id) - .map_or(Ok(None), |v| Ok(Some(v.decode((self, tcx))))) - } - - fn get_unused_generic_params(self, id: DefIndex) -> FiniteBitSet { - self.root - .tables - .unused_generic_params - .get(self, id) - .map(|params| params.decode(self)) - .unwrap_or_default() - } - - fn get_promoted_mir(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> IndexVec> { - self.root - .tables - .promoted_mir - .get(self, id) - .unwrap_or_else(|| { - bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id)) - }) - .decode((self, tcx)) - } - fn mir_const_qualif(self, id: DefIndex) -> mir::ConstQualifs { match self.kind(id) { EntryKind::AnonConst(qualif, _) @@ -1288,10 +1255,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn get_item_variances(self, id: DefIndex) -> impl Iterator + 'a { - self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self) - } - fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> { match self.kind(node_id) { EntryKind::Struct(data, _) | EntryKind::Variant(data) => { @@ -1479,7 +1442,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names, _ => Lazy::empty(), }; - tcx.arena.alloc_from_iter(param_names.decode((self, tcx))) + LazyQueryDecodable::decode_query(Some(param_names), self, tcx, || unreachable!()) } fn exported_symbols( @@ -1551,10 +1514,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn fn_sig(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> { - self.root.tables.fn_sig.get(self, id).unwrap().decode((self, tcx)) - } - #[inline] fn def_key(self, index: DefIndex) -> DefKey { *self diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 8b462ca09a531..ef840181b848d 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -1,3 +1,4 @@ +use super::LazyQueryDecodable; use crate::creader::{CStore, LoadedMacro}; use crate::foreign_modules; use crate::native_libs; @@ -8,7 +9,6 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE} use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::exported_symbols::ExportedSymbol; -use rustc_middle::middle::stability::DeprecationEntry; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::{ExternProviders, Providers}; use rustc_middle::ty::{self, TyCtxt, Visibility}; @@ -23,32 +23,51 @@ use rustc_data_structures::sync::Lrc; use smallvec::SmallVec; use std::any::Any; +macro_rules! provide_one { + (<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table }) => { + provide_one! { + <$lt> $tcx, $def_id, $other, $cdata, $name => { + $cdata.root.tables.$name.get($cdata, $def_id.index).decode_query( + $cdata, + $tcx, + || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)), + ) + } + } + }; + (<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => $compute:block) => { + fn $name<$lt>( + $tcx: TyCtxt<$lt>, + def_id_arg: ty::query::query_keys::$name<$lt>, + ) -> ty::query::query_values::$name<$lt> { + let _prof_timer = + $tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name))); + + #[allow(unused_variables)] + let ($def_id, $other) = def_id_arg.into_args(); + assert!(!$def_id.is_local()); + + // External query providers call `crate_hash` in order to register a dependency + // on the crate metadata. The exception is `crate_hash` itself, which obviously + // doesn't need to do this (and can't, as it would cause a query cycle). + use rustc_middle::dep_graph::DepKind; + if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() { + $tcx.ensure().crate_hash($def_id.krate); + } + + let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate); + + $compute + } + }; +} + macro_rules! provide { (<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident, - $($name:ident => $compute:block)*) => { + $($name:ident => { $($compute:tt)* })*) => { pub fn provide_extern(providers: &mut ExternProviders) { - $(fn $name<$lt>( - $tcx: TyCtxt<$lt>, - def_id_arg: ty::query::query_keys::$name<$lt>, - ) -> ty::query::query_values::$name<$lt> { - let _prof_timer = - $tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name))); - - #[allow(unused_variables)] - let ($def_id, $other) = def_id_arg.into_args(); - assert!(!$def_id.is_local()); - - // External query providers call `crate_hash` in order to register a dependency - // on the crate metadata. The exception is `crate_hash` itself, which obviously - // doesn't need to do this (and can't, as it would cause a query cycle). - use rustc_middle::dep_graph::DepKind; - if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() { - $tcx.ensure().crate_hash($def_id.krate); - } - - let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate); - - $compute + $(provide_one! { + <$lt> $tcx, $def_id, $other, $cdata, $name => { $($compute)* } })* *providers = ExternProviders { @@ -90,50 +109,50 @@ impl<'tcx> IntoArgs for ty::InstanceDef<'tcx> { } provide! { <'tcx> tcx, def_id, other, cdata, - type_of => { cdata.get_type(def_id.index, tcx) } - generics_of => { cdata.get_generics(def_id.index, tcx.sess) } - explicit_predicates_of => { cdata.get_explicit_predicates(def_id.index, tcx) } - inferred_outlives_of => { cdata.get_inferred_outlives(def_id.index, tcx) } - super_predicates_of => { cdata.get_super_predicates(def_id.index, tcx) } - explicit_item_bounds => { cdata.get_explicit_item_bounds(def_id.index, tcx) } + explicit_item_bounds => { table } + explicit_predicates_of => { table } + generics_of => { table } + inferred_outlives_of => { table } + super_predicates_of => { table } + type_of => { table } + variances_of => { table } + fn_sig => { table } + impl_trait_ref => { table } + const_param_default => { table } + thir_abstract_const => { table } + optimized_mir => { table } + mir_for_ctfe => { table } + promoted_mir => { table } + def_span => { table } + def_ident_span => { table } + lookup_stability => { table } + lookup_const_stability => { table } + lookup_deprecation_entry => { table } + visibility => { table } + unused_generic_params => { table } + opt_def_kind => { table } + trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } adt_destructor => { let _ = cdata; tcx.calculate_dtor(def_id, |_,_| Ok(())) } - variances_of => { tcx.arena.alloc_from_iter(cdata.get_item_variances(def_id.index)) } associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) } associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) } - impl_trait_ref => { cdata.get_impl_trait(def_id.index, tcx) } impl_polarity => { cdata.get_impl_polarity(def_id.index) } coerce_unsized_info => { cdata.get_coerce_unsized_info(def_id.index).unwrap_or_else(|| { bug!("coerce_unsized_info: `{:?}` is missing its info", def_id); }) } - optimized_mir => { tcx.arena.alloc(cdata.get_optimized_mir(tcx, def_id.index)) } - mir_for_ctfe => { tcx.arena.alloc(cdata.get_mir_for_ctfe(tcx, def_id.index)) } - promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) } - thir_abstract_const => { cdata.get_thir_abstract_const(tcx, def_id.index) } - unused_generic_params => { cdata.get_unused_generic_params(def_id.index) } - const_param_default => { cdata.get_const_param_default(tcx, def_id.index) } mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } - fn_sig => { cdata.fn_sig(def_id.index, tcx) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } asyncness => { cdata.asyncness(def_id.index) } is_foreign_item => { cdata.is_foreign_item(def_id.index) } static_mutability => { cdata.static_mutability(def_id.index) } generator_kind => { cdata.generator_kind(def_id.index) } - opt_def_kind => { Some(cdata.def_kind(def_id.index)) } - def_span => { cdata.get_span(def_id.index, &tcx.sess) } - def_ident_span => { cdata.opt_item_ident(def_id.index, &tcx.sess).map(|ident| ident.span) } - lookup_stability => { cdata.get_stability(def_id.index) } - lookup_const_stability => { cdata.get_const_stability(def_id.index) } - lookup_deprecation_entry => { - cdata.get_deprecation(def_id.index).map(DeprecationEntry::external) - } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) } rendered_const => { cdata.get_rendered_const(def_id.index) } @@ -185,7 +204,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) } implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) } - visibility => { cdata.get_visibility(def_id.index) } dep_kind => { let r = *cdata.dep_kind.lock(); r diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3c4b1899343f4..c0bf2b60b9c8b 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -983,13 +983,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_id = local_id.to_def_id(); let def_kind = tcx.opt_def_kind(local_id); let Some(def_kind) = def_kind else { continue }; - record!(self.tables.def_kind[def_id] <- match def_kind { + record!(self.tables.opt_def_kind[def_id] <- match def_kind { // Replace Ctor by the enclosing object to avoid leaking details in children crates. DefKind::Ctor(CtorOf::Struct, _) => DefKind::Struct, DefKind::Ctor(CtorOf::Variant, _) => DefKind::Variant, def_kind => def_kind, }); - record!(self.tables.span[def_id] <- tcx.def_span(def_id)); + record!(self.tables.def_span[def_id] <- tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- tcx.get_attrs(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); if should_encode_visibility(def_kind) { @@ -1002,19 +1002,19 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } if should_encode_variances(def_kind) { let v = self.tcx.variances_of(def_id); - record!(self.tables.variances[def_id] <- v); + record!(self.tables.variances_of[def_id] <- v); } if should_encode_generics(def_kind) { let g = tcx.generics_of(def_id); - record!(self.tables.generics[def_id] <- g); - record!(self.tables.explicit_predicates[def_id] <- self.tcx.explicit_predicates_of(def_id)); + record!(self.tables.generics_of[def_id] <- g); + record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id)); let inferred_outlives = self.tcx.inferred_outlives_of(def_id); if !inferred_outlives.is_empty() { - record!(self.tables.inferred_outlives[def_id] <- inferred_outlives); + record!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); } } if let DefKind::Trait | DefKind::TraitAlias = def_kind { - record!(self.tables.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id)); + record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id)); } } let inherent_impls = tcx.crate_inherent_impls(()); @@ -1031,7 +1031,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_item_type(&mut self, def_id: DefId) { debug!("EncodeContext::encode_item_type({:?})", def_id); - record!(self.tables.ty[def_id] <- self.tcx.type_of(def_id)); + record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); } fn encode_enum_variant_info(&mut self, def: &ty::AdtDef, index: VariantIdx) { @@ -1332,7 +1332,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EntryBuilder::encode_mir({:?})", def_id); if encode_opt { - record!(self.tables.mir[def_id.to_def_id()] <- self.tcx.optimized_mir(def_id)); + record!(self.tables.optimized_mir[def_id.to_def_id()] <- self.tcx.optimized_mir(def_id)); } if encode_const { record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- self.tcx.mir_for_ctfe(def_id)); @@ -1340,7 +1340,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir` let abstract_const = self.tcx.thir_abstract_const(def_id); if let Ok(Some(abstract_const)) = abstract_const { - record!(self.tables.thir_abstract_consts[def_id.to_def_id()] <- abstract_const); + record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); } } record!(self.tables.promoted_mir[def_id.to_def_id()] <- self.tcx.promoted_mir(def_id)); @@ -1361,7 +1361,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // the stability attributes are even enabled before using their queries. if self.feat.staged_api || self.tcx.sess.opts.debugging_opts.force_unstable_if_unmarked { if let Some(stab) = self.tcx.lookup_stability(def_id) { - record!(self.tables.stability[def_id] <- stab) + record!(self.tables.lookup_stability[def_id] <- stab) } } } @@ -1373,7 +1373,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // the stability attributes are even enabled before using their queries. if self.feat.staged_api || self.tcx.sess.opts.debugging_opts.force_unstable_if_unmarked { if let Some(stab) = self.tcx.lookup_const_stability(def_id) { - record!(self.tables.const_stability[def_id] <- stab) + record!(self.tables.lookup_const_stability[def_id] <- stab) } } } @@ -1381,7 +1381,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_deprecation(&mut self, def_id: DefId) { debug!("EncodeContext::encode_deprecation({:?})", def_id); if let Some(depr) = self.tcx.lookup_deprecation(def_id) { - record!(self.tables.deprecation[def_id] <- depr); + record!(self.tables.lookup_deprecation_entry[def_id] <- depr); } } @@ -1670,12 +1670,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.proc_macro_quoted_spans.set(i, span); } - record!(self.tables.def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod); - record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); + record!(self.tables.opt_def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod); + record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); record!(self.tables.attributes[LOCAL_CRATE.as_def_id()] <- tcx.get_attrs(LOCAL_CRATE.as_def_id())); record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id())); if let Some(stability) = stability { - record!(self.tables.stability[LOCAL_CRATE.as_def_id()] <- stability); + record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); } self.encode_deprecation(LOCAL_CRATE.as_def_id()); @@ -1711,15 +1711,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { def_key.disambiguated_data.data = DefPathData::MacroNs(name); let def_id = id.to_def_id(); - record!(self.tables.def_kind[def_id] <- DefKind::Macro(macro_kind)); + record!(self.tables.opt_def_kind[def_id] <- DefKind::Macro(macro_kind)); record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind)); record!(self.tables.attributes[def_id] <- attrs); record!(self.tables.def_keys[def_id] <- def_key); - record!(self.tables.ident_span[def_id] <- span); - record!(self.tables.span[def_id] <- span); + record!(self.tables.def_ident_span[def_id] <- span); + record!(self.tables.def_span[def_id] <- span); record!(self.tables.visibility[def_id] <- ty::Visibility::Public); if let Some(stability) = stability { - record!(self.tables.stability[def_id] <- stability); + record!(self.tables.lookup_stability[def_id] <- stability); } } @@ -1972,7 +1972,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_id = def_id.to_def_id(); self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true); if default.is_some() { - record!(self.tables.const_defaults[def_id] <- self.tcx.const_param_default(def_id)) + record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id)) } } } @@ -1986,7 +1986,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_ident_span(&mut self, def_id: DefId, ident: Ident) { - record!(self.tables.ident_span[def_id] <- ident.span); + record!(self.tables.def_ident_span[def_id] <- ident.span); } /// In some cases, along with the item itself, we also diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 8424a31d59fce..6dbab8b0a2783 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -275,35 +275,37 @@ macro_rules! define_tables { } define_tables! { - def_kind: Table>, kind: Table>, - visibility: Table>, - span: Table>, - ident_span: Table>, attributes: Table>, children: Table>, - stability: Table>, - const_stability: Table>, - deprecation: Table>, - ty: Table)>, + + opt_def_kind: Table>, + visibility: Table>, + def_span: Table>, + def_ident_span: Table>, + lookup_stability: Table>, + lookup_const_stability: Table>, + lookup_deprecation_entry: Table>, + // As an optimization, a missing entry indicates an empty `&[]`. + explicit_item_bounds: Table, Span)])>, + explicit_predicates_of: Table)>, + generics_of: Table>, + // As an optimization, a missing entry indicates an empty `&[]`. + inferred_outlives_of: Table, Span)])>, + super_predicates_of: Table)>, + type_of: Table)>, + variances_of: Table>, fn_sig: Table)>, impl_trait_ref: Table)>, + const_param_default: Table>>, + optimized_mir: Table)>, + mir_for_ctfe: Table)>, + promoted_mir: Table>)>, + thir_abstract_const: Table])>, + trait_item_def_id: Table>, inherent_impls: Table>, - variances: Table>, - generics: Table>, - explicit_predicates: Table)>, expn_that_defined: Table>, - // As an optimization, a missing entry indicates an empty `&[]`. - inferred_outlives: Table, Span)])>, - super_predicates: Table)>, - // As an optimization, a missing entry indicates an empty `&[]`. - explicit_item_bounds: Table, Span)])>, - mir: Table)>, - mir_for_ctfe: Table)>, - promoted_mir: Table>)>, - thir_abstract_consts: Table])>, - const_defaults: Table>>, unused_generic_params: Table>>, // `def_keys` and `def_path_hashes` represent a lazy version of a // `DefPathTable`. This allows us to avoid deserializing an entire From c7c306f94a426c945e870753a8ea6510ab43e9c2 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 17 Feb 2022 21:46:42 +0100 Subject: [PATCH 3/9] Do not decode span when we only need the name. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 34 ++++++++++--------- .../src/rmeta/decoder/cstore_impl.rs | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index cf32f1dd6abb6..e77b3ee1d1e93 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -808,8 +808,16 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { &self.raw_proc_macros.unwrap()[pos] } + fn opt_item_name(self, item_index: DefIndex) -> Option { + self.def_key(item_index).disambiguated_data.data.get_opt_name() + } + + fn item_name(self, item_index: DefIndex) -> Symbol { + self.opt_item_name(item_index).expect("no encoded ident for item") + } + fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option { - let name = self.def_key(item_index).disambiguated_data.data.get_opt_name()?; + let name = self.opt_item_name(item_index)?; let span = match self.root.tables.def_ident_span.get(self, item_index) { Some(lazy_span) => lazy_span.decode((self, sess)), None => { @@ -927,13 +935,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn get_variant( - self, - kind: &EntryKind, - index: DefIndex, - parent_did: DefId, - sess: &Session, - ) -> ty::VariantDef { + fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef { let data = match kind { EntryKind::Variant(data) | EntryKind::Struct(data, _) | EntryKind::Union(data, _) => { data.decode(self) @@ -953,7 +955,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { let ctor_did = data.ctor.map(|index| self.local_def_id(index)); ty::VariantDef::new( - self.item_ident(index, sess).name, + self.item_name(index), variant_did, ctor_did, data.discr, @@ -965,7 +967,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .decode(self) .map(|index| ty::FieldDef { did: self.local_def_id(index), - name: self.item_ident(index, sess).name, + name: self.item_name(index), vis: self.get_visibility(index), }) .collect(), @@ -995,10 +997,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .get(self, item_id) .unwrap_or_else(Lazy::empty) .decode(self) - .map(|index| self.get_variant(&self.kind(index), index, did, tcx.sess)) + .map(|index| self.get_variant(&self.kind(index), index, did)) .collect() } else { - std::iter::once(self.get_variant(&kind, item_id, did, tcx.sess)).collect() + std::iter::once(self.get_variant(&kind, item_id, did)).collect() }; tcx.alloc_adt_def(did, adt_kind, variants, repr) @@ -1228,10 +1230,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn get_associated_item(self, id: DefIndex, sess: &Session) -> ty::AssocItem { + fn get_associated_item(self, id: DefIndex) -> ty::AssocItem { let def_key = self.def_key(id); let parent = self.local_def_id(def_key.parent.unwrap()); - let ident = self.item_ident(id, sess); + let name = self.item_name(id); let (kind, container, has_self) = match self.kind(id) { EntryKind::AssocConst(container, _, _) => (ty::AssocKind::Const, container, false), @@ -1244,7 +1246,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { }; ty::AssocItem { - name: ident.name, + name, kind, vis: self.get_visibility(id), defaultness: container.defaultness(), @@ -1301,7 +1303,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .get(self, id) .unwrap_or_else(Lazy::empty) .decode(self) - .map(move |index| respan(self.get_span(index, sess), self.item_ident(index, sess).name)) + .map(move |index| respan(self.get_span(index, sess), self.item_name(index))) } fn get_struct_field_visibilities(self, id: DefIndex) -> impl Iterator + 'a { diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index ef840181b848d..8427961165dff 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -139,7 +139,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, tcx.calculate_dtor(def_id, |_,_| Ok(())) } associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) } - associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) } + associated_item => { cdata.get_associated_item(def_id.index) } impl_polarity => { cdata.get_impl_polarity(def_id.index) } coerce_unsized_info => { cdata.get_coerce_unsized_info(def_id.index).unwrap_or_else(|| { From 0b7ee3aca28e4036bb0045b14d8b807a7400bd28 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 17 Feb 2022 22:00:03 +0100 Subject: [PATCH 4/9] Drop ImplData. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 27 ----------- .../src/rmeta/decoder/cstore_impl.rs | 14 ++---- compiler/rustc_metadata/src/rmeta/encoder.rs | 46 ++++++++----------- compiler/rustc_metadata/src/rmeta/mod.rs | 20 +++----- 4 files changed, 31 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index e77b3ee1d1e93..4036cb9ae95bf 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1014,37 +1014,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { self.root.tables.visibility.get(self, id).unwrap().decode(self) } - fn get_impl_data(self, id: DefIndex) -> ImplData { - match self.kind(id) { - EntryKind::Impl(data) => data.decode(self), - _ => bug!(), - } - } - - fn get_parent_impl(self, id: DefIndex) -> Option { - self.get_impl_data(id).parent_impl - } - - fn get_impl_polarity(self, id: DefIndex) -> ty::ImplPolarity { - self.get_impl_data(id).polarity - } - - fn get_impl_defaultness(self, id: DefIndex) -> hir::Defaultness { - self.get_impl_data(id).defaultness - } - - fn get_impl_constness(self, id: DefIndex) -> hir::Constness { - self.get_impl_data(id).constness - } - fn get_trait_item_def_id(self, id: DefIndex) -> Option { self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode(self)) } - fn get_coerce_unsized_info(self, id: DefIndex) -> Option { - self.get_impl_data(id).coerce_unsized_info - } - fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId { self.root.tables.expn_that_defined.get(self, id).unwrap().decode((self, sess)) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 8427961165dff..430720ac2dba2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -131,6 +131,11 @@ provide! { <'tcx> tcx, def_id, other, cdata, visibility => { table } unused_generic_params => { table } opt_def_kind => { table } + impl_parent => { table } + impl_polarity => { table } + impl_defaultness => { table } + impl_constness => { table } + coerce_unsized_info => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -140,12 +145,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, } associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) } associated_item => { cdata.get_associated_item(def_id.index) } - impl_polarity => { cdata.get_impl_polarity(def_id.index) } - coerce_unsized_info => { - cdata.get_coerce_unsized_info(def_id.index).unwrap_or_else(|| { - bug!("coerce_unsized_info: `{:?}` is missing its info", def_id); - }) - } mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } @@ -156,7 +155,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) } rendered_const => { cdata.get_rendered_const(def_id.index) } - impl_parent => { cdata.get_parent_impl(def_id.index) } trait_of_item => { cdata.get_trait_of_item(def_id.index) } is_mir_available => { cdata.is_item_mir_available(def_id.index) } is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) } @@ -176,8 +174,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, } is_no_builtins => { cdata.root.no_builtins } symbol_mangling_version => { cdata.root.symbol_mangling_version } - impl_defaultness => { cdata.get_impl_defaultness(def_id.index) } - impl_constness => { cdata.get_impl_constness(def_id.index) } reachable_non_generics => { let reachable_non_generics = tcx .exported_symbols(cdata.cnum) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index c0bf2b60b9c8b..172ba90453693 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1468,39 +1468,31 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { ) } hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { + record!(self.tables.impl_defaultness[def_id] <- defaultness); + record!(self.tables.impl_constness[def_id] <- constness); + let trait_ref = self.tcx.impl_trait_ref(def_id); - let polarity = self.tcx.impl_polarity(def_id); - let parent = if let Some(trait_ref) = trait_ref { + if let Some(trait_ref) = trait_ref { let trait_def = self.tcx.trait_def(trait_ref.def_id); - trait_def.ancestors(self.tcx, def_id).ok().and_then(|mut an| { - an.nth(1).and_then(|node| match node { - specialization_graph::Node::Impl(parent) => Some(parent), - _ => None, - }) - }) - } else { - None - }; + if let Some(mut an) = trait_def.ancestors(self.tcx, def_id).ok() { + if let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) { + record!(self.tables.impl_parent[def_id] <- parent); + } + } - // if this is an impl of `CoerceUnsized`, create its - // "unsized info", else just store None - let coerce_unsized_info = trait_ref.and_then(|t| { - if Some(t.def_id) == self.tcx.lang_items().coerce_unsized_trait() { - Some(self.tcx.at(item.span).coerce_unsized_info(def_id)) - } else { - None + // if this is an impl of `CoerceUnsized`, create its + // "unsized info", else just store None + if Some(trait_ref.def_id) == self.tcx.lang_items().coerce_unsized_trait() { + let coerce_unsized_info = + self.tcx.at(item.span).coerce_unsized_info(def_id); + record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info); } - }); + } - let data = ImplData { - polarity, - defaultness, - constness, - parent_impl: parent, - coerce_unsized_info, - }; + let polarity = self.tcx.impl_polarity(def_id); + record!(self.tables.impl_polarity[def_id] <- polarity); - EntryKind::Impl(self.lazy(data)) + EntryKind::Impl } hir::ItemKind::Trait(..) => { let trait_def = self.tcx.trait_def(def_id); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 6dbab8b0a2783..924f194ce85c2 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -302,6 +302,12 @@ define_tables! { mir_for_ctfe: Table)>, promoted_mir: Table>)>, thir_abstract_const: Table])>, + impl_parent: Table, + impl_polarity: Table, + impl_constness: Table, + impl_defaultness: Table, + // FIXME(eddyb) perhaps compute this on the fly if cheap enough? + coerce_unsized_info: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -344,7 +350,7 @@ enum EntryKind { Closure, Generator(hir::GeneratorKind), Trait(Lazy), - Impl(Lazy), + Impl, AssocFn(Lazy), AssocType(AssocContainer), AssocConst(AssocContainer, mir::ConstQualifs, Lazy), @@ -383,18 +389,6 @@ struct TraitData { must_implement_one_of: Option>, } -#[derive(TyEncodable, TyDecodable)] -struct ImplData { - polarity: ty::ImplPolarity, - constness: hir::Constness, - defaultness: hir::Defaultness, - parent_impl: Option, - - /// This is `Some` only for impls of `CoerceUnsized`. - // FIXME(eddyb) perhaps compute this on the fly if cheap enough? - coerce_unsized_info: Option, -} - /// Describes whether the container of an associated item /// is a trait or an impl and whether, in a trait, it has /// a default, or an in impl, whether it's marked "default". From f8fd9733b600e5175fc6810b30f2e0e14e0aaf14 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 18 Feb 2022 18:53:47 +0100 Subject: [PATCH 5/9] Add mir_const_qualifs table. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 24 ++++--------------- .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 10 ++++---- compiler/rustc_metadata/src/rmeta/mod.rs | 7 +++--- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4036cb9ae95bf..5e298e94fcc86 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -24,7 +24,6 @@ use rustc_middle::arena::ArenaAllocatable; use rustc_middle::metadata::ModChild; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::middle::stability::DeprecationEntry; -use rustc_middle::mir; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::thir; use rustc_middle::ty::codec::TyDecoder; @@ -1171,21 +1170,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn mir_const_qualif(self, id: DefIndex) -> mir::ConstQualifs { - match self.kind(id) { - EntryKind::AnonConst(qualif, _) - | EntryKind::Const(qualif, _) - | EntryKind::AssocConst( - AssocContainer::ImplDefault - | AssocContainer::ImplFinal - | AssocContainer::TraitWithDefault, - qualif, - _, - ) => qualif, - _ => bug!("mir_const_qualif: unexpected kind"), - } - } - fn get_fn_has_self_parameter(self, id: DefIndex) -> bool { match self.kind(id) { EntryKind::AssocFn(data) => data.decode(self).has_self, @@ -1209,7 +1193,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { let name = self.item_name(id); let (kind, container, has_self) = match self.kind(id) { - EntryKind::AssocConst(container, _, _) => (ty::AssocKind::Const, container, false), + EntryKind::AssocConst(container, _) => (ty::AssocKind::Const, container, false), EntryKind::AssocFn(data) => { let data = data.decode(self); (ty::AssocKind::Fn, data.container, data.has_self) @@ -1429,9 +1413,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { fn get_rendered_const(self, id: DefIndex) -> String { match self.kind(id) { - EntryKind::AnonConst(_, data) - | EntryKind::Const(_, data) - | EntryKind::AssocConst(_, _, data) => data.decode(self).0, + EntryKind::AnonConst(data) + | EntryKind::Const(data) + | EntryKind::AssocConst(_, data) => data.decode(self).0, _ => bug!(), } } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 430720ac2dba2..02652f8431888 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -136,6 +136,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, impl_defaultness => { table } impl_constness => { table } coerce_unsized_info => { table } + mir_const_qualif => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -145,7 +146,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, } associated_item_def_ids => { cdata.get_associated_item_def_ids(tcx, def_id.index) } associated_item => { cdata.get_associated_item(def_id.index) } - mir_const_qualif => { cdata.mir_const_qualif(def_id.index) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } asyncness => { cdata.asyncness(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 172ba90453693..cff1e17a382a2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1192,9 +1192,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, - Default::default(), rendered_const, )); + record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default()); } ty::AssocKind::Fn => { let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind { @@ -1259,9 +1259,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, - qualifs, self.encode_rendered_const_for_body(body_id)) ); + record!(self.tables.mir_const_qualif[def_id] <- qualifs); } else { bug!() } @@ -1407,7 +1407,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic, hir::ItemKind::Const(_, body_id) => { let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); - EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id)) + record!(self.tables.mir_const_qualif[def_id] <- qualifs); + EntryKind::Const(self.encode_rendered_const_for_body(body_id)) } hir::ItemKind::Fn(ref sig, .., body) => { let data = FnData { @@ -1603,7 +1604,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(const_data)); + record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); self.encode_item_type(def_id.to_def_id()); } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 924f194ce85c2..c826800031e64 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -308,6 +308,7 @@ define_tables! { impl_defaultness: Table, // FIXME(eddyb) perhaps compute this on the fly if cheap enough? coerce_unsized_info: Table, + mir_const_qualif: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -324,8 +325,8 @@ define_tables! { #[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)] enum EntryKind { - AnonConst(mir::ConstQualifs, Lazy), - Const(mir::ConstQualifs, Lazy), + AnonConst(Lazy), + Const(Lazy), ImmStatic, MutStatic, ForeignImmStatic, @@ -353,7 +354,7 @@ enum EntryKind { Impl, AssocFn(Lazy), AssocType(AssocContainer), - AssocConst(AssocContainer, mir::ConstQualifs, Lazy), + AssocConst(AssocContainer, Lazy), TraitAlias, } From 7bacdb760f18265b3a5d05406bf2cabb1d33bb5a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 18 Feb 2022 19:09:02 +0100 Subject: [PATCH 6/9] Add rendered_const table. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 11 +------ .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 29 +++++++++---------- compiler/rustc_metadata/src/rmeta/mod.rs | 12 +++----- 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 5e298e94fcc86..7071a0ee22a79 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1193,7 +1193,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { let name = self.item_name(id); let (kind, container, has_self) = match self.kind(id) { - EntryKind::AssocConst(container, _) => (ty::AssocKind::Const, container, false), + EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false), EntryKind::AssocFn(data) => { let data = data.decode(self); (ty::AssocKind::Fn, data.container, data.has_self) @@ -1411,15 +1411,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx))) } - fn get_rendered_const(self, id: DefIndex) -> String { - match self.kind(id) { - EntryKind::AnonConst(data) - | EntryKind::Const(data) - | EntryKind::AssocConst(_, data) => data.decode(self).0, - _ => bug!(), - } - } - fn get_macro(self, id: DefIndex, sess: &Session) -> MacroDef { match self.kind(id) { EntryKind::MacroDef(macro_def) => macro_def.decode((self, sess)), diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 02652f8431888..0a065dc191098 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, impl_constness => { table } coerce_unsized_info => { table } mir_const_qualif => { table } + rendered_const => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -154,7 +155,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, generator_kind => { cdata.generator_kind(def_id.index) } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) } - rendered_const => { cdata.get_rendered_const(def_id.index) } trait_of_item => { cdata.get_trait_of_item(def_id.index) } is_mir_available => { cdata.is_item_mir_available(def_id.index) } is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index cff1e17a382a2..fcb2f3acdd62f 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1188,13 +1188,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { &(&self.tcx.hir() as &dyn intravisit::Map<'_>), |s| s.print_trait_item(ast_item), ); - let rendered_const = self.lazy(RenderedConst(rendered)); - record!(self.tables.kind[def_id] <- EntryKind::AssocConst( - container, - rendered_const, - )); + record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container)); record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default()); + record!(self.tables.rendered_const[def_id] <- rendered); } ty::AssocKind::Fn => { let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind { @@ -1256,12 +1253,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { ty::AssocKind::Const => { if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); + let const_data = self.encode_rendered_const_for_body(body_id); - record!(self.tables.kind[def_id] <- EntryKind::AssocConst( - container, - self.encode_rendered_const_for_body(body_id)) - ); + record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container)); record!(self.tables.mir_const_qualif[def_id] <- qualifs); + record!(self.tables.rendered_const[def_id] <- const_data); } else { bug!() } @@ -1385,14 +1381,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy { + fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> String { let hir = self.tcx.hir(); let body = hir.body(body_id); - let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| { + rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| { s.print_expr(&body.value) - }); - let rendered_const = &RenderedConst(rendered); - self.lazy(rendered_const) + }) } fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) { @@ -1407,8 +1401,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic, hir::ItemKind::Const(_, body_id) => { let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); + let const_data = self.encode_rendered_const_for_body(body_id); record!(self.tables.mir_const_qualif[def_id] <- qualifs); - EntryKind::Const(self.encode_rendered_const_for_body(body_id)) + record!(self.tables.rendered_const[def_id] <- const_data); + EntryKind::Const } hir::ItemKind::Fn(ref sig, .., body) => { let data = FnData { @@ -1604,8 +1600,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(const_data)); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst); record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); + record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); self.encode_item_type(def_id.to_def_id()); } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index c826800031e64..c0cfcf3f702f4 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -309,6 +309,7 @@ define_tables! { // FIXME(eddyb) perhaps compute this on the fly if cheap enough? coerce_unsized_info: Table, mir_const_qualif: Table, + rendered_const: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -325,8 +326,8 @@ define_tables! { #[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)] enum EntryKind { - AnonConst(Lazy), - Const(Lazy), + AnonConst, + Const, ImmStatic, MutStatic, ForeignImmStatic, @@ -354,15 +355,10 @@ enum EntryKind { Impl, AssocFn(Lazy), AssocType(AssocContainer), - AssocConst(AssocContainer, Lazy), + AssocConst(AssocContainer), TraitAlias, } -/// Contains a constant which has been rendered to a String. -/// Used by rustdoc. -#[derive(Encodable, Decodable)] -struct RenderedConst(String); - #[derive(MetadataEncodable, MetadataDecodable)] struct FnData { asyncness: hir::IsAsync, From 6cc96a45acfc2bf0af1942439c00adebf64c0ac5 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 18 Feb 2022 19:15:48 +0100 Subject: [PATCH 7/9] Add asyncness table. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 --------- .../rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 13 +++++-------- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 7071a0ee22a79..dc4909bbf5c8c 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1431,15 +1431,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { constness == hir::Constness::Const } - fn asyncness(self, id: DefIndex) -> hir::IsAsync { - match self.kind(id) { - EntryKind::Fn(data) => data.decode(self).asyncness, - EntryKind::AssocFn(data) => data.decode(self).fn_data.asyncness, - EntryKind::ForeignFn(data) => data.decode(self).asyncness, - _ => bug!("asyncness: expected function kind"), - } - } - fn is_foreign_item(self, id: DefIndex) -> bool { match self.kind(id) { EntryKind::ForeignImmStatic | EntryKind::ForeignMutStatic | EntryKind::ForeignFn(_) => { diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 0a065dc191098..240792a939b26 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -138,6 +138,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, coerce_unsized_info => { table } mir_const_qualif => { table } rendered_const => { table } + asyncness => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -149,7 +150,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, associated_item => { cdata.get_associated_item(def_id.index) } inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } - asyncness => { cdata.asyncness(def_id.index) } is_foreign_item => { cdata.is_foreign_item(def_id.index) } static_mutability => { cdata.static_mutability(def_id.index) } generator_kind => { cdata.generator_kind(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index fcb2f3acdd62f..7142242aac7d0 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1199,11 +1199,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::TraitFn::Required(ref names) => self.encode_fn_param_names(names), hir::TraitFn::Provided(body) => self.encode_fn_param_names_for_body(body), }; - FnData { - asyncness: m_sig.header.asyncness, - constness: hir::Constness::NotConst, - param_names, - } + record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness); + FnData { constness: hir::Constness::NotConst, param_names } } else { bug!() }; @@ -1264,8 +1261,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } ty::AssocKind::Fn => { let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind { + record!(self.tables.asyncness[def_id] <- sig.header.asyncness); FnData { - asyncness: sig.header.asyncness, // Can be inside `impl const Trait`, so using sig.header.constness is not reliable constness: if self.tcx.is_const_fn_raw(def_id) { hir::Constness::Const @@ -1407,8 +1404,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { EntryKind::Const } hir::ItemKind::Fn(ref sig, .., body) => { + record!(self.tables.asyncness[def_id] <- sig.header.asyncness); let data = FnData { - asyncness: sig.header.asyncness, constness: sig.header.constness, param_names: self.encode_fn_param_names_for_body(body), }; @@ -1876,8 +1873,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { match nitem.kind { hir::ForeignItemKind::Fn(_, ref names, _) => { + record!(self.tables.asyncness[def_id] <- hir::IsAsync::NotAsync); let data = FnData { - asyncness: hir::IsAsync::NotAsync, constness: if self.tcx.is_const_fn_raw(def_id) { hir::Constness::Const } else { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index c0cfcf3f702f4..d3d3fc61e4cd2 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -310,6 +310,7 @@ define_tables! { coerce_unsized_info: Table, mir_const_qualif: Table, rendered_const: Table, + asyncness: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -361,7 +362,6 @@ enum EntryKind { #[derive(MetadataEncodable, MetadataDecodable)] struct FnData { - asyncness: hir::IsAsync, constness: hir::Constness, param_names: Lazy<[Ident]>, } From 381d32e7d6ac5037be4ce23aca7c7b38404928a8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 18 Feb 2022 19:23:58 +0100 Subject: [PATCH 8/9] Add fn_arg_names table. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 ------ .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 30 ++++++++----------- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index dc4909bbf5c8c..6f9cd871e9175 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1395,15 +1395,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self)) } - fn get_fn_param_names(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Ident] { - let param_names = match self.kind(id) { - EntryKind::Fn(data) | EntryKind::ForeignFn(data) => data.decode(self).param_names, - EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names, - _ => Lazy::empty(), - }; - LazyQueryDecodable::decode_query(Some(param_names), self, tcx, || unreachable!()) - } - fn exported_symbols( self, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 240792a939b26..d53631d49d38d 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -139,6 +139,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, mir_const_qualif => { table } rendered_const => { table } asyncness => { table } + fn_arg_names => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -154,7 +155,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, static_mutability => { cdata.static_mutability(def_id.index) } generator_kind => { cdata.generator_kind(def_id.index) } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } - fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) } trait_of_item => { cdata.get_trait_of_item(def_id.index) } is_mir_available => { cdata.is_item_mir_available(def_id.index) } is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7142242aac7d0..9821a63dfaaca 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1195,12 +1195,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } ty::AssocKind::Fn => { let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind { - let param_names = match *m { - hir::TraitFn::Required(ref names) => self.encode_fn_param_names(names), - hir::TraitFn::Provided(body) => self.encode_fn_param_names_for_body(body), + match *m { + hir::TraitFn::Required(ref names) => { + record!(self.tables.fn_arg_names[def_id] <- *names) + } + hir::TraitFn::Provided(body) => { + record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)) + } }; record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness); - FnData { constness: hir::Constness::NotConst, param_names } + FnData { constness: hir::Constness::NotConst } } else { bug!() }; @@ -1262,6 +1266,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { ty::AssocKind::Fn => { let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind { record!(self.tables.asyncness[def_id] <- sig.header.asyncness); + record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)); FnData { // Can be inside `impl const Trait`, so using sig.header.constness is not reliable constness: if self.tcx.is_const_fn_raw(def_id) { @@ -1269,7 +1274,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } else { hir::Constness::NotConst }, - param_names: self.encode_fn_param_names_for_body(body), } } else { bug!() @@ -1294,14 +1298,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Ident]> { - self.lazy(self.tcx.hir().body_param_names(body_id)) - } - - fn encode_fn_param_names(&mut self, param_names: &[Ident]) -> Lazy<[Ident]> { - self.lazy(param_names.iter()) - } - fn encode_mir(&mut self) { if self.is_proc_macro { return; @@ -1405,10 +1401,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } hir::ItemKind::Fn(ref sig, .., body) => { record!(self.tables.asyncness[def_id] <- sig.header.asyncness); - let data = FnData { - constness: sig.header.constness, - param_names: self.encode_fn_param_names_for_body(body), - }; + record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)); + let data = FnData { constness: sig.header.constness }; EntryKind::Fn(self.lazy(data)) } @@ -1874,13 +1868,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { match nitem.kind { hir::ForeignItemKind::Fn(_, ref names, _) => { record!(self.tables.asyncness[def_id] <- hir::IsAsync::NotAsync); + record!(self.tables.fn_arg_names[def_id] <- *names); let data = FnData { constness: if self.tcx.is_const_fn_raw(def_id) { hir::Constness::Const } else { hir::Constness::NotConst }, - param_names: self.encode_fn_param_names(names), }; record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data))); } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index d3d3fc61e4cd2..b3255f6f570e9 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -311,6 +311,7 @@ define_tables! { mir_const_qualif: Table, rendered_const: Table, asyncness: Table, + fn_arg_names: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -363,7 +364,6 @@ enum EntryKind { #[derive(MetadataEncodable, MetadataDecodable)] struct FnData { constness: hir::Constness, - param_names: Lazy<[Ident]>, } #[derive(TyEncodable, TyDecodable)] From 7afcf9fcd14af52e53e38ffe60b1eac9b3232b21 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 18 Feb 2022 19:34:24 +0100 Subject: [PATCH 9/9] Add generator_kind table. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 7 ------- compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 3 ++- compiler/rustc_metadata/src/rmeta/mod.rs | 3 ++- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 6f9cd871e9175..f43dcea343d64 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1439,13 +1439,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn generator_kind(self, id: DefIndex) -> Option { - match self.kind(id) { - EntryKind::Generator(data) => Some(data), - _ => None, - } - } - #[inline] fn def_key(self, index: DefIndex) -> DefKey { *self diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index d53631d49d38d..192ad9608b87c 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -140,6 +140,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, rendered_const => { table } asyncness => { table } fn_arg_names => { table } + generator_kind => { table } trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) } adt_def => { cdata.get_adt_def(def_id.index, tcx) } @@ -153,7 +154,6 @@ provide! { <'tcx> tcx, def_id, other, cdata, is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) } is_foreign_item => { cdata.is_foreign_item(def_id.index) } static_mutability => { cdata.static_mutability(def_id.index) } - generator_kind => { cdata.generator_kind(def_id.index) } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } trait_of_item => { cdata.get_trait_of_item(def_id.index) } is_mir_available => { cdata.is_item_mir_available(def_id.index) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 9821a63dfaaca..da8995df1ac9e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1569,7 +1569,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { match ty.kind() { ty::Generator(..) => { let data = self.tcx.generator_kind(def_id).unwrap(); - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator(data)); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator); + record!(self.tables.generator_kind[def_id.to_def_id()] <- data); } ty::Closure(..) => { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index b3255f6f570e9..da17d9d4c6706 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -312,6 +312,7 @@ define_tables! { rendered_const: Table, asyncness: Table, fn_arg_names: Table, + generator_kind: Table, trait_item_def_id: Table>, inherent_impls: Table>, @@ -352,7 +353,7 @@ enum EntryKind { MacroDef(Lazy), ProcMacro(MacroKind), Closure, - Generator(hir::GeneratorKind), + Generator, Trait(Lazy), Impl, AssocFn(Lazy),