Skip to content

Commit 9e26c90

Browse files
authored
Rollup merge of #115534 - ouz-a:smir_def, r=oli-obk
Expose more information with DefId in smir Currently `Debug` for `DefId` doesn't provide enough information, this changes so that we get `usize` of the `DefId` and the name of it. r? `@oli-obk`
2 parents 7f93014 + 56d10a8 commit 9e26c90

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ impl<'tcx> Context for Tables<'tcx> {
3737
})
3838
}
3939

40+
fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
41+
self.tcx.def_path_str(self[def_id])
42+
}
43+
4044
fn all_local_items(&mut self) -> stable_mir::CrateItems {
4145
self.tcx.mir_keys(()).iter().map(|item| self.crate_item(item.to_def_id())).collect()
4246
}
47+
4348
fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
4449
Some(self.crate_item(self.tcx.entry_fn(())?.0))
4550
}

compiler/rustc_smir/src/stable_mir/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
1313
1414
use std::cell::Cell;
15+
use std::fmt;
16+
use std::fmt::Debug;
1517

1618
use self::ty::{
1719
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
@@ -29,9 +31,18 @@ pub type Symbol = String;
2931
pub type CrateNum = usize;
3032

3133
/// A unique identification number for each item accessible for the current compilation unit.
32-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
34+
#[derive(Clone, Copy, PartialEq, Eq)]
3335
pub struct DefId(pub(crate) usize);
3436

37+
impl Debug for DefId {
38+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
f.debug_struct("DefId:")
40+
.field("id", &self.0)
41+
.field("name", &with(|cx| cx.name_of_def_id(*self)))
42+
.finish()
43+
}
44+
}
45+
3546
/// A unique identification number for each provenance
3647
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
3748
pub struct AllocId(pub(crate) usize);
@@ -127,6 +138,9 @@ pub trait Context {
127138
/// Find a crate with the given name.
128139
fn find_crate(&self, name: &str) -> Option<Crate>;
129140

141+
/// Prints the name of given `DefId`
142+
fn name_of_def_id(&self, def_id: DefId) -> String;
143+
130144
/// Obtain the representation of a type.
131145
fn ty_kind(&mut self, ty: Ty) -> TyKind;
132146

0 commit comments

Comments
 (0)