Skip to content

Commit 85e355e

Browse files
committed
Auto merge of #80919 - cjgillot:defkey-span, r=oli-obk
Generate metadata by iterating on DefId instead of traversing the HIR tree 1/N Sample from #80347.
2 parents 446cbc9 + 97ee7c7 commit 85e355e

File tree

12 files changed

+305
-265
lines changed

12 files changed

+305
-265
lines changed

compiler/rustc_hir/src/definitions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ impl Definitions {
419419
pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) {
420420
self.parent_modules_of_macro_defs.insert(expn_id, module);
421421
}
422+
423+
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
424+
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
425+
}
422426
}
423427

424428
#[derive(Copy, Clone, PartialEq, Debug)]

compiler/rustc_metadata/src/rmeta/decoder.rs

+24-54
Original file line numberDiff line numberDiff line change
@@ -618,43 +618,6 @@ impl MetadataBlob {
618618
}
619619
}
620620

621-
impl EntryKind {
622-
fn def_kind(&self) -> DefKind {
623-
match *self {
624-
EntryKind::AnonConst(..) => DefKind::AnonConst,
625-
EntryKind::Const(..) => DefKind::Const,
626-
EntryKind::AssocConst(..) => DefKind::AssocConst,
627-
EntryKind::ImmStatic
628-
| EntryKind::MutStatic
629-
| EntryKind::ForeignImmStatic
630-
| EntryKind::ForeignMutStatic => DefKind::Static,
631-
EntryKind::Struct(_, _) => DefKind::Struct,
632-
EntryKind::Union(_, _) => DefKind::Union,
633-
EntryKind::Fn(_) | EntryKind::ForeignFn(_) => DefKind::Fn,
634-
EntryKind::AssocFn(_) => DefKind::AssocFn,
635-
EntryKind::Type => DefKind::TyAlias,
636-
EntryKind::TypeParam => DefKind::TyParam,
637-
EntryKind::ConstParam => DefKind::ConstParam,
638-
EntryKind::OpaqueTy => DefKind::OpaqueTy,
639-
EntryKind::AssocType(_) => DefKind::AssocTy,
640-
EntryKind::Mod(_) => DefKind::Mod,
641-
EntryKind::Variant(_) => DefKind::Variant,
642-
EntryKind::Trait(_) => DefKind::Trait,
643-
EntryKind::TraitAlias => DefKind::TraitAlias,
644-
EntryKind::Enum(..) => DefKind::Enum,
645-
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
646-
EntryKind::ProcMacro(kind) => DefKind::Macro(kind),
647-
EntryKind::ForeignType => DefKind::ForeignTy,
648-
EntryKind::Impl(_) => DefKind::Impl,
649-
EntryKind::Closure => DefKind::Closure,
650-
EntryKind::ForeignMod => DefKind::ForeignMod,
651-
EntryKind::GlobalAsm => DefKind::GlobalAsm,
652-
EntryKind::Field => DefKind::Field,
653-
EntryKind::Generator(_) => DefKind::Generator,
654-
}
655-
}
656-
}
657-
658621
impl CrateRoot<'_> {
659622
crate fn is_proc_macro_crate(&self) -> bool {
660623
self.proc_macro_data.is_some()
@@ -685,21 +648,6 @@ impl CrateRoot<'_> {
685648
}
686649

687650
impl<'a, 'tcx> CrateMetadataRef<'a> {
688-
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
689-
self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
690-
}
691-
692-
fn kind(&self, item_id: DefIndex) -> EntryKind {
693-
self.maybe_kind(item_id).unwrap_or_else(|| {
694-
bug!(
695-
"CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
696-
item_id,
697-
self.root.name,
698-
self.cnum,
699-
)
700-
})
701-
}
702-
703651
fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
704652
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
705653
// with items in 'raw_proc_macros'.
@@ -736,8 +684,30 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
736684
self.try_item_ident(item_index, sess).unwrap()
737685
}
738686

739-
fn def_kind(&self, index: DefIndex) -> DefKind {
740-
self.kind(index).def_kind()
687+
fn maybe_kind(&self, item_id: DefIndex) -> Option<EntryKind> {
688+
self.root.tables.kind.get(self, item_id).map(|k| k.decode(self))
689+
}
690+
691+
fn kind(&self, item_id: DefIndex) -> EntryKind {
692+
self.maybe_kind(item_id).unwrap_or_else(|| {
693+
bug!(
694+
"CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}",
695+
item_id,
696+
self.root.name,
697+
self.cnum,
698+
)
699+
})
700+
}
701+
702+
fn def_kind(&self, item_id: DefIndex) -> DefKind {
703+
self.root.tables.def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(|| {
704+
bug!(
705+
"CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}",
706+
item_id,
707+
self.root.name,
708+
self.cnum,
709+
)
710+
})
741711
}
742712

743713
fn get_span(&self, index: DefIndex, sess: &Session) -> Span {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
130130
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
131131
static_mutability => { cdata.static_mutability(def_id.index) }
132132
generator_kind => { cdata.generator_kind(def_id.index) }
133-
def_kind => { cdata.def_kind(def_id.index) }
133+
opt_def_kind => { Some(cdata.def_kind(def_id.index)) }
134134
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
135135
def_ident_span => {
136136
cdata.try_item_ident(def_id.index, &tcx.sess).ok().map(|ident| ident.span)

0 commit comments

Comments
 (0)