diff --git a/compiler/rustc_hir_id/src/definitions.rs b/compiler/rustc_hir_id/src/definitions.rs index 426a4e0109153..28a878cd06d9f 100644 --- a/compiler/rustc_hir_id/src/definitions.rs +++ b/compiler/rustc_hir_id/src/definitions.rs @@ -13,11 +13,12 @@ use rustc_data_structures::stable_hash::StableHasher; use rustc_hashes::Hash64; use rustc_index::IndexVec; use rustc_macros::{BlobDecodable, Decodable, Encodable, extension}; +use rustc_serialize::Encodable; pub use rustc_span::def_id::DefPathHash; use rustc_span::def_id::{ CRATE_DEF_INDEX, CrateNum, DefIndex, LOCAL_CRATE, LocalDefId, LocalDefIdMap, StableCrateId, }; -use rustc_span::{Symbol, kw, sym}; +use rustc_span::{SpanEncoder, Symbol, kw, sym}; use tracing::{debug, instrument}; use crate::def_path_hash_map::DefPathHashMap; @@ -101,7 +102,7 @@ pub struct Definitions { /// A unique identifier that we can use to lookup a definition /// precisely. It combines the index of the definition's parent (if /// any) with a `DisambiguatedDefPathData`. -#[derive(Copy, Clone, PartialEq, Debug, Encodable, BlobDecodable)] +#[derive(Copy, Clone, PartialEq, Debug, BlobDecodable)] pub struct DefKey { /// The parent path. pub parent: Option, @@ -110,6 +111,13 @@ pub struct DefKey { pub disambiguated_data: DisambiguatedDefPathData, } +impl Encodable for DefKey { + default fn encode(&self, e: &mut E) { + self.parent.encode(e); + self.disambiguated_data.encode(e); + } +} + impl DefKey { pub(crate) fn compute_stable_hash(&self, parent: DefPathHash) -> DefPathHash { let mut hasher = StableHasher::new(); diff --git a/compiler/rustc_hir_id/src/lib.rs b/compiler/rustc_hir_id/src/lib.rs index 0e01600f9cb3d..cbdcb6cc30c03 100644 --- a/compiler/rustc_hir_id/src/lib.rs +++ b/compiler/rustc_hir_id/src/lib.rs @@ -2,6 +2,7 @@ //! on all of `rustc_hir` (which is large and depends on other large things like `rustc_target`). #![allow(internal_features)] #![feature(negative_impls)] +#![feature(min_specialization)] #![feature(rustc_attrs)] pub mod def_path_hash_map; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 7dd920c7e39bd..7c8a6d3206ca2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -100,12 +100,12 @@ pub(crate) struct CrateMetadata { /// Trait impl data. /// FIXME: Used only from queries and can use query cache, /// so pre-decoding can probably be avoided. - trait_impls: FxIndexMap<(u32, DefIndex), LazyArray<(DefIndex, Option)>>, + trait_impls: FxIndexMap<(u32, u32), LazyArray<(LocalDefId, Option)>>, /// Inherent impls which do not follow the normal coherence rules. /// /// These can be introduced using either `#![rustc_coherence_is_core]` /// or `#[rustc_allow_incoherent_impl]`. - incoherent_impls: FxIndexMap>, + incoherent_impls: FxIndexMap>, /// Proc macro function pointers for this crate, if it's a proc macro crate. raw_proc_macros: Option<&'static [ProcMacroClient]>, /// Source maps for code from the crate. @@ -441,6 +441,20 @@ impl<'a, 'tcx> Decodable> for ExpnIndex { } } +impl<'a, 'tcx> Decodable> for LocalDefId { + #[inline] + fn decode(d: &mut MetadataDecodeContext<'a, 'tcx>) -> LocalDefId { + LocalDefId { local_def_index: DefIndex::from_u32(d.read_u32()) } + } +} + +impl<'a> Decodable> for LocalDefId { + #[inline] + fn decode(d: &mut BlobDecodeContext<'a>) -> LocalDefId { + LocalDefId { local_def_index: DefIndex::from_u32(d.read_u32()) } + } +} + impl<'a, 'tcx> SpanDecoder for MetadataDecodeContext<'a, 'tcx> { fn decode_attr_id(&mut self) -> rustc_span::AttrId { self.tcx.sess.psess.attr_id_generator.mk_attr_id() @@ -831,7 +845,7 @@ impl MetadataBlob { out, "{} = crate{}", lang_item.name(), - DefPath::make(LOCAL_CRATE, id, |parent| root + DefPath::make(LOCAL_CRATE, id.local_def_index, |parent| root .tables .def_keys .get(self, parent) @@ -886,21 +900,21 @@ impl MetadataBlob { .get_opt_name() .unwrap_or_else(|| Symbol::intern("???")) }; - let visibility = - root.tables.visibility.get(blob, item).unwrap().decode(blob).map_id( - |index| { - format!( - "crate{}", - DefPath::make(LOCAL_CRATE, index, |parent| root - .tables - .def_keys - .get(blob, parent) - .unwrap() - .decode(blob)) - .to_string_no_crate_verbose() - ) - }, - ); + let visibility = root + .tables + .visibility + .get(blob, item) + .unwrap() + .decode(blob) + .map_id(|id| { + format!( + "crate{}", + DefPath::make(LOCAL_CRATE, id.local_def_index, |parent| { + root.tables.def_keys.get(blob, parent).unwrap().decode(blob) + }) + .to_string_no_crate_verbose() + ) + }); write!( out, "{nil: None, @@ -1205,7 +1220,7 @@ impl CrateMetadata { .get(self, id) .unwrap_or_else(|| self.missing("visibility", id)) .decode((self, tcx)) - .map_id(|index| ModId::new_unchecked(self.local_def_id(index))) + .map_id(|id| ModId::new_unchecked(self.local_def_id(id.local_def_index))) } fn get_mut_restriction(&self, tcx: TyCtxt<'_>, id: DefIndex) -> RestrictionKind { @@ -1263,7 +1278,7 @@ impl CrateMetadata { self.root .lang_items .decode((self, tcx)) - .map(move |(def_index, index)| (self.local_def_id(def_index), index)), + .map(move |(id, index)| (self.local_def_id(id.local_def_index), index)), ) } @@ -1272,11 +1287,11 @@ impl CrateMetadata { tcx: TyCtxt<'tcx>, cnum: CrateNum, ) -> &'tcx [StrippedCfgItem] { - let item_names = self - .root - .stripped_cfg_items - .decode((self, tcx)) - .map(|item| item.map_scope_id(|index| DefId { krate: cnum, index })); + let item_names = + self.root.stripped_cfg_items.decode((self, tcx)).map(|item| { + item.map_scope_id(|id| DefId { krate: cnum, index: id.local_def_index }) + }); + tcx.arena.alloc_from_iter(item_names) } @@ -1287,8 +1302,8 @@ impl CrateMetadata { .root .diagnostic_items .decode((self, tcx)) - .map(|(name, def_index)| { - let id = self.local_def_id(def_index); + .map(|(name, id)| { + let id = self.local_def_id(id.local_def_index); id_to_name.insert(id, name); (name, id) }) @@ -1300,8 +1315,8 @@ impl CrateMetadata { fn get_canonical_symbols(&self, tcx: TyCtxt<'_>) -> CanonicalSymbols { let mut canonical_symbols = CanonicalSymbols::new(); - for (name, def_index) in self.root.canonical_symbols.decode((self, tcx)) { - let id = self.local_def_id(def_index); + for (name, id) in self.root.canonical_symbols.decode((self, tcx)) { + let id = self.local_def_id(id.local_def_index); let _ = canonical_symbols.set(name, id); } @@ -1312,8 +1327,8 @@ impl CrateMetadata { fn get_fake_doc_items(&self, tcx: TyCtxt<'_>) -> Vec { let mut fake_doc_items = Vec::new(); - for def_index in self.root.fake_doc_items.decode((self, tcx)) { - let id = self.local_def_id(def_index); + for id in self.root.fake_doc_items.decode((self, tcx)) { + let id = self.local_def_id(id.local_def_index); fake_doc_items.push(id); } @@ -1343,7 +1358,7 @@ impl CrateMetadata { // the view of this crate as a proc macro crate. if id == CRATE_DEF_INDEX { for (child_index, _) in data.macros.decode((self, tcx)) { - yield self.get_mod_child(tcx, child_index); + yield self.get_mod_child(tcx, child_index.local_def_index); } } } else { @@ -1352,7 +1367,7 @@ impl CrateMetadata { let non_reexports = non_reexports.expect("provided `DefIndex` must refer to a module-like item"); for child_index in non_reexports.decode((self, tcx)) { - yield self.get_mod_child(tcx, child_index); + yield self.get_mod_child(tcx, child_index.local_def_index); } let reexports = self.root.tables.module_children_reexports.get(self, id); @@ -1406,7 +1421,7 @@ impl CrateMetadata { .get(self, id) .unwrap_or_else(|| self.missing("associated_item_or_field_def_ids", id)) .decode((self, tcx)) - .map(move |child_index| self.local_def_id(child_index)) + .map(move |id| self.local_def_id(id.local_def_index)) } fn get_associated_item(&self, tcx: TyCtxt<'_>, id: DefIndex) -> ty::AssocItem { @@ -1437,7 +1452,7 @@ impl CrateMetadata { DefKind::Struct | DefKind::Variant => { let vdata = self.root.tables.variant_data.get(self, node_id).unwrap().decode((self, tcx)); - vdata.ctor.map(|(kind, index)| (kind, self.local_def_id(index))) + vdata.ctor.map(|(kind, id)| (kind, self.local_def_id(id.local_def_index))) } _ => None, } @@ -1487,25 +1502,27 @@ impl CrateMetadata { .inherent_impls .get(self, id) .decode((self, tcx)) - .map(|index| self.local_def_id(index)), + .map(|id| self.local_def_id(id.local_def_index)), ) } /// Decodes all traits in the crate (for rustdoc and rustc diagnostics). fn get_traits(&self, tcx: TyCtxt<'_>) -> impl Iterator { - self.root.traits.decode((self, tcx)).map(move |index| self.local_def_id(index)) + self.root.traits.decode((self, tcx)).map(move |id| self.local_def_id(id.local_def_index)) } /// Decodes all trait impls in the crate (for rustdoc). fn get_trait_impls(&self, tcx: TyCtxt<'_>) -> impl Iterator { self.trait_impls.values().flat_map(move |impls| { - impls.decode((self, tcx)).map(move |(impl_index, _)| self.local_def_id(impl_index)) + impls.decode((self, tcx)).map(move |(id, _)| self.local_def_id(id.local_def_index)) }) } fn get_incoherent_impls<'tcx>(&self, tcx: TyCtxt<'tcx>, simp: SimplifiedType) -> &'tcx [DefId] { if let Some(impls) = self.incoherent_impls.get(&simp) { - tcx.arena.alloc_from_iter(impls.decode((self, tcx)).map(|idx| self.local_def_id(idx))) + tcx.arena.alloc_from_iter( + impls.decode((self, tcx)).map(|id| self.local_def_id(id.local_def_index)), + ) } else { &[] } @@ -1523,16 +1540,14 @@ impl CrateMetadata { // Do a reverse lookup beforehand to avoid touching the crate_num // hash map in the loop below. let key = match self.reverse_translate_def_id(trait_def_id) { - Some(def_id) => (def_id.krate.as_u32(), def_id.index), + Some(def_id) => (def_id.krate.as_u32(), def_id.index.as_u32()), None => return &[], }; if let Some(impls) = self.trait_impls.get(&key) { - tcx.arena.alloc_from_iter( - impls - .decode((self, tcx)) - .map(|(idx, simplified_self_ty)| (self.local_def_id(idx), simplified_self_ty)), - ) + tcx.arena.alloc_from_iter(impls.decode((self, tcx)).map(|(id, simplified_self_ty)| { + (self.local_def_id(id.local_def_index), simplified_self_ty) + })) } else { &[] } @@ -1544,7 +1559,9 @@ impl CrateMetadata { fn get_proc_macro_quoted_span(&self, tcx: TyCtxt<'_>, index: usize) -> Span { self.root - .tables + .proc_macro_data + .as_ref() + .unwrap_or_else(|| panic!("missing proc macro data")) .proc_macro_quoted_spans .get(self, index) .unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}")) @@ -1581,7 +1598,10 @@ impl CrateMetadata { } fn get_exportable_items(&self, tcx: TyCtxt<'_>) -> impl Iterator { - self.root.exportable_items.decode((self, tcx)).map(move |index| self.local_def_id(index)) + self.root + .exportable_items + .decode((self, tcx)) + .map(move |id| self.local_def_id(id.local_def_index)) } fn get_stable_order_of_exportable_impls( @@ -1591,7 +1611,7 @@ impl CrateMetadata { self.root .stable_order_of_exportable_impls .decode((self, tcx)) - .map(move |v| (self.local_def_id(v.0), v.1)) + .map(move |v| (self.local_def_id(v.0.local_def_index), v.1)) } fn exported_non_generic_symbols<'tcx>( @@ -2105,8 +2125,10 @@ impl CrateMetadata { ) -> impl Iterator { gen move { if let Some(data) = &self.root.proc_macro_data { - for def_id in - data.macros.decode((self, tcx)).map(move |(index, _)| DefId { index, krate }) + for def_id in data + .macros + .decode((self, tcx)) + .map(move |(id, _)| DefId { krate, index: id.local_def_index }) { yield def_id; } diff --git a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs index 61ec7ad4cf7b3..a784a447c734c 100644 --- a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs +++ b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs @@ -6,8 +6,8 @@ use rustc_hir::definitions::DefPathToIndexMap; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::def_id::{DefIndex, DefPathHash}; -use crate::rmeta::EncodeContext; use crate::rmeta::decoder::BlobDecodeContext; +use crate::rmeta::{EncodeContext, LocalDefId}; pub(crate) enum DefPathHashMapRef<'tcx> { OwnedFromMetadata(odht::HashTable, SortedMap), @@ -40,11 +40,16 @@ impl<'a, 'tcx> Encodable> for DefPathHashMapRef<'tcx> { e.emit_usize(bytes.len()); e.emit_raw_bytes(bytes); - map.after_parallel_alloc + let map = map + .after_parallel_alloc .as_ref() - .expect("must be set before metadata encoding") - .range(..) - .encode(e); + .expect("must be set before metadata encoding"); + + map.len().encode(e); + for (h, i) in map.iter().map(|(h, i)| (h, LocalDefId { local_def_index: *i })) { + h.encode(e); + i.encode(e); + } } DefPathHashMapRef::OwnedFromMetadata(..) => { panic!("DefPathHashMap::OwnedFromMetadata variant only exists for deserialization") diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 40b61a52d2310..6ceddef49fbaf 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -155,13 +155,14 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { self.emit_u32(crate_num.as_u32()); } - fn encode_def_index(&mut self, def_index: DefIndex) { - self.emit_u32(def_index.as_u32()); + fn encode_def_index(&mut self, _: DefIndex) { + panic!("use LocalDefId or DefId or encode DefIndex manually") } fn encode_def_id(&mut self, def_id: DefId) { def_id.krate.encode(self); - def_id.index.encode(self); + + self.emit_u32(def_id.index.as_u32()); } fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) { @@ -380,6 +381,19 @@ impl<'a, 'tcx> Encodable> for [u8] { } } +impl Encodable> for DefKey { + fn encode(&self, e: &mut EncodeContext<'_, '_>) { + self.parent.as_ref().map(|p| LocalDefId { local_def_index: *p }).encode(e); + self.disambiguated_data.encode(e); + } +} + +impl Encodable> for LocalDefId { + fn encode(&self, e: &mut EncodeContext<'_, '_>) { + e.emit_u32(self.local_def_index.as_u32()); + } +} + impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; @@ -404,12 +418,32 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { // Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy($value))`, which would // normally need extra variables to avoid errors about multiple mutable borrows. -macro_rules! record { +// Also those macros greatly simplify refactorings and modifications of writing to tables. +macro_rules! record_some_lazy { ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ { let value = $value; let lazy = $self.lazy(value); - $self.$tables.$table.set_some($def_id.index, lazy); + let index = $def_id.expect_local(); + $self.$tables.$table.set_some(index, lazy); + } + }}; +} + +macro_rules! record_some { + ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ + { + let index = $def_id.expect_local(); + $self.$tables.$table.set_some(index, $value); + } + }}; +} + +macro_rules! record_value { + ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ + { + let index = $def_id.expect_local(); + $self.$tables.$table.set(index, $value); } }}; } @@ -421,7 +455,8 @@ macro_rules! record_array { { let value = $value; let lazy = $self.lazy_array(value); - $self.$tables.$table.set_some($def_id.index, lazy); + let index = $def_id.expect_local(); + $self.$tables.$table.set_some(index, lazy); } }}; } @@ -431,7 +466,8 @@ macro_rules! record_defaulted_array { { let value = $value; let lazy = $self.lazy_array(value); - $self.$tables.$table.set($def_id.index, lazy); + let index = $def_id.expect_local(); + $self.$tables.$table.set(index, lazy); } }}; } @@ -527,18 +563,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { for def_id in std::iter::once(CRATE_DEF_ID) .chain(self.tcx.resolutions(()).proc_macros.iter().copied()) { - let def_key = self.lazy(defs.def_key(def_id)); + let def_key = defs.def_key(def_id); let def_path_hash = defs.def_path_hash(def_id); - self.tables.def_keys.set_some(def_id.local_def_index, def_key); - self.tables - .def_path_hashes - .set(def_id.local_def_index, def_path_hash.local_hash().as_u64()); + let def_id = def_id.to_def_id(); + + record_some_lazy!(self.tables.def_keys[def_id] <- def_key); + record_value!(self.tables.def_path_hashes[def_id] <- def_path_hash.local_hash().as_u64()) } } else { for (def_index, def_key, def_path_hash) in defs.enumerated_keys_and_path_hashes() { - let def_key = self.lazy(def_key); - self.tables.def_keys.set_some(def_index, def_key); - self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64()); + let def_id = LocalDefId { local_def_index: def_index }.to_def_id(); + record_some_lazy!(self.tables.def_keys[def_id] <- def_key); + record_value!(self.tables.def_path_hashes[def_id] <- def_path_hash.local_hash().as_u64()) } } } @@ -1444,7 +1480,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if state.is_doc_hidden { attr_flags |= AttrFlags::IS_DOC_HIDDEN; } - self.tables.attr_flags.set(def_id.local_def_index, attr_flags); + + record_value!(self.tables.attr_flags[def_id.to_def_id()] <- attr_flags) } fn encode_def_ids(&mut self) { @@ -1461,7 +1498,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { for local_id in tcx.iter_local_def_id() { let def_id = local_id.to_def_id(); let def_kind = tcx.def_kind(local_id); - self.tables.def_kind.set_some(def_id.index, def_kind); + record_some!(self.tables.def_kind[def_id] <- def_kind); // The `DefCollector` will sometimes create unnecessary `DefId`s // for trivial const arguments which are directly lowered to @@ -1482,33 +1519,33 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { && let hir::Node::Field(field) = tcx.hir_node_by_def_id(local_id) && let Some(anon) = field.default { - record!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id()); + record_some_lazy!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id()); } if should_encode_span(def_kind) { let def_span = tcx.def_span(local_id); - record!(self.tables.def_span[def_id] <- def_span); + record_some_lazy!(self.tables.def_span[def_id] <- def_span); } if should_encode_attrs(def_kind) { self.encode_attrs(local_id); } if should_encode_expn_that_defined(def_kind) { - record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); + record_some_lazy!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); } if should_encode_span(def_kind) && let Some(ident_span) = tcx.def_ident_span(def_id) { - record!(self.tables.def_ident_span[def_id] <- ident_span); + record_some_lazy!(self.tables.def_ident_span[def_id] <- ident_span); } if def_kind.has_codegen_attrs() { - record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); + record_some_lazy!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); } if should_encode_visibility(def_kind) { let vis = self .tcx .local_visibility(local_id) - .map_id(|mod_id| mod_id.to_local_def_id().local_def_index); - record!(self.tables.visibility[def_id] <- vis); + .map_id(|mod_id| mod_id.to_local_def_id().into()); + record_some_lazy!(self.tables.visibility[def_id] <- vis); } if should_encode_stability(def_kind) { self.encode_stability(def_id); @@ -1521,60 +1558,60 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record_array!(self.tables.variances_of[def_id] <- v); } if should_encode_fn_sig(def_kind) { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); + record_some_lazy!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); } if should_encode_generics(def_kind) { let g = tcx.generics_of(def_id); - record!(self.tables.generics_of[def_id] <- g); - record!(self.tables.explicit_clauses_of[def_id] <- self.tcx.explicit_clauses_of(def_id)); + record_some_lazy!(self.tables.generics_of[def_id] <- g); + record_some_lazy!(self.tables.explicit_clauses_of[def_id] <- self.tcx.explicit_clauses_of(def_id)); let inferred_outlives = self.tcx.inferred_outlives_of(def_id); record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); for param in &g.own_params { if let ty::GenericParamDefKind::Const { has_default: true, .. } = param.kind { let default = self.tcx.const_param_default(param.def_id); - record!(self.tables.const_param_default[param.def_id] <- default); + record_some_lazy!(self.tables.const_param_default[param.def_id] <- default); } } } if tcx.is_conditionally_const(def_id) { - record!(self.tables.const_conditions[def_id] <- self.tcx.const_conditions(def_id)); + record_some_lazy!(self.tables.const_conditions[def_id] <- self.tcx.const_conditions(def_id)); } if should_encode_type(tcx, local_id, def_kind) { - record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); + record_some_lazy!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); } if should_encode_constness(def_kind) { let constness = self.tcx.constness(def_id); - self.tables.constness.set(def_id.index, constness); + record_value!(self.tables.constness[def_id] <- constness) } if let DefKind::Fn | DefKind::AssocFn = def_kind { let asyncness = tcx.asyncness(def_id); - self.tables.asyncness.set(def_id.index, asyncness); + record_value!(self.tables.asyncness[def_id] <- asyncness); record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id)); } if let Some(name) = tcx.intrinsic(def_id) { - record!(self.tables.intrinsic[def_id] <- name); + record_some_lazy!(self.tables.intrinsic[def_id] <- name); } if let DefKind::TyParam | DefKind::Trait = def_kind { let default = self.tcx.object_lifetime_default(def_id); - record!(self.tables.object_lifetime_default[def_id] <- default); + record_some_lazy!(self.tables.object_lifetime_default[def_id] <- default); } if let DefKind::Trait = def_kind { - record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); + record_some_lazy!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); record_defaulted_array!(self.tables.explicit_super_clauses_of[def_id] <- self.tcx.explicit_super_clauses_of(def_id).skip_binder()); record_defaulted_array!(self.tables.explicit_implied_clauses_of[def_id] <- self.tcx.explicit_implied_clauses_of(def_id).skip_binder()); let module_children = self.tcx.module_children_local(local_id); record_array!(self.tables.module_children_non_reexports[def_id] <- - module_children.iter().map(|child| child.res.def_id().index)); + module_children.iter().map(|child| child.res.def_id().expect_local())); if self.tcx.is_const_trait(def_id) { record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id] <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder()); } } if let DefKind::TraitAlias = def_kind { - record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); + record_some_lazy!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); record_defaulted_array!(self.tables.explicit_super_clauses_of[def_id] <- self.tcx.explicit_super_clauses_of(def_id).skip_binder()); record_defaulted_array!(self.tables.explicit_implied_clauses_of[def_id] <- @@ -1584,8 +1621,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id); record_array!(self.tables.associated_item_or_field_def_ids[def_id] <- associated_item_def_ids.iter().map(|&def_id| { - assert!(def_id.is_local()); - def_id.index + def_id.expect_local() }) ); for &def_id in associated_item_def_ids { @@ -1595,28 +1631,28 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if let DefKind::Closure | DefKind::SyntheticCoroutineBody = def_kind && let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id) { - self.tables.coroutine_kind.set(def_id.index, Some(coroutine_kind)) + record_value!(self.tables.coroutine_kind[def_id] <- Some(coroutine_kind)) } if def_kind == DefKind::Closure && tcx.type_of(def_id).skip_binder().is_coroutine_closure() { let coroutine_for_closure = self.tcx.coroutine_for_closure(def_id); - self.tables - .coroutine_for_closure - .set_some(def_id.index, coroutine_for_closure.into()); + record_some!(self.tables.coroutine_for_closure[def_id] <- coroutine_for_closure.into()); // If this async closure has a by-move body, record it too. if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure) { - self.tables.coroutine_by_move_body_def_id.set_some( - coroutine_for_closure.index, - self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure).into(), - ); + let id = self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure); + record_some!( + self.tables.coroutine_by_move_body_def_id[coroutine_for_closure] <- id.into() + ) } } if let DefKind::Static { .. } = def_kind { if !self.tcx.is_foreign_item(def_id) { match self.tcx.eval_static_initializer(def_id) { - Ok(data) => record!(self.tables.eval_static_initializer[def_id] <- data), + Ok(data) => { + record_some_lazy!(self.tables.eval_static_initializer[def_id] <- data) + } Err(err) => match err { interpret::ErrorHandled::Reported(_, _) => { self.tcx.dcx().delayed_bug(format!( @@ -1640,62 +1676,60 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.encode_info_for_macro(local_id); } if let DefKind::TyAlias = def_kind { - self.tables - .type_alias_is_checked - .set(def_id.index, self.tcx.type_alias_is_checked(def_id)); + record_value!(self.tables.type_alias_is_checked[def_id] <- self.tcx.type_alias_is_checked(def_id)); + if self.tcx.type_alias_is_checked(def_id) { - record!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); + record_some_lazy!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } } if let DefKind::OpaqueTy = def_kind { self.encode_explicit_item_bounds(def_id); self.encode_explicit_item_self_bounds(def_id); - record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id)); + record_some_lazy!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id)); self.encode_precise_capturing_args(def_id); if tcx.is_conditionally_const(def_id) { record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id] <- tcx.explicit_implied_const_bounds(def_id).skip_binder()); } - record!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); + record_some_lazy!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } if let DefKind::AssocTy = def_kind { let assoc_item = tcx.associated_item(def_id); match assoc_item.container { ty::AssocContainer::Trait => { - record!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); + record_some_lazy!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } ty::AssocContainer::InherentImpl => { - record!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); + record_some_lazy!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } ty::AssocContainer::TraitImpl(_) => {} } } if let DefKind::AnonConst = def_kind { - record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id)); + record_some_lazy!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id)); } if let DefKind::Const | DefKind::AssocConst = def_kind { - record!(self.tables.const_of_item[def_id] <- self.tcx.const_of_item(def_id)); + record_some_lazy!(self.tables.const_of_item[def_id] <- self.tcx.const_of_item(def_id)); } if tcx.impl_method_has_trait_impl_trait_tys(def_id) && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id) { - record!(self.tables.collect_return_position_impl_trait_in_trait_tys[def_id] <- table); + record_some_lazy!(self.tables.collect_return_position_impl_trait_in_trait_tys[def_id] <- table); } if let DefKind::Impl { .. } | DefKind::Trait = def_kind { let table = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id); - record!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table); + record_some_lazy!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table); } } for (def_id, impls) in &tcx.crate_inherent_impls(()).0.inherent_impls { record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| { - assert!(def_id.is_local()); - def_id.index + def_id.expect_local() })); } for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions { - record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map); + record_some_lazy!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map); } for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope { @@ -1722,15 +1756,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let def_id = local_def_id.to_def_id(); let tcx = self.tcx; let adt_def = tcx.adt_def(def_id); - record!(self.tables.repr_options[def_id] <- adt_def.repr()); + record_some_lazy!(self.tables.repr_options[def_id] <- adt_def.repr()); let params_in_repr = self.tcx.params_in_repr(def_id); - record!(self.tables.params_in_repr[def_id] <- params_in_repr); + record_some_lazy!(self.tables.params_in_repr[def_id] <- params_in_repr); if adt_def.is_enum() { let module_children = tcx.module_children_local(local_def_id); record_array!(self.tables.module_children_non_reexports[def_id] <- - module_children.iter().map(|child| child.res.def_id().index)); + module_children.iter().map(|child| child.res.def_id().expect_local())); } else { // For non-enum, there is only one variant, and its def_id is the adt's. debug_assert_eq!(adt_def.variants().len(), 1); @@ -1742,19 +1776,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let data = VariantData { discr: variant.discr, idx, - ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), + ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.expect_local())), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.variant_data[variant.def_id] <- data); + record_some_lazy!(self.tables.variant_data[variant.def_id] <- data); record_array!(self.tables.associated_item_or_field_def_ids[variant.def_id] <- variant.fields.iter().map(|f| { - assert!(f.did.is_local()); - f.did.index + f.did.expect_local() })); for field in &variant.fields { - self.tables.safety.set(field.did.index, field.safety); - record!( + record_value!(self.tables.safety[field.did] <- field.safety); + record_some_lazy!( self.tables.mut_restriction[field.did] <- field.mut_restriction ); } @@ -1762,16 +1795,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { let fn_sig = tcx.fn_sig(ctor_def_id); // FIXME only encode signature for ctor_def_id - record!(self.tables.fn_sig[variant.def_id] <- fn_sig); + record_some_lazy!(self.tables.fn_sig[variant.def_id] <- fn_sig); } } if let Some(destructor) = tcx.adt_destructor(local_def_id) { - record!(self.tables.adt_destructor[def_id] <- destructor); + record_some_lazy!(self.tables.adt_destructor[def_id] <- destructor); } if let Some(destructor) = tcx.adt_async_destructor(local_def_id) { - record!(self.tables.adt_async_destructor[def_id] <- destructor); + record_some_lazy!(self.tables.adt_async_destructor[def_id] <- destructor); } } @@ -1787,13 +1820,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // items - we encode information about proc-macros later on. if self.is_proc_macro { // Encode this here because we don't do it in encode_def_ids. - record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); + record_some_lazy!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); } else { let module_children = tcx.module_children_local(local_def_id); record_array!(self.tables.module_children_non_reexports[def_id] <- module_children.iter().filter(|child| child.reexport_chain.is_empty()) - .map(|child| child.res.def_id().index)); + .map(|child| child.res.def_id().expect_local())); record_defaulted_array!(self.tables.module_children_reexports[def_id] <- module_children.iter().filter(|child| !child.reexport_chain.is_empty())); @@ -1826,10 +1859,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let item = tcx.associated_item(def_id); if matches!(item.container, AssocContainer::Trait | AssocContainer::TraitImpl(_)) { - self.tables.defaultness.set(def_id.index, item.defaultness(tcx)); + record_value!(self.tables.defaultness[def_id] <- item.defaultness(tcx)); } - record!(self.tables.assoc_container[def_id] <- item.container); + record_some_lazy!(self.tables.assoc_container[def_id] <- item.container); if let AssocContainer::Trait = item.container && item.is_type() @@ -1842,7 +1875,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } if let ty::AssocKind::Type { data: ty::AssocTypeData::Rpitit(rpitit_info) } = item.kind { - record!(self.tables.opt_rpitit_info[def_id] <- rpitit_info); + record_some_lazy!(self.tables.opt_rpitit_info[def_id] <- rpitit_info); if matches!(rpitit_info, ty::ImplTraitInTraitData::Trait { .. }) { record_array!( self.tables.assumed_wf_types_for_rpitit[def_id] @@ -1878,53 +1911,53 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EntryBuilder::encode_mir({:?})", def_id); if encode_opt { - record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id)); - self.tables - .cross_crate_inlinable - .set(def_id.to_def_id().index, self.tcx.cross_crate_inlinable(def_id)); - record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()] + record_some_lazy!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id)); + + record_value!(self.tables.cross_crate_inlinable[def_id.to_def_id()] <- self.tcx.cross_crate_inlinable(def_id)); + + record_some_lazy!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()] <- tcx.closure_saved_names_of_captured_variables(def_id)); if self.tcx.is_coroutine(def_id.to_def_id()) && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id) { - record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); + record_some_lazy!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); } } let mut is_trivial = false; if encode_const { if let Some((val, ty)) = tcx.trivial_const(def_id) { is_trivial = true; - record!(self.tables.trivial_const[def_id.to_def_id()] <- (val, ty)); + record_some_lazy!(self.tables.trivial_const[def_id.to_def_id()] <- (val, ty)); } else { is_trivial = false; - record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id)); + record_some_lazy!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id)); } // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir` let abstract_const = tcx.thir_abstract_const(def_id); if let Ok(Some(abstract_const)) = abstract_const { - record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); + record_some_lazy!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); } if should_encode_const(tcx.def_kind(def_id)) { let qualifs = tcx.mir_const_qualif(def_id); - record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); + record_some_lazy!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); let body = tcx.hir_maybe_body_owned_by(def_id); if let Some(body) = body { let const_data = rendered_const(self.tcx, &body, def_id); - record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); + record_some_lazy!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); } } } if !is_trivial { - record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); + record_some_lazy!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); } if self.tcx.is_coroutine(def_id.to_def_id()) && let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id) { - record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); + record_some_lazy!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); } } @@ -1950,7 +1983,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.unstable_opts.force_unstable_if_unmarked { if let Some(stab) = self.tcx.lookup_stability(def_id) { - record!(self.tables.lookup_stability[def_id] <- stab) + record_some_lazy!(self.tables.lookup_stability[def_id] <- stab) } } } @@ -1961,7 +1994,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.unstable_opts.force_unstable_if_unmarked { if let Some(stab) = self.tcx.lookup_const_stability(def_id) { - record!(self.tables.lookup_const_stability[def_id] <- stab) + record_some_lazy!(self.tables.lookup_const_stability[def_id] <- stab) } } } @@ -1972,7 +2005,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.unstable_opts.force_unstable_if_unmarked { if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) { - record!(self.tables.lookup_default_body_stability[def_id] <- stab) + record_some_lazy!(self.tables.lookup_default_body_stability[def_id] <- stab) } } } @@ -1980,7 +2013,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { #[instrument(level = "debug", skip(self))] fn encode_deprecation(&mut self, def_id: DefId) { if let Some(depr) = self.tcx.lookup_deprecation(def_id) { - record!(self.tables.lookup_deprecation_entry[def_id] <- depr); + record_some_lazy!(self.tables.lookup_deprecation_entry[def_id] <- depr); } } @@ -1989,8 +2022,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let tcx = self.tcx; let (_, macro_def, _) = tcx.hir_expect_item(def_id).expect_macro(); - self.tables.is_macro_rules.set(def_id.local_def_index, macro_def.macro_rules); - record!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body); + record_value!(self.tables.is_macro_rules[def_id.to_def_id()] <- macro_def.macro_rules); + record_some_lazy!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body); } fn encode_native_libraries(&mut self) -> LazyArray { @@ -2037,26 +2070,22 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let is_proc_macro = self.tcx.crate_types().contains(&CrateType::ProcMacro); if is_proc_macro { let tcx = self.tcx; - let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; + let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap(); + let proc_macro_decls_static = self.lazy(proc_macro_decls_static); + let stability = tcx.lookup_stability(CRATE_DEF_ID); - for (i, span) in self.tcx.sess.proc_macro_quoted_spans() { - let span = self.lazy(span); - self.tables.proc_macro_quoted_spans.set_some(i, span); - } - self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod); - record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); + record_some!(self.tables.def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod); + record_some_lazy!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local()); - let vis = tcx - .local_visibility(CRATE_DEF_ID) - .map_id(|mod_id| mod_id.to_local_def_id().local_def_index); - record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); + let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|mod_id| mod_id.to_local_def_id()); + record_some_lazy!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); if let Some(stability) = stability { - record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); + record_some_lazy!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); } self.encode_deprecation(LOCAL_CRATE.as_def_id()); if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_MOD_ID) { - record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map); + record_some_lazy!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map); } if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_MOD_ID) { record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits); @@ -2097,26 +2126,39 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { bug!("Unknown proc-macro type for item {:?}", id); }; - macros.push((id.local_def_index, self.lazy(kind))); + macros.push((id.into(), self.lazy(kind))); let mut def_key = self.tcx.hir_def_key(id); def_key.disambiguated_data.data = DefPathData::MacroNs(name); let def_id = id.to_def_id(); - self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind.into())); + record_some!(self.tables.def_kind[def_id] <- DefKind::Macro(macro_kind.into())); + self.encode_attrs(id); - record!(self.tables.def_keys[def_id] <- def_key); - 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); + record_some_lazy!(self.tables.def_keys[def_id] <- def_key); + record_some_lazy!(self.tables.def_ident_span[def_id] <- span); + record_some_lazy!(self.tables.def_span[def_id] <- span); + record_some_lazy!(self.tables.visibility[def_id] <- ty::Visibility::Public); if let Some(stability) = stability { - record!(self.tables.lookup_stability[def_id] <- stability); + record_some_lazy!(self.tables.lookup_stability[def_id] <- stability); } } let macros = self.lazy_array(macros); - Some(ProcMacroData { proc_macro_decls_static, stability, macros }) + let mut proc_macro_quoted_spans = TableBuilder::default(); + for (i, span) in self.tcx.sess.proc_macro_quoted_spans() { + proc_macro_quoted_spans.set_some(i, self.lazy(span)); + } + + let proc_macro_quoted_spans = proc_macro_quoted_spans.encode(&mut self.opaque); + + Some(ProcMacroData { + proc_macro_decls_static, + stability, + macros, + proc_macro_quoted_spans, + }) } else { None } @@ -2198,33 +2240,36 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(sorted.into_iter().map(|(k, v)| (*k, *v))) } - fn encode_canonical_symbols(&mut self) -> LazyArray<(Symbol, DefIndex)> { + fn encode_canonical_symbols(&mut self) -> LazyArray<(Symbol, LocalDefId)> { empty_proc_macro!(self); let tcx = self.tcx; let canonical_symbols = &tcx.canonical_symbols(LOCAL_CRATE); - self.lazy_array(canonical_symbols.iter().map(|cs| (cs.symbol, cs.def_id.index))) + self.lazy_array(canonical_symbols.iter().map(|cs| (cs.symbol, cs.def_id.expect_local()))) } - fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> { + fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, LocalDefId)> { empty_proc_macro!(self); let tcx = self.tcx; let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id; - self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index))) + self.lazy_array( + diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.expect_local())), + ) } - fn encode_fake_doc_items(&mut self) -> LazyArray { + fn encode_fake_doc_items(&mut self) -> LazyArray { empty_proc_macro!(self); let tcx = self.tcx; let fake_doc_items = &tcx.fake_doc_items(LOCAL_CRATE); - self.lazy_array(fake_doc_items.iter().map(|cs| cs.index)) + self.lazy_array(fake_doc_items.iter().map(|cs| cs.expect_local())) } - fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> { + fn encode_lang_items(&mut self) -> LazyArray<(LocalDefId, LangItem)> { empty_proc_macro!(self); let lang_items = self.tcx.lang_items().iter(); - self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| { - def_id.as_local().map(|id| (id.local_def_index, lang_item)) - })) + self.lazy_array( + lang_items + .filter_map(|(lang_item, def_id)| def_id.as_local().map(|id| (id, lang_item))), + ) } fn encode_lang_items_missing(&mut self) -> LazyArray { @@ -2233,18 +2278,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(&tcx.lang_items().missing) } - fn encode_stripped_cfg_items(&mut self) -> LazyArray> { + fn encode_stripped_cfg_items(&mut self) -> LazyArray> { self.lazy_array( self.tcx .stripped_cfg_items(LOCAL_CRATE) .into_iter() - .map(|item| item.clone().map_scope_id(|def_id| def_id.index)), + .map(|item| item.clone().map_scope_id(|def_id| def_id.expect_local())), ) } - fn encode_traits(&mut self) -> LazyArray { + fn encode_traits(&mut self) -> LazyArray { empty_proc_macro!(self); - self.lazy_array(self.tcx.traits(LOCAL_CRATE).iter().map(|def_id| def_id.index)) + self.lazy_array(self.tcx.traits(LOCAL_CRATE).iter().map(|def_id| def_id.expect_local())) } /// Encodes an index, mapping each trait to its (local) implementations. @@ -2252,7 +2297,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_impls(&mut self) -> LazyArray { empty_proc_macro!(self); let tcx = self.tcx; - let mut trait_impls: FxIndexMap)>> = + let mut trait_impls: FxIndexMap)>> = FxIndexMap::default(); for id in tcx.hir_free_items() { @@ -2263,15 +2308,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if of_trait { let header = tcx.impl_trait_header(def_id); - record!(self.tables.impl_trait_header[def_id] <- header); + record_some_lazy!(self.tables.impl_trait_header[def_id] <- header); let impl_is_fully_generic_for_reflection = tcx.impl_is_fully_generic_for_reflection(def_id); - self.tables - .impl_is_fully_generic_for_reflection - .set(def_id.index, impl_is_fully_generic_for_reflection); - self.tables.defaultness.set(def_id.index, tcx.defaultness(def_id)); + record_value!(self.tables.impl_is_fully_generic_for_reflection[def_id] <- impl_is_fully_generic_for_reflection); + record_value!(self.tables.defaultness[def_id] <- tcx.defaultness(def_id)); let trait_ref = header.trait_ref.instantiate_identity().skip_norm_wip(); let simplified_self_ty = fast_reject::simplify_type( @@ -2282,20 +2325,20 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { trait_impls .entry(trait_ref.def_id) .or_default() - .push((id.owner_id.def_id.local_def_index, simplified_self_ty)); + .push((id.owner_id.def_id, simplified_self_ty)); let trait_def = tcx.trait_def(trait_ref.def_id); if let Ok(mut an) = trait_def.ancestors(tcx, def_id) && let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) { - self.tables.impl_parent.set_some(def_id.index, parent.into()); + record_some!(self.tables.impl_parent[def_id] <- parent.into()); } // if this is an impl of `CoerceUnsized`, create its // "unsized info", else just store None if tcx.is_lang_item(trait_ref.def_id, LangItem::CoerceUnsized) { let coerce_unsized_info = tcx.coerce_unsized_info(def_id).unwrap(); - record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info); + record_some_lazy!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info); } } } @@ -2303,7 +2346,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let trait_impls: Vec<_> = trait_impls .into_iter() .map(|(trait_def_id, impls)| TraitImpls { - trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index), + trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index.as_u32()), impls: self.lazy_array(&impls), }) .collect(); @@ -2323,24 +2366,28 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .iter() .map(|(&simp, impls)| IncoherentImpls { self_ty: self.lazy(simp), - impls: self.lazy_array(impls.iter().map(|def_id| def_id.local_def_index)), + impls: self.lazy_array(impls.iter().map(|def_id| *def_id)), }) .collect(); self.lazy_array(&all_impls) } - fn encode_exportable_items(&mut self) -> LazyArray { + fn encode_exportable_items(&mut self) -> LazyArray { empty_proc_macro!(self); - self.lazy_array(self.tcx.exportable_items(LOCAL_CRATE).iter().map(|def_id| def_id.index)) + self.lazy_array( + self.tcx.exportable_items(LOCAL_CRATE).iter().map(|def_id| def_id.expect_local()), + ) } - fn encode_stable_order_of_exportable_impls(&mut self) -> LazyArray<(DefIndex, usize)> { + fn encode_stable_order_of_exportable_impls(&mut self) -> LazyArray<(LocalDefId, usize)> { empty_proc_macro!(self); let stable_order_of_exportable_impls = self.tcx.stable_order_of_exportable_impls(LOCAL_CRATE); self.lazy_array( - stable_order_of_exportable_impls.iter().map(|(def_id, idx)| (def_id.index, *idx)), + stable_order_of_exportable_impls + .iter() + .map(|(def_id, idx)| (def_id.expect_local(), *idx)), ) } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index b70151604f679..8a089ac170309 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -11,7 +11,9 @@ pub(crate) use parameterized::ParameterizedOverTcx; use rustc_abi::{FieldIdx, ReprOptions, VariantIdx}; use rustc_ast as ast; use rustc_attr_ir::lang_items::LangItem; -use rustc_attr_ir::{Stability, StrippedCfgItem}; +use rustc_attr_ir::{ + Attribute, ConstStability, DefaultBodyStability, Deprecation, Stability, StrippedCfgItem, +}; use rustc_crate_store::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; @@ -19,7 +21,9 @@ use rustc_data_structures::svh::Svh; use rustc_hir as hir; use rustc_hir::PreciseCapturingArgKind; use rustc_hir::def::{CtorKind, DefKind, MacroKinds}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIndex, DefPathHash, StableCrateId}; +use rustc_hir::def_id::{ + CrateNum, DefId, DefIdMap, DefIndex, DefPathHash, LocalDefId, StableCrateId, +}; use rustc_hir::definitions::DefKey; use rustc_index::IndexVec; use rustc_index::bit_set::DenseBitSet; @@ -48,7 +52,6 @@ use rustc_target::spec::{PanicStrategy, TargetTuple}; use table::TableBuilder; use crate::eii::EiiMapEncodedKeyValue; -use crate::rmeta::table::TableBuilderSingleIdx; mod decoder; mod def_path_hash_map; @@ -211,9 +214,10 @@ type ExpnHashTable = LazyTableSingleIdx>>; #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct ProcMacroData { - proc_macro_decls_static: DefIndex, + proc_macro_decls_static: LazyValue, stability: Option, - macros: LazyArray<(DefIndex, LazyValue)>, + macros: LazyArray<(LocalDefId, LazyValue)>, + proc_macro_quoted_spans: LazyTable>>, } #[derive(MetadataEncodable, LazyDecodable)] @@ -283,15 +287,15 @@ pub(crate) struct CrateRoot { dylib_dependency_formats: LazyArray>, lib_features: LazyArray<(Symbol, FeatureStability)>, stability_implications: LazyArray<(Symbol, Symbol)>, - lang_items: LazyArray<(DefIndex, LangItem)>, + lang_items: LazyArray<(LocalDefId, LangItem)>, lang_items_missing: LazyArray, - stripped_cfg_items: LazyArray>, - diagnostic_items: LazyArray<(Symbol, DefIndex)>, - canonical_symbols: LazyArray<(Symbol, DefIndex)>, - fake_doc_items: LazyArray, + stripped_cfg_items: LazyArray>, + diagnostic_items: LazyArray<(Symbol, LocalDefId)>, + canonical_symbols: LazyArray<(Symbol, LocalDefId)>, + fake_doc_items: LazyArray, native_libraries: LazyArray, foreign_modules: LazyArray, - traits: LazyArray, + traits: LazyArray, impls: LazyArray, incoherent_impls: LazyArray, interpret_alloc_index: LazyArray, @@ -300,8 +304,8 @@ pub(crate) struct CrateRoot { tables: LazyTables, debugger_visualizers: LazyArray, - exportable_items: LazyArray, - stable_order_of_exportable_impls: LazyArray<(DefIndex, usize)>, + exportable_items: LazyArray, + stable_order_of_exportable_impls: LazyArray<(LocalDefId, usize)>, exported_non_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>, exported_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>, @@ -377,32 +381,32 @@ pub(crate) struct CrateDep { #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct TraitImpls { - trait_id: (u32, DefIndex), - impls: LazyArray<(DefIndex, Option)>, + trait_id: (u32, u32), + impls: LazyArray<(LocalDefId, Option)>, } #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct IncoherentImpls { self_ty: LazyValue, - impls: LazyArray, + impls: LazyArray, } /// Define `LazyTables` and `TableBuilders` at the same time. macro_rules! define_tables { ( - - defaulted: $($name1:ident: Table<$IDX1:ty, $T1:ty>,)+ - - optional: $($name2:ident: Table<$IDX2:ty, $T2:ty>,)+ + - defaulted: $($name1:ident: Table<$T1:ty>,)+ + - optional: $($name2:ident: Table<$T2:ty>,)+ ) => { #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct LazyTables { - $($name1: LazyTableSingleIdx<$IDX1, $T1>,)+ - $($name2: LazyTableSingleIdx<$IDX2, Option<$T2>>,)+ + $($name1: LazyTable,)+ + $($name2: LazyTable>,)+ } #[derive(Default)] struct TableBuilders { - $($name1: TableBuilderSingleIdx<$IDX1, $T1>,)+ - $($name2: TableBuilderSingleIdx<$IDX2, Option<$T2>>,)+ + $($name1: TableBuilder,)+ + $($name2: TableBuilder>,)+ } impl TableBuilders { @@ -418,107 +422,106 @@ macro_rules! define_tables { define_tables! { - defaulted: - intrinsic: Table>>, - is_macro_rules: Table, - type_alias_is_checked: Table, - attr_flags: Table, + intrinsic: Table>>, + is_macro_rules: Table, + type_alias_is_checked: Table, + attr_flags: Table, // The u64 is the crate-local part of the DefPathHash. All hashes in this crate have the same // StableCrateId, so we omit encoding those into the table. // // Note also that this table is fully populated (no gaps) as every DefIndex should have a // corresponding DefPathHash. - def_path_hashes: Table, - explicit_item_bounds: Table, Span)>>, - explicit_item_self_bounds: Table, Span)>>, - inferred_outlives_of: Table, Span)>>, - explicit_super_clauses_of: Table, Span)>>, - explicit_implied_clauses_of: Table, Span)>>, - explicit_implied_const_bounds: Table, Span)>>, - inherent_impls: Table>, - opt_rpitit_info: Table>>, + def_path_hashes: Table, + explicit_item_bounds: Table, Span)>>, + explicit_item_self_bounds: Table, Span)>>, + inferred_outlives_of: Table, Span)>>, + explicit_super_clauses_of: Table, Span)>>, + explicit_implied_clauses_of: Table, Span)>>, + explicit_implied_const_bounds: Table, Span)>>, + inherent_impls: Table>, + opt_rpitit_info: Table>>, // Reexported names are not associated with individual `DefId`s, // e.g. a glob import can introduce a lot of names, all with the same `DefId`. // That's why the encoded list needs to contain `ModChild` structures describing all the names // individually instead of `DefId`s. - module_children_reexports: Table>, - ambig_module_children: Table>, - cross_crate_inlinable: Table, - asyncness: Table, - constness: Table, - safety: Table, - defaultness: Table, - impl_is_fully_generic_for_reflection: Table, + module_children_reexports: Table>, + ambig_module_children: Table>, + cross_crate_inlinable: Table, + asyncness: Table, + constness: Table, + safety: Table, + defaultness: Table, + impl_is_fully_generic_for_reflection: Table, - optional: - attributes: Table>, + attributes: Table>, // For non-reexported names in a module every name is associated with a separate `DefId`, // so we can take their names, visibilities etc from other encoded tables. - module_children_non_reexports: Table>, - associated_item_or_field_def_ids: Table>, - def_kind: Table, - visibility: Table>>, - def_span: Table>, - def_ident_span: Table>, - lookup_stability: Table>, - lookup_const_stability: Table>, - lookup_default_body_stability: Table>, - lookup_deprecation_entry: Table>, - explicit_clauses_of: Table>>, - generics_of: Table>, - type_of: Table>>>, - variances_of: Table>, - fn_sig: Table>>>, - codegen_fn_attrs: Table>, - impl_trait_header: Table>>, - const_param_default: Table>>>, - object_lifetime_default: Table>, - optimized_mir: Table>>, - mir_for_ctfe: Table>>, - trivial_const: Table)>>, - closure_saved_names_of_captured_variables: Table>>, - mir_coroutine_witnesses: Table>>, - promoted_mir: Table>>>, - thir_abstract_const: Table>>>, - impl_parent: Table, - const_conditions: Table>>, + module_children_non_reexports: Table>, + associated_item_or_field_def_ids: Table>, + def_kind: Table, + visibility: Table>>, + def_span: Table>, + def_ident_span: Table>, + lookup_stability: Table>, + lookup_const_stability: Table>, + lookup_default_body_stability: Table>, + lookup_deprecation_entry: Table>, + explicit_clauses_of: Table>>, + generics_of: Table>, + type_of: Table>>>, + variances_of: Table>, + fn_sig: Table>>>, + codegen_fn_attrs: Table>, + impl_trait_header: Table>>, + const_param_default: Table>>>, + object_lifetime_default: Table>, + optimized_mir: Table>>, + mir_for_ctfe: Table>>, + trivial_const: Table)>>, + closure_saved_names_of_captured_variables: Table>>, + mir_coroutine_witnesses: Table>>, + promoted_mir: Table>>>, + thir_abstract_const: Table>>>, + impl_parent: Table, + const_conditions: Table>>, // FIXME(eddyb) perhaps compute this on the fly if cheap enough? - coerce_unsized_info: Table>, - mir_const_qualif: Table>, - rendered_const: Table>, - rendered_precise_capturing_args: Table>>, - fn_arg_idents: Table>>, - coroutine_kind: Table, - coroutine_for_closure: Table, - adt_destructor: Table>, - adt_async_destructor: Table>, - coroutine_by_move_body_def_id: Table, - eval_static_initializer: Table>>, - trait_def: Table>, - expn_that_defined: Table>, - default_fields: Table>, - params_in_repr: Table>>, - repr_options: Table>, + coerce_unsized_info: Table>, + mir_const_qualif: Table>, + rendered_const: Table>, + rendered_precise_capturing_args: Table>>, + fn_arg_idents: Table>>, + coroutine_kind: Table, + coroutine_for_closure: Table, + adt_destructor: Table>, + adt_async_destructor: Table>, + coroutine_by_move_body_def_id: Table, + eval_static_initializer: Table>>, + trait_def: Table>, + expn_that_defined: Table>, + default_fields: Table>, + params_in_repr: Table>>, + repr_options: Table>, // `def_keys` and `def_path_hashes` represent a lazy version of a // `DefPathTable`. This allows us to avoid deserializing an entire // `DefPathTable` up front, since we may only ever use a few // definitions from any given crate. - def_keys: Table>, - proc_macro_quoted_spans: Table>, - variant_data: Table>, - assoc_container: Table>, - macro_definition: Table>, - deduced_param_attrs: Table>, - collect_return_position_impl_trait_in_trait_tys: Table>>>>, - doc_link_resolutions: Table>, - doc_link_traits_in_scope: Table>, - assumed_wf_types_for_rpitit: Table, Span)>>, - opaque_ty_origin: Table>>, - anon_const_kind: Table>, - const_of_item: Table>>>>, - associated_types_for_impl_traits_in_trait_or_impl: Table>>>, - live_args_for_alias_from_outlives_bounds: Table>>, - args_known_to_outlive_alias_params: Table)>>>, - mut_restriction: Table>, + def_keys: Table>, + variant_data: Table>, + assoc_container: Table>, + macro_definition: Table>, + deduced_param_attrs: Table>, + collect_return_position_impl_trait_in_trait_tys: Table>>>>, + doc_link_resolutions: Table>, + doc_link_traits_in_scope: Table>, + assumed_wf_types_for_rpitit: Table, Span)>>, + opaque_ty_origin: Table>>, + anon_const_kind: Table>, + const_of_item: Table>>>>, + associated_types_for_impl_traits_in_trait_or_impl: Table>>>, + live_args_for_alias_from_outlives_bounds: Table>>, + args_known_to_outlive_alias_params: Table)>>>, + mut_restriction: Table>, } #[derive(TyEncodable, TyDecodable)] @@ -526,7 +529,7 @@ struct VariantData { idx: VariantIdx, discr: ty::VariantDiscr, /// If this is unit or tuple-variant/struct, then this is the index of the ctor id. - ctor: Option<(CtorKind, DefIndex)>, + ctor: Option<(CtorKind, LocalDefId)>, is_non_exhaustive: bool, } diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 670d875f885c9..24177e4f83de8 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -1,12 +1,11 @@ use std::hash::Hash; use rustc_data_structures::unord::UnordMap; -use rustc_hir::def_id::DefIndex; use rustc_index::{Idx, IndexVec}; use rustc_middle::ty::{Binder, EarlyBinder, GenericArg, Region}; use rustc_span::Symbol; -use crate::rmeta::{LazyArray, LazyValue}; +use crate::rmeta::{LazyArray, LazyValue, LocalDefId}; pub(crate) trait ParameterizedOverTcx: 'static { type Value<'tcx>; @@ -80,6 +79,7 @@ trivially_parameterized_over_tcx! { crate::rmeta::CrateRoot, crate::rmeta::CrateRootUnhashed, crate::rmeta::IncoherentImpls, + crate::rmeta::LocalDefId, crate::rmeta::ProcMacroKind, crate::rmeta::RawDefId, crate::rmeta::TraitImpls, @@ -93,7 +93,7 @@ trivially_parameterized_over_tcx! { rustc_attr_ir::EiiDecl, rustc_attr_ir::EiiImpl, rustc_attr_ir::Stability, - rustc_attr_ir::StrippedCfgItem, + rustc_attr_ir::StrippedCfgItem, rustc_attr_ir::lang_items::LangItem, rustc_crate_store::ForeignModule, rustc_crate_store::LinkagePreference, @@ -131,7 +131,7 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::RestrictionKind, rustc_middle::ty::TraitDef, rustc_middle::ty::Variance, - rustc_middle::ty::Visibility, + rustc_middle::ty::Visibility, rustc_middle::ty::adjustment::CoerceUnsizedInfo, rustc_middle::ty::fast_reject::SimplifiedType, rustc_session::config::TargetModifier, diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index c023b61d73571..bbac0b5ad5435 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -433,8 +433,6 @@ pub(super) struct TableBuilder, } -pub(super) type TableBuilderSingleIdx = TableBuilder; - impl Default for TableBuilder { fn default() -> Self { TableBuilder { width: 0, blocks: Default::default(), _marker: PhantomData } diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index b2b5d2aa22bc1..fc3b3cf435d2f 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -399,13 +399,13 @@ impl fmt::Debug for LocalDefId { } impl Encodable for LocalDefId { - fn encode(&self, s: &mut E) { - self.to_def_id().encode(s); + default fn encode(&self, e: &mut E) { + self.to_def_id().encode(e); } } impl Decodable for LocalDefId { - fn decode(d: &mut D) -> LocalDefId { + default fn decode(d: &mut D) -> LocalDefId { DefId::decode(d).expect_local() } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index ddec29eca6517..e94ba51920e6a 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -22,6 +22,7 @@ #![feature(decl_macro)] #![feature(diagnostic_on_unknown)] #![feature(map_try_insert)] +#![feature(min_specialization)] #![feature(negative_impls)] #![feature(read_buf)] #![feature(rustc_attrs)]