diff --git a/compiler/rustc_hir_id/src/definitions.rs b/compiler/rustc_hir_id/src/definitions.rs index 426a4e0109153..369075b8fd87e 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; @@ -59,15 +60,17 @@ pub struct DefPathToIndexMap { /// hash (which does not change between compiler invocations) mapping. We use it for relatively small number /// of definitions, so the majority of them would be stored in `DefPathHashMap`, which makes insertion /// and serialization costs of `SortedMap` acceptable. - pub after_parallel_alloc: Option>, + pub after_parallel_alloc: Option>, } impl DefPathToIndexMap { #[inline] pub fn get(&self, hash: Hash64) -> Option { - self.before_parallel_alloc - .get(&hash) - .or_else(|| self.after_parallel_alloc.as_ref().and_then(|map| map.get(&hash).copied())) + self.before_parallel_alloc.get(&hash).or_else(|| { + self.after_parallel_alloc + .as_ref() + .and_then(|map| map.get(&hash).map(|id| id.local_def_index)) + }) } /// This insert function does not behave like regular `insert` of a `HashMap`, @@ -83,7 +86,7 @@ impl DefPathToIndexMap { return Some(existing); } - map.insert(hash, index) + map.insert(hash, LocalDefId { local_def_index: index }).map(|id| id.local_def_index) } } } @@ -101,7 +104,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 +113,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 5348ee0ccaac1..5705831bcfaee 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -100,12 +100,18 @@ 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)>>, + /// FIXME: make type of the value `LazyArray<(DefIndex, Option)>` + /// through transmuting or somehow else, as `LocalDefId` here should be `DefIndex`, + /// as it is from external crate. + trait_impls: FxIndexMap)>>, /// 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>, + /// FIXME: make type of the value `LazyArray` + /// through transmuting or somehow else, as `LocalDefId` here should be `DefIndex`, + /// as it is from external crate. + 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. @@ -530,6 +536,10 @@ impl<'a, 'tcx> BlobDecoder for MetadataDecodeContext<'a, 'tcx> { fn decode_byte_symbol(&mut self) -> ByteSymbol { self.blob_decoder.decode_byte_symbol() } + + fn decode_local_def_id(&mut self) -> LocalDefId { + self.blob_decoder.decode_local_def_id() + } } impl<'a> BlobDecoder for BlobDecodeContext<'a> { @@ -551,6 +561,15 @@ impl<'a> BlobDecoder for BlobDecodeContext<'a> { |opaque| ByteSymbol::intern(opaque.read_byte_str()), ) } + + /// The `LocalDefId` decoding logic is different from the default + /// one (which is decoding local def id as default def id with crate num), + /// because during metadata encoding/decoding `LocalDefId` is used as a wrapper + /// around `DefIndex` with a guarantee that it is local, that is why we can + /// create `LocalDefId` straight from a def index. + fn decode_local_def_id(&mut self) -> LocalDefId { + LocalDefId { local_def_index: Decodable::decode(self) } + } } impl<'a, 'tcx> Decodable> for SpanData { @@ -831,7 +850,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 +905,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 +1225,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 +1283,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 +1292,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 +1307,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 +1320,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 +1332,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 +1363,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 +1372,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 +1426,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 +1457,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 +1507,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 +1545,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, 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 { &[] } @@ -1583,7 +1603,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( @@ -1593,7 +1616,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>( @@ -2107,8 +2130,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; } @@ -2138,10 +2163,10 @@ impl CrateMetadata { // Translate a DefId from the current compilation environment to a DefId // for an external crate. - fn reverse_translate_def_id(&self, did: DefId) -> Option { + fn reverse_translate_def_id(&self, did: DefId) -> Option { for (local, &global) in self.cnum_map.iter_enumerated() { if global == did.krate { - return Some(DefId { krate: local, index: did.index }); + return Some(RawDefId { krate: local.as_u32(), index: did.index.as_u32() }); } } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 03493d0e1b6a9..d17d3333399c7 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -155,13 +155,17 @@ 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()); + /// This function panics as we need to remap only local def indices, however + /// we can not determine if the def index is local given only def index. + /// In metadata `LocalDefId` is used instead of `DefIndex` as with this wrapper + /// around def index we certainly know that it came from local crate. + 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) { @@ -224,6 +228,15 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { this.emit_byte_str(byte_sym.as_byte_str()) }); } + + /// We override `encode` function for `LocalDefId` as its default + /// encoding logic is `self.encode_def_id(def_id.to_def_id())` + /// meaning that the crate num will also be encoded, however as in metadata we use + /// `LocalDefId` as a wrapper around def index we want to encode + /// def index only, so we override the encode implementation. + fn encode_local_def_id(&mut self, def_id: LocalDefId) { + self.emit_u32(def_id.local_def_index.as_u32()); + } } fn bytes_needed(n: usize) -> usize { @@ -380,6 +393,23 @@ impl<'a, 'tcx> Encodable> for [u8] { } } +// The specialization is used as we want to convert parent (which is `DefIndex`) +// to `LocalDefId` only during metadata encoding. +// There are other solutions for this problem which can be implemented if needed: +// move `DefKey` encoding logic to `SpanEncoder` and `SpanDecoder` (or `BlobDecoder`), +// as it is done with `LocalDefId`; the other option is to introduce `LocalDefKey` type +// which will have parent of type `Option` not `Option`. +impl Encodable> for DefKey { + /// We specialize encoding logic of `DefKey` during metadata encoding, + /// as `DefKey` has a field `parent` of type `DefIndex` whose encoding will + /// cause panic during metadata encoding. So we remap it to local def index + /// as def keys during metadata encoding are all local. + 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<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; @@ -409,19 +439,19 @@ 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); + $self.$tables.$table.set_some($def_id, lazy); }}; } macro_rules! record_some { ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => { - $self.$tables.$table.set_some($def_id.index, $value) + $self.$tables.$table.set_some($def_id, $value) }; } macro_rules! record_value { ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => { - $self.$tables.$table.set($def_id.index, $value) + $self.$tables.$table.set($def_id, $value) }; } @@ -431,7 +461,7 @@ macro_rules! record_array { ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ let value = $value; let lazy = $self.lazy_array(value); - $self.$tables.$table.set_some($def_id.index, lazy); + $self.$tables.$table.set_some($def_id, lazy); }}; } @@ -439,7 +469,7 @@ macro_rules! record_defaulted_array { ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ let value = $value; let lazy = $self.lazy_array(value); - $self.$tables.$table.set($def_id.index, lazy); + $self.$tables.$table.set($def_id, lazy); }}; } @@ -536,14 +566,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { { let def_key = defs.def_key(def_id); let def_path_hash = defs.def_path_hash(def_id); - 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_id = LocalDefId { local_def_index: def_index }.to_def_id(); + let def_id = LocalDefId { local_def_index: def_index }; 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()) } @@ -1445,14 +1474,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .iter() .filter(|attr| analyze_attr(*attr, &mut state)); - record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter); + record_array!(self.tables.attributes[def_id] <- attr_iter); let mut attr_flags = AttrFlags::empty(); if state.is_doc_hidden { attr_flags |= AttrFlags::IS_DOC_HIDDEN; } - record_value!(self.tables.attr_flags[def_id.to_def_id()] <- attr_flags) + record_value!(self.tables.attr_flags[def_id] <- attr_flags) } fn encode_def_ids(&mut self) { @@ -1469,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); - record_some!(self.tables.def_kind[def_id] <- def_kind); + record_some!(self.tables.def_kind[local_id] <- def_kind); // The `DefCollector` will sometimes create unnecessary `DefId`s // for trivial const arguments which are directly lowered to @@ -1490,132 +1519,129 @@ 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_some_lazy!(self.tables.default_fields[def_id] <- anon.def_id.to_def_id()); + record_some_lazy!(self.tables.default_fields[local_id] <- anon.def_id.to_def_id()); } if should_encode_span(def_kind) { let def_span = tcx.def_span(local_id); - record_some_lazy!(self.tables.def_span[def_id] <- def_span); + record_some_lazy!(self.tables.def_span[local_id] <- def_span); } if should_encode_attrs(def_kind) { self.encode_attrs(local_id); } if should_encode_expn_that_defined(def_kind) { - record_some_lazy!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); + record_some_lazy!(self.tables.expn_that_defined[local_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_some_lazy!(self.tables.def_ident_span[def_id] <- ident_span); + record_some_lazy!(self.tables.def_ident_span[local_id] <- ident_span); } if def_kind.has_codegen_attrs() { - record_some_lazy!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); + record_some_lazy!(self.tables.codegen_fn_attrs[local_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_some_lazy!(self.tables.visibility[def_id] <- vis); + let vis = + self.tcx.local_visibility(local_id).map_id(|mod_id| mod_id.to_local_def_id()); + record_some_lazy!(self.tables.visibility[local_id] <- vis); } if should_encode_stability(def_kind) { - self.encode_stability(def_id); - self.encode_const_stability(def_id); - self.encode_default_body_stability(def_id); - self.encode_deprecation(def_id); + self.encode_stability(local_id); + self.encode_const_stability(local_id); + self.encode_default_body_stability(local_id); + self.encode_deprecation(local_id); } if should_encode_variances(tcx, def_id, def_kind) { let v = self.tcx.variances_of(def_id); - record_array!(self.tables.variances_of[def_id] <- v); + record_array!(self.tables.variances_of[local_id] <- v); } if should_encode_fn_sig(def_kind) { - record_some_lazy!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); + record_some_lazy!(self.tables.fn_sig[local_id] <- tcx.fn_sig(def_id)); } if should_encode_generics(def_kind) { let g = tcx.generics_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)); + record_some_lazy!(self.tables.generics_of[local_id] <- g); + record_some_lazy!(self.tables.explicit_clauses_of[local_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); + record_defaulted_array!(self.tables.inferred_outlives_of[local_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_some_lazy!(self.tables.const_param_default[param.def_id] <- default); + record_some_lazy!(self.tables.const_param_default[param.def_id.expect_local()] <- default); } } } if tcx.is_conditionally_const(def_id) { - record_some_lazy!(self.tables.const_conditions[def_id] <- self.tcx.const_conditions(def_id)); + record_some_lazy!(self.tables.const_conditions[local_id] <- self.tcx.const_conditions(def_id)); } if should_encode_type(tcx, local_id, def_kind) { - record_some_lazy!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); + record_some_lazy!(self.tables.type_of[local_id] <- self.tcx.type_of(def_id)); } if should_encode_constness(def_kind) { let constness = self.tcx.constness(def_id); - record_value!(self.tables.constness[def_id] <- constness) + record_value!(self.tables.constness[local_id] <- constness) } if let DefKind::Fn | DefKind::AssocFn = def_kind { let asyncness = tcx.asyncness(def_id); - record_value!(self.tables.asyncness[def_id] <- asyncness); - record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id)); + record_value!(self.tables.asyncness[local_id] <- asyncness); + record_array!(self.tables.fn_arg_idents[local_id] <- tcx.fn_arg_idents(def_id)); } if let Some(name) = tcx.intrinsic(def_id) { - record_some_lazy!(self.tables.intrinsic[def_id] <- name); + record_some_lazy!(self.tables.intrinsic[local_id] <- name); } if let DefKind::TyParam | DefKind::Trait = def_kind { let default = self.tcx.object_lifetime_default(def_id); - record_some_lazy!(self.tables.object_lifetime_default[def_id] <- default); + record_some_lazy!(self.tables.object_lifetime_default[local_id] <- default); } if let DefKind::Trait = def_kind { - 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] <- + record_some_lazy!(self.tables.trait_def[local_id] <- self.tcx.trait_def(def_id)); + record_defaulted_array!(self.tables.explicit_super_clauses_of[local_id] <- self.tcx.explicit_super_clauses_of(def_id).skip_binder()); - record_defaulted_array!(self.tables.explicit_implied_clauses_of[def_id] <- + record_defaulted_array!(self.tables.explicit_implied_clauses_of[local_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)); + record_array!(self.tables.module_children_non_reexports[local_id] <- + 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] + record_defaulted_array!(self.tables.explicit_implied_const_bounds[local_id] <- self.tcx.explicit_implied_const_bounds(def_id).skip_binder()); } } if let DefKind::TraitAlias = def_kind { - 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] <- + record_some_lazy!(self.tables.trait_def[local_id] <- self.tcx.trait_def(def_id)); + record_defaulted_array!(self.tables.explicit_super_clauses_of[local_id] <- self.tcx.explicit_super_clauses_of(def_id).skip_binder()); - record_defaulted_array!(self.tables.explicit_implied_clauses_of[def_id] <- + record_defaulted_array!(self.tables.explicit_implied_clauses_of[local_id] <- self.tcx.explicit_implied_clauses_of(def_id).skip_binder()); } if let DefKind::Trait | DefKind::Impl { .. } = def_kind { 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] <- + record_array!(self.tables.associated_item_or_field_def_ids[local_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 { - self.encode_info_for_assoc_item(def_id); + self.encode_info_for_assoc_item(def_id.expect_local()); } } if let DefKind::Closure | DefKind::SyntheticCoroutineBody = def_kind && let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id) { - record_value!(self.tables.coroutine_kind[def_id] <- Some(coroutine_kind)) + record_value!(self.tables.coroutine_kind[local_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); - record_some!(self.tables.coroutine_for_closure[def_id] <- coroutine_for_closure.into()); + record_some!(self.tables.coroutine_for_closure[local_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) { 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() + self.tables.coroutine_by_move_body_def_id[coroutine_for_closure.expect_local()] <- id.into() ) } } @@ -1623,7 +1649,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { if !self.tcx.is_foreign_item(def_id) { match self.tcx.eval_static_initializer(def_id) { Ok(data) => { - record_some_lazy!(self.tables.eval_static_initializer[def_id] <- data) + record_some_lazy!(self.tables.eval_static_initializer[local_id] <- data) } Err(err) => match err { interpret::ErrorHandled::Reported(_, _) => { @@ -1648,65 +1674,64 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.encode_info_for_macro(local_id); } if let DefKind::TyAlias = def_kind { - record_value!(self.tables.type_alias_is_checked[def_id] <- self.tcx.type_alias_is_checked(def_id)); + record_value!(self.tables.type_alias_is_checked[local_id] <- self.tcx.type_alias_is_checked(def_id)); if self.tcx.type_alias_is_checked(def_id) { - record_some_lazy!(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[local_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_some_lazy!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id)); - self.encode_precise_capturing_args(def_id); + self.encode_explicit_item_bounds(local_id); + self.encode_explicit_item_self_bounds(local_id); + record_some_lazy!(self.tables.opaque_ty_origin[local_id] <- self.tcx.opaque_ty_origin(def_id)); + self.encode_precise_capturing_args(local_id); if tcx.is_conditionally_const(def_id) { - record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id] + record_defaulted_array!(self.tables.explicit_implied_const_bounds[local_id] <- tcx.explicit_implied_const_bounds(def_id).skip_binder()); } - record_some_lazy!(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[local_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_some_lazy!(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[local_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } ty::AssocContainer::InherentImpl => { - record_some_lazy!(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[local_id] <- tcx.args_known_to_outlive_alias_params(def_id)); } ty::AssocContainer::TraitImpl(_) => {} } } if let DefKind::AnonConst = def_kind { - record_some_lazy!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id)); + record_some_lazy!(self.tables.anon_const_kind[local_id] <- self.tcx.anon_const_kind(def_id)); } if let DefKind::Const | DefKind::AssocConst = def_kind { - record_some_lazy!(self.tables.const_of_item[def_id] <- self.tcx.const_of_item(def_id)); + record_some_lazy!(self.tables.const_of_item[local_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_some_lazy!(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[local_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_some_lazy!(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[local_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 + record_defaulted_array!(self.tables.inherent_impls[*def_id] <- impls.iter().map(|def_id| { + def_id.expect_local() })); } for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions { - record_some_lazy!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map); + record_some_lazy!(self.tables.doc_link_resolutions[def_id.to_local_def_id()] <- res_map); } for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope { - record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits); + record_array!(self.tables.doc_link_traits_in_scope[def_id.to_local_def_id()] <- traits); } } @@ -1729,15 +1754,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_some_lazy!(self.tables.repr_options[def_id] <- adt_def.repr()); + record_some_lazy!(self.tables.repr_options[local_def_id] <- adt_def.repr()); let params_in_repr = self.tcx.params_in_repr(def_id); - record_some_lazy!(self.tables.params_in_repr[def_id] <- params_in_repr); + record_some_lazy!(self.tables.params_in_repr[local_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)); + record_array!(self.tables.module_children_non_reexports[local_def_id] <- + 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); @@ -1746,46 +1771,47 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } for (idx, variant) in adt_def.variants().iter_enumerated() { + let variant_id = variant.def_id.expect_local(); + 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_some_lazy!(self.tables.variant_data[variant.def_id] <- data); + record_some_lazy!(self.tables.variant_data[variant_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 + record_array!(self.tables.associated_item_or_field_def_ids[variant_id] <- variant.fields.iter().map(|f| { + f.did.expect_local() })); for field in &variant.fields { - record_value!(self.tables.safety[field.did] <- field.safety); + let field_id = field.did.expect_local(); + record_value!(self.tables.safety[field_id] <- field.safety); record_some_lazy!( - self.tables.mut_restriction[field.did] <- field.mut_restriction + self.tables.mut_restriction[field_id] <- field.mut_restriction ); } 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_some_lazy!(self.tables.fn_sig[variant.def_id] <- fn_sig); + record_some_lazy!(self.tables.fn_sig[variant_id] <- fn_sig); } } if let Some(destructor) = tcx.adt_destructor(local_def_id) { - record_some_lazy!(self.tables.adt_destructor[def_id] <- destructor); + record_some_lazy!(self.tables.adt_destructor[local_def_id] <- destructor); } if let Some(destructor) = tcx.adt_async_destructor(local_def_id) { - record_some_lazy!(self.tables.adt_async_destructor[def_id] <- destructor); + record_some_lazy!(self.tables.adt_async_destructor[local_def_id] <- destructor); } } #[instrument(level = "debug", skip(self))] fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) { let tcx = self.tcx; - let def_id = local_def_id.to_def_id(); // If we are encoding a proc-macro crates, `encode_info_for_mod` will // only ever get called for the crate root. We still want to encode @@ -1794,15 +1820,15 @@ 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_some_lazy!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); + record_some_lazy!(self.tables.expn_that_defined[local_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] <- + record_array!(self.tables.module_children_non_reexports[local_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] <- + record_defaulted_array!(self.tables.module_children_reexports[local_def_id] <- module_children.iter().filter(|child| !child.reexport_chain.is_empty())); let ambig_module_children = tcx @@ -1810,25 +1836,25 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .ambig_module_children .get(&local_def_id) .map_or_default(|v| &v[..]); - record_defaulted_array!(self.tables.ambig_module_children[def_id] <- + record_defaulted_array!(self.tables.ambig_module_children[local_def_id] <- ambig_module_children); } } - fn encode_explicit_item_bounds(&mut self, def_id: DefId) { + fn encode_explicit_item_bounds(&mut self, def_id: LocalDefId) { debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id); let bounds = self.tcx.explicit_item_bounds(def_id).skip_binder(); record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds); } - fn encode_explicit_item_self_bounds(&mut self, def_id: DefId) { + fn encode_explicit_item_self_bounds(&mut self, def_id: LocalDefId) { debug!("EncodeContext::encode_explicit_item_self_bounds({:?})", def_id); let bounds = self.tcx.explicit_item_self_bounds(def_id).skip_binder(); record_defaulted_array!(self.tables.explicit_item_self_bounds[def_id] <- bounds); } #[instrument(level = "debug", skip(self))] - fn encode_info_for_assoc_item(&mut self, def_id: DefId) { + fn encode_info_for_assoc_item(&mut self, def_id: LocalDefId) { let tcx = self.tcx; let item = tcx.associated_item(def_id); @@ -1860,7 +1886,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_precise_capturing_args(&mut self, def_id: DefId) { + fn encode_precise_capturing_args(&mut self, def_id: LocalDefId) { let Some(precise_capturing_args) = self.tcx.rendered_precise_capturing_args(def_id) else { return; }; @@ -1885,53 +1911,53 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EntryBuilder::encode_mir({:?})", def_id); if encode_opt { - record_some_lazy!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id)); + record_some_lazy!(self.tables.optimized_mir[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_value!(self.tables.cross_crate_inlinable[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()] + record_some_lazy!(self.tables.closure_saved_names_of_captured_variables[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_some_lazy!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); + record_some_lazy!(self.tables.mir_coroutine_witnesses[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_some_lazy!(self.tables.trivial_const[def_id.to_def_id()] <- (val, ty)); + record_some_lazy!(self.tables.trivial_const[def_id] <- (val, ty)); } else { is_trivial = false; - record_some_lazy!(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] <- 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_some_lazy!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); + record_some_lazy!(self.tables.thir_abstract_const[def_id] <- abstract_const); } if should_encode_const(tcx.def_kind(def_id)) { let qualifs = tcx.mir_const_qualif(def_id); - record_some_lazy!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); + record_some_lazy!(self.tables.mir_const_qualif[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_some_lazy!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); + record_some_lazy!(self.tables.rendered_const[def_id] <- const_data); } } } if !is_trivial { - record_some_lazy!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); + record_some_lazy!(self.tables.promoted_mir[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_some_lazy!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); + record_some_lazy!(self.tables.mir_coroutine_witnesses[def_id] <- witnesses); } } @@ -1944,7 +1970,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { { for &local_def_id in tcx.mir_keys(()) { if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) { - record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <- + record_array!(self.tables.deduced_param_attrs[local_def_id] <- self.tcx.deduced_param_attrs(local_def_id.to_def_id())); } } @@ -1952,7 +1978,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } #[instrument(level = "debug", skip(self))] - fn encode_stability(&mut self, def_id: DefId) { + fn encode_stability(&mut self, def_id: LocalDefId) { // The query lookup can take a measurable amount of time in crates with many items. Check if // 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 { @@ -1963,7 +1989,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } #[instrument(level = "debug", skip(self))] - fn encode_const_stability(&mut self, def_id: DefId) { + fn encode_const_stability(&mut self, def_id: LocalDefId) { // The query lookup can take a measurable amount of time in crates with many items. Check if // 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 { @@ -1974,7 +2000,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } #[instrument(level = "debug", skip(self))] - fn encode_default_body_stability(&mut self, def_id: DefId) { + fn encode_default_body_stability(&mut self, def_id: LocalDefId) { // The query lookup can take a measurable amount of time in crates with many items. Check if // 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 { @@ -1985,8 +2011,8 @@ 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) { + fn encode_deprecation(&mut self, def_id: LocalDefId) { + if let Some(depr) = self.tcx.lookup_deprecation(def_id.to_def_id()) { record_some_lazy!(self.tables.lookup_deprecation_entry[def_id] <- depr); } } @@ -1996,8 +2022,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let tcx = self.tcx; let (_, macro_def, _) = tcx.hir_expect_item(def_id).expect_macro(); - 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); + record_value!(self.tables.is_macro_rules[def_id] <- macro_def.macro_rules); + record_some_lazy!(self.tables.macro_definition[def_id] <- &*macro_def.body); } fn encode_native_libraries(&mut self) -> LazyArray { @@ -2044,25 +2070,25 @@ 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); - 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_some_lazy!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); + record_some!(self.tables.def_kind[CRATE_DEF_ID] <- DefKind::Mod); + record_some_lazy!(self.tables.def_span[CRATE_DEF_ID] <- tcx.def_span(CRATE_DEF_ID.to_def_id())); + self.encode_attrs(CRATE_DEF_ID); + let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|mod_id| mod_id.to_local_def_id()); + record_some_lazy!(self.tables.visibility[CRATE_DEF_ID] <- vis); if let Some(stability) = stability { - record_some_lazy!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); + record_some_lazy!(self.tables.lookup_stability[CRATE_DEF_ID] <- stability); } - self.encode_deprecation(LOCAL_CRATE.as_def_id()); + self.encode_deprecation(CRATE_DEF_ID); if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_MOD_ID) { - record_some_lazy!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map); + record_some_lazy!(self.tables.doc_link_resolutions[CRATE_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); + record_array!(self.tables.doc_link_traits_in_scope[CRATE_DEF_ID] <- traits); } let mut macros = vec![]; @@ -2100,21 +2126,20 @@ 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, 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(); - record_some!(self.tables.def_kind[def_id] <- DefKind::Macro(macro_kind.into())); + record_some!(self.tables.def_kind[id] <- DefKind::Macro(macro_kind.into())); self.encode_attrs(id); - 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); + record_some_lazy!(self.tables.def_keys[id] <- def_key); + record_some_lazy!(self.tables.def_ident_span[id] <- span); + record_some_lazy!(self.tables.def_span[id] <- span); + record_some_lazy!(self.tables.visibility[id] <- ty::Visibility::Public); if let Some(stability) = stability { - record_some_lazy!(self.tables.lookup_stability[def_id] <- stability); + record_some_lazy!(self.tables.lookup_stability[id] <- stability); } } @@ -2214,33 +2239,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 { @@ -2249,18 +2277,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. @@ -2268,24 +2296,24 @@ 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() { let DefKind::Impl { of_trait } = tcx.def_kind(id.owner_id) else { continue; }; - let def_id = id.owner_id.to_def_id(); + let local_id = id.owner_id.def_id; if of_trait { - let header = tcx.impl_trait_header(def_id); - record_some_lazy!(self.tables.impl_trait_header[def_id] <- header); + let header = tcx.impl_trait_header(local_id); + record_some_lazy!(self.tables.impl_trait_header[local_id] <- header); let impl_is_fully_generic_for_reflection = - tcx.impl_is_fully_generic_for_reflection(def_id); + tcx.impl_is_fully_generic_for_reflection(local_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)); + record_value!(self.tables.impl_is_fully_generic_for_reflection[local_id] <- impl_is_fully_generic_for_reflection); + record_value!(self.tables.defaultness[local_id] <- tcx.defaultness(local_id)); let trait_ref = header.trait_ref.instantiate_identity().skip_norm_wip(); let simplified_self_ty = fast_reject::simplify_type( @@ -2296,20 +2324,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((local_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) + if let Ok(mut an) = trait_def.ancestors(tcx, local_id.to_def_id()) && let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) { - record_some!(self.tables.impl_parent[def_id] <- parent.into()); + record_some!(self.tables.impl_parent[local_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_some_lazy!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info); + let coerce_unsized_info = tcx.coerce_unsized_info(local_id).unwrap(); + record_some_lazy!(self.tables.coerce_unsized_info[local_id] <- coerce_unsized_info); } } } @@ -2317,7 +2345,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.into(), impls: self.lazy_array(&impls), }) .collect(); @@ -2337,24 +2365,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 f7a437043dade..eef513382b57e 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; @@ -157,8 +160,6 @@ struct LazyTable { _marker: PhantomData T>, } -type LazyTableSingleIdx = LazyTable; - impl LazyTable { fn from_position_and_encoded_size( position: NonZero, @@ -205,16 +206,16 @@ enum LazyState { Previous(NonZero), } -type SyntaxContextTable = LazyTableSingleIdx>>; -type ExpnDataTable = LazyTableSingleIdx>>; -type ExpnHashTable = LazyTableSingleIdx>>; +type SyntaxContextTable = LazyTable>>; +type ExpnDataTable = LazyTable>>; +type ExpnHashTable = LazyTable>>; #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct ProcMacroData { - proc_macro_decls_static: DefIndex, + proc_macro_decls_static: LazyValue, stability: Option, - macros: LazyArray<(DefIndex, LazyValue)>, - proc_macro_quoted_spans: LazyTableSingleIdx>>, + macros: LazyArray<(LocalDefId, LazyValue)>, + proc_macro_quoted_spans: LazyTable>>, } #[derive(MetadataEncodable, LazyDecodable)] @@ -284,15 +285,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, @@ -301,8 +302,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)>, @@ -312,7 +313,7 @@ pub(crate) struct CrateRoot { def_path_hash_map: LazyValue>, - source_map: LazyTableSingleIdx>>, + source_map: LazyTable>>, target_modifiers: LazyArray, denied_partial_mitigations: LazyArray, @@ -342,7 +343,7 @@ pub(crate) struct CrateRootUnhashed { /// On-disk representation of `DefId`. /// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk /// representation and the compilation session. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Hash, PartialEq, Eq, Encodable, BlobDecodable)] pub(crate) struct RawDefId { krate: u32, index: u32, @@ -378,32 +379,32 @@ pub(crate) struct CrateDep { #[derive(MetadataEncodable, LazyDecodable)] pub(crate) struct TraitImpls { - trait_id: (u32, DefIndex), - impls: LazyArray<(DefIndex, Option)>, + trait_id: RawDefId, + 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 { @@ -419,106 +420,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>, - 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 +527,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_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 738521b2005e0..e6a6d2079cc3e 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -688,6 +688,10 @@ impl<'a, 'tcx> BlobDecoder for CacheDecoder<'a, 'tcx> { fn decode_def_index(&mut self) -> DefIndex { panic!("trying to decode `DefIndex` outside the context of a `DefId`") } + + fn decode_local_def_id(&mut self) -> LocalDefId { + self.decode_def_id().expect_local() + } } /// Implements [`Decodable`] for `&'tcx T`, where [`T: RefDecodable`](RefDecodable). diff --git a/compiler/rustc_monomorphize/src/offload/manifest.rs b/compiler/rustc_monomorphize/src/offload/manifest.rs index bf72c97bc14fa..a6f5d1228082e 100644 --- a/compiler/rustc_monomorphize/src/offload/manifest.rs +++ b/compiler/rustc_monomorphize/src/offload/manifest.rs @@ -290,6 +290,10 @@ impl<'a, 'tcx> BlobDecoder for OffloadManifestDecoder<'a, 'tcx> { let v = self.read_u32(); rustc_span::def_id::DefIndex::from_u32(v) } + + fn decode_local_def_id(&mut self) -> rustc_hir::def_id::LocalDefId { + self.decode_def_id().expect_local() + } } impl<'a, 'tcx> SpanDecoder for OffloadManifestDecoder<'a, 'tcx> { diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index b2b5d2aa22bc1..e0143923fb5d5 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -12,7 +12,7 @@ use rustc_index::Idx; use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash}; use rustc_serialize::{Decodable, Encodable}; -use crate::{SpanDecoder, SpanEncoder, Symbol}; +use crate::{BlobDecoder, SpanEncoder, Symbol}; pub type StableCrateIdMap = indexmap::IndexMap>; @@ -399,14 +399,14 @@ impl fmt::Debug for LocalDefId { } impl Encodable for LocalDefId { - fn encode(&self, s: &mut E) { - self.to_def_id().encode(s); + fn encode(&self, e: &mut E) { + e.encode_local_def_id(*self); } } -impl Decodable for LocalDefId { +impl Decodable for LocalDefId { fn decode(d: &mut D) -> LocalDefId { - DefId::decode(d).expect_local() + d.decode_local_def_id() } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index ddec29eca6517..40e0f29bfdcbc 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1354,6 +1354,10 @@ pub trait SpanEncoder: Encoder { fn encode_crate_num(&mut self, crate_num: CrateNum); fn encode_def_index(&mut self, def_index: DefIndex); fn encode_def_id(&mut self, def_id: DefId); + + fn encode_local_def_id(&mut self, def_id: LocalDefId) { + self.encode_def_id(def_id.to_def_id()); + } } impl SpanEncoder for FileEncoder<'_> { @@ -1409,11 +1413,11 @@ impl SpanEncoder for MemEncoder { } fn encode_expn_id(&mut self, _expn_id: ExpnId) { - panic!("cannot encode `ExpnId` with `FileEncoder`"); + panic!("cannot encode `ExpnId` with `MemEncoder`"); } fn encode_syntax_context(&mut self, _syntax_context: SyntaxContext) { - panic!("cannot encode `SyntaxContext` with `FileEncoder`"); + panic!("cannot encode `SyntaxContext` with `MemEncoder`"); } fn encode_crate_num(&mut self, crate_num: CrateNum) { @@ -1421,7 +1425,7 @@ impl SpanEncoder for MemEncoder { } fn encode_def_index(&mut self, _def_index: DefIndex) { - panic!("cannot encode `DefIndex` with `FileEncoder`"); + panic!("cannot encode `DefIndex` with `MemEncoder`"); } fn encode_def_id(&mut self, def_id: DefId) { @@ -1488,6 +1492,14 @@ pub trait BlobDecoder: Decoder { fn decode_symbol(&mut self) -> Symbol; fn decode_byte_symbol(&mut self) -> ByteSymbol; fn decode_def_index(&mut self) -> DefIndex; + + // This function is placed here (not in a `SpanDecoder`), + // as during metadata decoding `LocalDefId` is just a wrapper + // around `DefIndex`, so it can be decoded without extra knowledge + // like crate remapping. + fn decode_local_def_id(&mut self) -> LocalDefId { + panic!("override in a concrete decoder") + } } /// This trait is used to allow decoder specific encodings of certain types.