summary refs log tree commit diff
path: root/compiler/rustc_metadata/src/rmeta/decoder.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-02-24 10:02:26 +0000
committerbors <bors@rust-lang.org>2022-02-24 10:02:26 +0000
commit7ccfe2ff1d59666dc0188dfd5847304fec257565 (patch)
tree48ab85a723d1398aa894f6fe89e88d01986b8f57 /compiler/rustc_metadata/src/rmeta/decoder.rs
parent1204400ab8da9830f6f77a5e40e7ad3ea459676a (diff)
parent7afcf9fcd14af52e53e38ffe60b1eac9b3232b21 (diff)
downloadrust-7ccfe2ff1d59666dc0188dfd5847304fec257565.tar.gz
rust-7ccfe2ff1d59666dc0188dfd5847304fec257565.zip
Auto merge of #94129 - cjgillot:rmeta-table, r=petrochenkov
Back more metadata using per-query tables

r? `@ghost`
Diffstat (limited to 'compiler/rustc_metadata/src/rmeta/decoder.rs')
-rw-r--r--compiler/rustc_metadata/src/rmeta/decoder.rs372
1 files changed, 128 insertions, 244 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs
index b715f6c3f1f..aaa44a68dc0 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,11 @@ 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::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 +278,99 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> 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<Lazy<T>>
+where
+    T: Decodable<DecodeContext<'a, 'tcx>>,
+{
+    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<Lazy<T>>
+where
+    T: Decodable<DecodeContext<'a, 'tcx>>,
+    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<T>> for Option<Lazy<T>>
+where
+    T: Decodable<DecodeContext<'a, 'tcx>>,
+{
+    fn decode_query(
+        self,
+        cdata: CrateMetadataRef<'a>,
+        tcx: TyCtxt<'tcx>,
+        _err: impl FnOnce() -> !,
+    ) -> Option<T> {
+        self.map(|l| l.decode((cdata, tcx)))
+    }
+}
+
+impl<'a, 'tcx, T, E> LazyQueryDecodable<'a, 'tcx, Result<Option<T>, E>> for Option<Lazy<T>>
+where
+    T: Decodable<DecodeContext<'a, 'tcx>>,
+{
+    fn decode_query(
+        self,
+        cdata: CrateMetadataRef<'a>,
+        tcx: TyCtxt<'tcx>,
+        _err: impl FnOnce() -> !,
+    ) -> Result<Option<T>, E> {
+        Ok(self.map(|l| l.decode((cdata, tcx))))
+    }
+}
+
+impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx [T]> for Option<Lazy<[T], usize>>
+where
+    T: Decodable<DecodeContext<'a, 'tcx>> + 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<DeprecationEntry>>
+    for Option<Lazy<attr::Deprecation>>
+{
+    fn decode_query(
+        self,
+        cdata: CrateMetadataRef<'a>,
+        tcx: TyCtxt<'tcx>,
+        _err: impl FnOnce() -> !,
+    ) -> Option<DeprecationEntry> {
+        self.map(|l| l.decode((cdata, tcx))).map(DeprecationEntry::external)
+    }
+}
+
 impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
     #[inline]
     fn tcx(&self) -> TyCtxt<'tcx> {
@@ -714,9 +807,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
         &self.raw_proc_macros.unwrap()[pos]
     }
 
+    fn opt_item_name(self, item_index: DefIndex) -> Option<Symbol> {
+        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<Ident> {
-        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 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 => {
                 // FIXME: this weird case of a name with no span is specific to `extern crate`
@@ -750,20 +851,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))
@@ -831,13 +934,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)
@@ -857,7 +954,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,
@@ -869,7 +966,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(),
@@ -899,133 +996,31 @@ 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)
     }
 
-    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<attr::Stability> {
-        self.root.tables.stability.get(self, id).map(|stab| stab.decode(self))
-    }
-
-    fn get_const_stability(self, id: DefIndex) -> Option<attr::ConstStability> {
-        self.root.tables.const_stability.get(self, id).map(|stab| stab.decode(self))
-    }
-
-    fn get_deprecation(self, id: DefIndex) -> Option<attr::Deprecation> {
-        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 {
         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<DefId> {
-        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<DefId> {
         self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode(self))
     }
 
-    fn get_coerce_unsized_info(self, id: DefIndex) -> Option<ty::adjustment::CoerceUnsizedInfo> {
-        self.get_impl_data(id).coerce_unsized_info
-    }
-
-    fn get_impl_trait(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
-        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<Symbol>)] {
         tcx.arena.alloc_from_iter(self.root.lib_features.decode(self))
@@ -1163,7 +1158,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,75 +1170,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<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, 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<u32> {
-        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<Promoted, Body<'tcx>> {
-        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, _)
-            | 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,
@@ -1261,13 +1187,13 @@ 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),
+            EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
             EntryKind::AssocFn(data) => {
                 let data = data.decode(self);
                 (ty::AssocKind::Fn, data.container, data.has_self)
@@ -1277,7 +1203,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
         };
 
         ty::AssocItem {
-            name: ident.name,
+            name,
             kind,
             vis: self.get_visibility(id),
             defaultness: container.defaultness(),
@@ -1288,10 +1214,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
         }
     }
 
-    fn get_item_variances(self, id: DefIndex) -> impl Iterator<Item = ty::Variance> + '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) => {
@@ -1338,7 +1260,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<Item = Visibility> + 'a {
@@ -1473,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(),
-        };
-        tcx.arena.alloc_from_iter(param_names.decode((self, tcx)))
-    }
-
     fn exported_symbols(
         self,
         tcx: TyCtxt<'tcx>,
@@ -1489,15 +1402,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)),
@@ -1518,15 +1422,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(_) => {
@@ -1544,17 +1439,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
         }
     }
 
-    fn generator_kind(self, id: DefIndex) -> Option<hir::GeneratorKind> {
-        match self.kind(id) {
-            EntryKind::Generator(data) => Some(data),
-            _ => None,
-        }
-    }
-
-    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