Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f07f1bf

Browse files
committed
Encode metadata using queries.
1 parent 227d912 commit f07f1bf

File tree

4 files changed

+222
-243
lines changed

4 files changed

+222
-243
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 111 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_data_structures::fx::FxHashMap;
1111
use rustc_data_structures::svh::Svh;
1212
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
1313
use rustc_data_structures::unhash::UnhashMap;
14-
use rustc_errors::ErrorReported;
1514
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1615
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, ProcMacroDerive};
1716
use rustc_hir as hir;
@@ -21,10 +20,12 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
2120
use rustc_hir::diagnostic_items::DiagnosticItems;
2221
use rustc_hir::lang_items;
2322
use rustc_index::vec::{Idx, IndexVec};
23+
use rustc_middle::arena::ArenaAllocatable;
2424
use rustc_middle::metadata::ModChild;
2525
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
26+
use rustc_middle::middle::stability::DeprecationEntry;
27+
use rustc_middle::mir;
2628
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
27-
use rustc_middle::mir::{self, Body, Promoted};
2829
use rustc_middle::thir;
2930
use rustc_middle::ty::codec::TyDecoder;
3031
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -278,6 +279,99 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> Lazy<[T]> {
278279
}
279280
}
280281

282+
trait LazyQueryDecodable<'a, 'tcx, T> {
283+
fn decode_query(
284+
self,
285+
cdata: CrateMetadataRef<'a>,
286+
tcx: TyCtxt<'tcx>,
287+
err: impl FnOnce() -> !,
288+
) -> T;
289+
}
290+
291+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<Lazy<T>>
292+
where
293+
T: Decodable<DecodeContext<'a, 'tcx>>,
294+
{
295+
fn decode_query(
296+
self,
297+
cdata: CrateMetadataRef<'a>,
298+
tcx: TyCtxt<'tcx>,
299+
err: impl FnOnce() -> !,
300+
) -> T {
301+
if let Some(l) = self { l.decode((cdata, tcx)) } else { err() }
302+
}
303+
}
304+
305+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx T> for Option<Lazy<T>>
306+
where
307+
T: Decodable<DecodeContext<'a, 'tcx>>,
308+
T: ArenaAllocatable<'tcx>,
309+
{
310+
fn decode_query(
311+
self,
312+
cdata: CrateMetadataRef<'a>,
313+
tcx: TyCtxt<'tcx>,
314+
err: impl FnOnce() -> !,
315+
) -> &'tcx T {
316+
if let Some(l) = self { tcx.arena.alloc(l.decode((cdata, tcx))) } else { err() }
317+
}
318+
}
319+
320+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, Option<T>> for Option<Lazy<T>>
321+
where
322+
T: Decodable<DecodeContext<'a, 'tcx>>,
323+
{
324+
fn decode_query(
325+
self,
326+
cdata: CrateMetadataRef<'a>,
327+
tcx: TyCtxt<'tcx>,
328+
_err: impl FnOnce() -> !,
329+
) -> Option<T> {
330+
self.map(|l| l.decode((cdata, tcx)))
331+
}
332+
}
333+
334+
impl<'a, 'tcx, T, E> LazyQueryDecodable<'a, 'tcx, Result<Option<T>, E>> for Option<Lazy<T>>
335+
where
336+
T: Decodable<DecodeContext<'a, 'tcx>>,
337+
{
338+
fn decode_query(
339+
self,
340+
cdata: CrateMetadataRef<'a>,
341+
tcx: TyCtxt<'tcx>,
342+
_err: impl FnOnce() -> !,
343+
) -> Result<Option<T>, E> {
344+
Ok(self.map(|l| l.decode((cdata, tcx))))
345+
}
346+
}
347+
348+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, &'tcx [T]> for Option<Lazy<[T], usize>>
349+
where
350+
T: Decodable<DecodeContext<'a, 'tcx>> + Copy,
351+
{
352+
fn decode_query(
353+
self,
354+
cdata: CrateMetadataRef<'a>,
355+
tcx: TyCtxt<'tcx>,
356+
_err: impl FnOnce() -> !,
357+
) -> &'tcx [T] {
358+
if let Some(l) = self { tcx.arena.alloc_from_iter(l.decode((cdata, tcx))) } else { &[] }
359+
}
360+
}
361+
362+
impl<'a, 'tcx> LazyQueryDecodable<'a, 'tcx, Option<DeprecationEntry>>
363+
for Option<Lazy<attr::Deprecation>>
364+
{
365+
fn decode_query(
366+
self,
367+
cdata: CrateMetadataRef<'a>,
368+
tcx: TyCtxt<'tcx>,
369+
_err: impl FnOnce() -> !,
370+
) -> Option<DeprecationEntry> {
371+
self.map(|l| l.decode((cdata, tcx))).map(DeprecationEntry::external)
372+
}
373+
}
374+
281375
impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
282376
#[inline]
283377
fn tcx(&self) -> TyCtxt<'tcx> {
@@ -716,7 +810,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
716810

717811
fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option<Ident> {
718812
let name = self.def_key(item_index).disambiguated_data.data.get_opt_name()?;
719-
let span = match self.root.tables.ident_span.get(self, item_index) {
813+
let span = match self.root.tables.def_ident_span.get(self, item_index) {
720814
Some(lazy_span) => lazy_span.decode((self, sess)),
721815
None => {
722816
// FIXME: this weird case of a name with no span is specific to `extern crate`
@@ -750,20 +844,22 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
750844
}
751845

752846
fn def_kind(self, item_id: DefIndex) -> DefKind {
753-
self.root.tables.def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(|| {
754-
bug!(
755-
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
756-
item_id,
757-
self.root.name,
758-
self.cnum,
759-
)
760-
})
847+
self.root.tables.opt_def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(
848+
|| {
849+
bug!(
850+
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
851+
item_id,
852+
self.root.name,
853+
self.cnum,
854+
)
855+
},
856+
)
761857
}
762858

763859
fn get_span(self, index: DefIndex, sess: &Session) -> Span {
764860
self.root
765861
.tables
766-
.span
862+
.def_span
767863
.get(self, index)
768864
.unwrap_or_else(|| panic!("Missing span for {:?}", index))
769865
.decode((self, sess))
@@ -908,71 +1004,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
9081004
tcx.alloc_adt_def(did, adt_kind, variants, repr)
9091005
}
9101006

911-
fn get_explicit_predicates(
912-
self,
913-
item_id: DefIndex,
914-
tcx: TyCtxt<'tcx>,
915-
) -> ty::GenericPredicates<'tcx> {
916-
self.root.tables.explicit_predicates.get(self, item_id).unwrap().decode((self, tcx))
917-
}
918-
919-
fn get_inferred_outlives(
920-
self,
921-
item_id: DefIndex,
922-
tcx: TyCtxt<'tcx>,
923-
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
924-
self.root
925-
.tables
926-
.inferred_outlives
927-
.get(self, item_id)
928-
.map(|predicates| tcx.arena.alloc_from_iter(predicates.decode((self, tcx))))
929-
.unwrap_or_default()
930-
}
931-
932-
fn get_super_predicates(
933-
self,
934-
item_id: DefIndex,
935-
tcx: TyCtxt<'tcx>,
936-
) -> ty::GenericPredicates<'tcx> {
937-
self.root.tables.super_predicates.get(self, item_id).unwrap().decode((self, tcx))
938-
}
939-
940-
fn get_explicit_item_bounds(
941-
self,
942-
item_id: DefIndex,
943-
tcx: TyCtxt<'tcx>,
944-
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
945-
self.root
946-
.tables
947-
.explicit_item_bounds
948-
.get(self, item_id)
949-
.map(|bounds| tcx.arena.alloc_from_iter(bounds.decode((self, tcx))))
950-
.unwrap_or_default()
951-
}
952-
9531007
fn get_generics(self, item_id: DefIndex, sess: &Session) -> ty::Generics {
954-
self.root.tables.generics.get(self, item_id).unwrap().decode((self, sess))
955-
}
956-
957-
fn get_type(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
958-
self.root
959-
.tables
960-
.ty
961-
.get(self, id)
962-
.unwrap_or_else(|| panic!("Not a type: {:?}", id))
963-
.decode((self, tcx))
964-
}
965-
966-
fn get_stability(self, id: DefIndex) -> Option<attr::Stability> {
967-
self.root.tables.stability.get(self, id).map(|stab| stab.decode(self))
968-
}
969-
970-
fn get_const_stability(self, id: DefIndex) -> Option<attr::ConstStability> {
971-
self.root.tables.const_stability.get(self, id).map(|stab| stab.decode(self))
972-
}
973-
974-
fn get_deprecation(self, id: DefIndex) -> Option<attr::Deprecation> {
975-
self.root.tables.deprecation.get(self, id).map(|depr| depr.decode(self))
1008+
self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
9761009
}
9771010

9781011
fn get_visibility(self, id: DefIndex) -> ty::Visibility {
@@ -1010,22 +1043,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10101043
self.get_impl_data(id).coerce_unsized_info
10111044
}
10121045

1013-
fn get_impl_trait(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> Option<ty::TraitRef<'tcx>> {
1014-
self.root.tables.impl_trait_ref.get(self, id).map(|tr| tr.decode((self, tcx)))
1015-
}
1016-
10171046
fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId {
10181047
self.root.tables.expn_that_defined.get(self, id).unwrap().decode((self, sess))
10191048
}
10201049

1021-
fn get_const_param_default(
1022-
self,
1023-
tcx: TyCtxt<'tcx>,
1024-
id: DefIndex,
1025-
) -> rustc_middle::ty::Const<'tcx> {
1026-
self.root.tables.const_defaults.get(self, id).unwrap().decode((self, tcx))
1027-
}
1028-
10291050
/// Iterates over all the stability attributes in the given crate.
10301051
fn get_lib_features(self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option<Symbol>)] {
10311052
tcx.arena.alloc_from_iter(self.root.lib_features.decode(self))
@@ -1163,7 +1184,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11631184
}
11641185

11651186
fn is_item_mir_available(self, id: DefIndex) -> bool {
1166-
self.root.tables.mir.get(self, id).is_some()
1187+
self.root.tables.optimized_mir.get(self, id).is_some()
11671188
}
11681189

11691190
fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId {
@@ -1175,60 +1196,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11751196
}
11761197
}
11771198

1178-
fn get_optimized_mir(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
1179-
self.root
1180-
.tables
1181-
.mir
1182-
.get(self, id)
1183-
.unwrap_or_else(|| {
1184-
bug!("get_optimized_mir: missing MIR for `{:?}`", self.local_def_id(id))
1185-
})
1186-
.decode((self, tcx))
1187-
}
1188-
1189-
fn get_mir_for_ctfe(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> Body<'tcx> {
1190-
self.root
1191-
.tables
1192-
.mir_for_ctfe
1193-
.get(self, id)
1194-
.unwrap_or_else(|| {
1195-
bug!("get_mir_for_ctfe: missing MIR for `{:?}`", self.local_def_id(id))
1196-
})
1197-
.decode((self, tcx))
1198-
}
1199-
1200-
fn get_thir_abstract_const(
1201-
self,
1202-
tcx: TyCtxt<'tcx>,
1203-
id: DefIndex,
1204-
) -> Result<Option<&'tcx [thir::abstract_const::Node<'tcx>]>, ErrorReported> {
1205-
self.root
1206-
.tables
1207-
.thir_abstract_consts
1208-
.get(self, id)
1209-
.map_or(Ok(None), |v| Ok(Some(v.decode((self, tcx)))))
1210-
}
1211-
1212-
fn get_unused_generic_params(self, id: DefIndex) -> FiniteBitSet<u32> {
1213-
self.root
1214-
.tables
1215-
.unused_generic_params
1216-
.get(self, id)
1217-
.map(|params| params.decode(self))
1218-
.unwrap_or_default()
1219-
}
1220-
1221-
fn get_promoted_mir(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> IndexVec<Promoted, Body<'tcx>> {
1222-
self.root
1223-
.tables
1224-
.promoted_mir
1225-
.get(self, id)
1226-
.unwrap_or_else(|| {
1227-
bug!("get_promoted_mir: missing MIR for `{:?}`", self.local_def_id(id))
1228-
})
1229-
.decode((self, tcx))
1230-
}
1231-
12321199
fn mir_const_qualif(self, id: DefIndex) -> mir::ConstQualifs {
12331200
match self.kind(id) {
12341201
EntryKind::AnonConst(qualif, _)
@@ -1288,10 +1255,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12881255
}
12891256
}
12901257

1291-
fn get_item_variances(self, id: DefIndex) -> impl Iterator<Item = ty::Variance> + 'a {
1292-
self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self)
1293-
}
1294-
12951258
fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> {
12961259
match self.kind(node_id) {
12971260
EntryKind::Struct(data, _) | EntryKind::Variant(data) => {
@@ -1479,7 +1442,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14791442
EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names,
14801443
_ => Lazy::empty(),
14811444
};
1482-
tcx.arena.alloc_from_iter(param_names.decode((self, tcx)))
1445+
LazyQueryDecodable::decode_query(Some(param_names), self, tcx, || unreachable!())
14831446
}
14841447

14851448
fn exported_symbols(
@@ -1551,10 +1514,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15511514
}
15521515
}
15531516

1554-
fn fn_sig(self, id: DefIndex, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
1555-
self.root.tables.fn_sig.get(self, id).unwrap().decode((self, tcx))
1556-
}
1557-
15581517
#[inline]
15591518
fn def_key(self, index: DefIndex) -> DefKey {
15601519
*self

0 commit comments

Comments
 (0)