Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions compiler/rustc_hir_id/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SortedMap<Hash64, DefIndex>>,
pub after_parallel_alloc: Option<SortedMap<Hash64, LocalDefId>>,
}

impl DefPathToIndexMap {
#[inline]
pub fn get(&self, hash: Hash64) -> Option<DefIndex> {
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`,
Expand All @@ -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)
}
}
}
Expand All @@ -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<DefIndex>,
Comment thread
aerooneqq marked this conversation as resolved.
Expand All @@ -110,6 +113,13 @@ pub struct DefKey {
pub disambiguated_data: DisambiguatedDefPathData,
}

impl<E: SpanEncoder> Encodable<E> for DefKey {
default fn encode(&self, e: &mut E) {

@petrochenkov petrochenkov Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a comment telling why specialization is used.

View changes since the review

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();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
124 changes: 69 additions & 55 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimplifiedType>)>>,
trait_impls: FxIndexMap<RawDefId, LazyArray<(LocalDefId, Option<SimplifiedType>)>>,

@petrochenkov petrochenkov Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an even worse consequence of #163321 (comment).

Unlike LocalDefIds in compiler/rustc_metadata/src/rmeta/mod.rs used during encoding, the indices here and in incoherent_impls are never actually local, they are always external.
Is it possible to convert these to LazyArrays of DefIndexes immediately (perhaps with transmute?), so that the incorrect invariant-breaking LocalDefId existed only briefly during decoding, but did not get stored into data structures?

View changes since the review

/// 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<SimplifiedType, LazyArray<DefIndex>>,
incoherent_impls: FxIndexMap<SimplifiedType, LazyArray<LocalDefId>>,
/// 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.
Expand Down Expand Up @@ -530,6 +530,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> {
Expand All @@ -551,6 +555,10 @@ impl<'a> BlobDecoder for BlobDecodeContext<'a> {
|opaque| ByteSymbol::intern(opaque.read_byte_str()),
)
}

fn decode_local_def_id(&mut self) -> LocalDefId {
LocalDefId { local_def_index: Decodable::decode(self) }
}
}

impl<'a, 'tcx> Decodable<MetadataDecodeContext<'a, 'tcx>> for SpanData {
Expand Down Expand Up @@ -831,7 +839,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)
Expand Down Expand Up @@ -886,21 +894,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: <indent$}{:?} {:?} {} {{",
Expand All @@ -915,7 +923,7 @@ impl MetadataBlob {
{
write!(out, "\n")?;
for child in children.decode(blob) {
print_item(blob, out, child, indent + 4)?;
print_item(blob, out, child.local_def_index, indent + 4)?;
}
writeln!(out, "{nil: <indent$}}}", nil = "")?;
} else {
Expand Down Expand Up @@ -1021,7 +1029,7 @@ impl CrateMetadata {
.macros
.decode((self, tcx))
.enumerate()
.find(|(_pos, (i, _))| *i == id)
.find(|(_pos, (i, _))| i.local_def_index == id)
.unwrap();
(self.raw_proc_macros.unwrap()[pos], kind.decode((self, tcx)))
}
Expand Down Expand Up @@ -1131,7 +1139,7 @@ impl CrateMetadata {

let variant_did =
if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None };
let ctor = data.ctor.map(|(kind, index)| (kind, self.local_def_id(index)));
let ctor = data.ctor.map(|(kind, id)| (kind, self.local_def_id(id.local_def_index)));

(
data.idx,
Expand Down Expand Up @@ -1176,7 +1184,8 @@ impl CrateMetadata {
.get(self, item_id)
.expect("variants are not encoded for an enum")
.decode((self, tcx))
.filter_map(|index| {
.filter_map(|id| {
let index = id.local_def_index;
let kind = self.def_kind(index);
match kind {
DefKind::Ctor(..) => None,
Expand Down Expand Up @@ -1205,7 +1214,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 {
Expand Down Expand Up @@ -1263,7 +1272,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)),
)
}

Expand All @@ -1272,11 +1281,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)
}

Expand All @@ -1287,8 +1296,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)
})
Expand All @@ -1300,8 +1309,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);
}

Expand All @@ -1312,8 +1321,8 @@ impl CrateMetadata {
fn get_fake_doc_items(&self, tcx: TyCtxt<'_>) -> Vec<DefId> {
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);
}

Expand Down Expand Up @@ -1343,7 +1352,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 {
Expand All @@ -1352,7 +1361,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);
Expand Down Expand Up @@ -1406,7 +1415,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 {
Expand Down Expand Up @@ -1437,7 +1446,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,
}
Expand Down Expand Up @@ -1487,25 +1496,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<Item = DefId> {
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<Item = DefId> {
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 {
&[]
}
Expand All @@ -1523,16 +1534,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) => RawDefId::from(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 {
&[]
}
Expand Down Expand Up @@ -1583,7 +1592,10 @@ impl CrateMetadata {
}

fn get_exportable_items(&self, tcx: TyCtxt<'_>) -> impl Iterator<Item = DefId> {
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(
Expand All @@ -1593,7 +1605,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>(
Expand Down Expand Up @@ -2107,8 +2119,10 @@ impl CrateMetadata {
) -> impl Iterator<Item = DefId> {
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;
}
Expand Down Expand Up @@ -2138,10 +2152,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<DefId> {
fn reverse_translate_def_id(&self, did: DefId) -> Option<RawDefId> {
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() });
}
}

Expand Down
Loading
Loading