Skip to content
Draft
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
2 changes: 1 addition & 1 deletion compiler/rustc_hir_id/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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>,

@aerooneqq aerooneqq Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

By the way that is the case where maybe we can use LocalDefIndex outside of metadata and it makes sense.

View changes since the review

Expand Down
45 changes: 28 additions & 17 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<(u32, u32), LazyArray<(LocalDefIndex, Option<SimplifiedType>)>>,
/// 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<LocalDefIndex>>,
/// 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 @@ -189,7 +189,7 @@ pub(super) trait LazyDecoder: BlobDecoder {
self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len))
}

fn read_lazy_table<I, T>(&mut self, width: usize, len: usize) -> LazyTable<I, T> {
fn read_lazy_table<Ie, Id, T>(&mut self, width: usize, len: usize) -> LazyTable<Ie, Id, T> {
self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, width, len))
}

Expand Down Expand Up @@ -667,7 +667,7 @@ impl<D: LazyDecoder, T> Decodable<D> for LazyArray<T> {
}
}

impl<I: Idx, D: LazyDecoder, T> Decodable<D> for LazyTable<I, T> {
impl<Ie: Idx, Id: Idx, D: LazyDecoder, T> Decodable<D> for LazyTable<Ie, Id, T> {
fn decode(decoder: &mut D) -> Self {
let width = decoder.read_usize();
let len = decoder.read_usize();
Expand Down Expand Up @@ -831,7 +831,7 @@ impl MetadataBlob {
out,
"{} = crate{}",
lang_item.name(),
DefPath::make(LOCAL_CRATE, id, |parent| root
DefPath::make(LOCAL_CRATE, id.into(), |parent| root
.tables
.def_keys
.get(self, parent)
Expand Down Expand Up @@ -869,11 +869,12 @@ impl MetadataBlob {
fn print_item(
blob: &MetadataBlob,
out: &mut dyn io::Write,
item: DefIndex,
item: LocalDefIndex,
indent: usize,
) -> io::Result<()> {
let root = blob.get_root();

let item = item.into();
let def_kind = root.tables.def_kind.get(blob, item).unwrap();
let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob);
#[allow(rustc::symbol_intern_string_literal)]
Expand All @@ -891,7 +892,7 @@ impl MetadataBlob {
|index| {
format!(
"crate{}",
DefPath::make(LOCAL_CRATE, index, |parent| root
DefPath::make(LOCAL_CRATE, index.into(), |parent| root
.tables
.def_keys
.get(blob, parent)
Expand Down Expand Up @@ -925,7 +926,7 @@ impl MetadataBlob {
Ok(())
}

print_item(self, out, CRATE_DEF_INDEX, 0)?;
print_item(self, out, CRATE_LOCAL_DEF_INDEX, 0)?;

write!(out, "\n")?;
}
Expand Down Expand Up @@ -1021,7 +1022,7 @@ impl CrateMetadata {
.macros
.decode((self, tcx))
.enumerate()
.find(|(_pos, (i, _))| *i == id)
.find(|(_pos, (i, _))| i.as_u32() == id.as_u32())
.unwrap();
(self.raw_proc_macros.unwrap()[pos], kind.decode((self, tcx)))
}
Expand Down Expand Up @@ -1177,6 +1178,7 @@ impl CrateMetadata {
.expect("variants are not encoded for an enum")
.decode((self, tcx))
.filter_map(|index| {
let index = index.into();
let kind = self.def_kind(index);
match kind {
DefKind::Ctor(..) => None,
Expand Down Expand Up @@ -1276,7 +1278,7 @@ impl CrateMetadata {
.root
.stripped_cfg_items
.decode((self, tcx))
.map(|item| item.map_scope_id(|index| DefId { krate: cnum, index }));
.map(|item| item.map_scope_id(|index| Self::create_def_id(cnum, index)));
tcx.arena.alloc_from_iter(item_names)
}

Expand Down Expand Up @@ -1320,7 +1322,8 @@ impl CrateMetadata {
fake_doc_items
}

fn get_mod_child(&self, tcx: TyCtxt<'_>, id: DefIndex) -> ModChild {
fn get_mod_child(&self, tcx: TyCtxt<'_>, id: LocalDefIndex) -> ModChild {
let id = id.into();
let ident = self.item_ident(tcx, id);
let res = Res::Def(self.def_kind(id), self.local_def_id(id));
let vis = self.get_visibility(tcx, id);
Expand Down Expand Up @@ -1523,7 +1526,7 @@ 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 &[],
};

Expand All @@ -1544,7 +1547,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:?}"))
Expand Down Expand Up @@ -2105,8 +2110,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 |(index, _)| Self::create_def_id(krate, index))
{
yield def_id;
}
Expand All @@ -2130,8 +2137,12 @@ impl CrateMetadata {
self.root.tables.def_keys.size()
}

fn local_def_id(&self, index: DefIndex) -> DefId {
DefId { krate: self.cnum, index }
fn local_def_id(&self, index: impl Into<DefIndex>) -> DefId {
DefId { krate: self.cnum, index: index.into() }
}

fn create_def_id(krate: CrateNum, index: LocalDefIndex) -> DefId {
DefId { krate, index: index.into() }
}

// Translate a DefId from the current compilation environment to a DefId
Expand Down
Loading
Loading