Skip to content

Commit dadce25

Browse files
committed
always print def-path in Debug impl for DefId
I also added an `opt_def_path` so that we can deal with DefIds that are missing a `DefPath` entry.
1 parent 2446e25 commit dadce25

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

src/librustc/hir/def_id.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,14 @@ impl fmt::Debug for DefId {
5858
write!(f, "DefId {{ krate: {:?}, node: {:?}",
5959
self.krate, self.index)?;
6060

61-
// Unfortunately, there seems to be no way to attempt to print
62-
// a path for a def-id, so I'll just make a best effort for now
63-
// and otherwise fallback to just printing the crate/node pair
64-
if self.is_local() { // (1)
65-
// (1) side-step fact that not all external things have paths at
66-
// the moment, such as type parameters
67-
ty::tls::with_opt(|opt_tcx| {
68-
if let Some(tcx) = opt_tcx {
69-
write!(f, " => {}", tcx.item_path_str(*self))?;
61+
ty::tls::with_opt(|opt_tcx| {
62+
if let Some(tcx) = opt_tcx {
63+
if let Some(def_path) = tcx.opt_def_path(*self) {
64+
write!(f, " => {}", def_path.to_string(tcx))?;
7065
}
71-
Ok(())
72-
})?;
73-
}
66+
}
67+
Ok(())
68+
})?;
7469

7570
write!(f, " }}")
7671
}

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub trait CrateStore<'tcx> {
233233
def: DefKey)
234234
-> Option<DefIndex>;
235235
fn def_key(&self, def: DefId) -> hir_map::DefKey;
236-
fn relative_def_path(&self, def: DefId) -> hir_map::DefPath;
236+
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
237237
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind>;
238238
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
239239
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
@@ -430,7 +430,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
430430

431431
// resolve
432432
fn def_key(&self, def: DefId) -> hir_map::DefKey { bug!("def_key") }
433-
fn relative_def_path(&self, def: DefId) -> hir_map::DefPath { bug!("relative_def_path") }
433+
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> { bug!("relative_def_path") }
434434
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { bug!("variant_kind") }
435435
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
436436
{ bug!("struct_ctor_def_id") }

src/librustc/ty/mod.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -2437,12 +2437,41 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24372437
}
24382438
}
24392439

2440-
/// Returns the `DefPath` of an item. Note that if `id` is not
2441-
/// local to this crate -- or is inlined into this crate -- the
2442-
/// result will be a non-local `DefPath`.
2440+
/// Convert a `DefId` into its fully expanded `DefPath` (every
2441+
/// `DefId` is really just an interned def-path).
2442+
///
2443+
/// Note that if `id` is not local to this crate -- or is
2444+
/// inlined into this crate -- the result will be a non-local
2445+
/// `DefPath`.
2446+
///
2447+
/// This function is only safe to use when you are sure that the
2448+
/// full def-path is accessible. Examples that are known to be
2449+
/// safe are local def-ids or items; see `opt_def_path` for more
2450+
/// details.
24432451
pub fn def_path(self, id: DefId) -> ast_map::DefPath {
2452+
self.opt_def_path(id).unwrap_or_else(|| {
2453+
bug!("could not load def-path for {:?}", id)
2454+
})
2455+
}
2456+
2457+
/// Convert a `DefId` into its fully expanded `DefPath` (every
2458+
/// `DefId` is really just an interned def-path).
2459+
///
2460+
/// When going across crates, we do not save the full info for
2461+
/// every cross-crate def-id, and hence we may not always be able
2462+
/// to create a def-path. Therefore, this returns
2463+
/// `Option<DefPath>` to cover that possibility. It will always
2464+
/// return `Some` for local def-ids, however, as well as for
2465+
/// items. The problems arise with "minor" def-ids like those
2466+
/// associated with a pattern, `impl Trait`, or other internal
2467+
/// detail to a fn.
2468+
///
2469+
/// Note that if `id` is not local to this crate -- or is
2470+
/// inlined into this crate -- the result will be a non-local
2471+
/// `DefPath`.
2472+
pub fn opt_def_path(self, id: DefId) -> Option<ast_map::DefPath> {
24442473
if id.is_local() {
2445-
self.map.def_path(id)
2474+
Some(self.map.def_path(id))
24462475
} else {
24472476
self.sess.cstore.relative_def_path(id)
24482477
}

src/librustc_metadata/csearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
435435
decoder::def_key(&cdata, def.index)
436436
}
437437

438-
fn relative_def_path(&self, def: DefId) -> hir_map::DefPath {
438+
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
439439
// See `Note` above in `def_key()` for why this read is
440440
// commented out:
441441
//

src/librustc_metadata/decoder.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ pub fn maybe_get_item_ast<'a, 'tcx>(cdata: Cmd, tcx: TyCtxt<'a, 'tcx, 'tcx>, id:
759759
krate: cdata.cnum,
760760
index: def_key(cdata, id).parent.unwrap()
761761
};
762-
let mut parent_def_path = def_path(cdata, id);
762+
let mut parent_def_path = def_path(cdata, id).unwrap();
763763
parent_def_path.data.pop();
764764
if let Some(ast_doc) = reader::maybe_get_doc(item_doc, tag_ast as usize) {
765765
let ii = decode_inlined_item(cdata,
@@ -1626,9 +1626,16 @@ fn item_def_key(item_doc: rbml::Doc) -> hir_map::DefKey {
16261626
}
16271627
}
16281628

1629-
pub fn def_path(cdata: Cmd, id: DefIndex) -> hir_map::DefPath {
1629+
// Returns the path leading to the thing with this `id`. Note that
1630+
// some def-ids don't wind up in the metadata, so `def_path` sometimes
1631+
// returns `None`
1632+
pub fn def_path(cdata: Cmd, id: DefIndex) -> Option<hir_map::DefPath> {
16301633
debug!("def_path(id={:?})", id);
1631-
hir_map::DefPath::make(cdata.cnum, id, |parent| def_key(cdata, parent))
1634+
if cdata.get_item(id).is_some() {
1635+
Some(hir_map::DefPath::make(cdata.cnum, id, |parent| def_key(cdata, parent)))
1636+
} else {
1637+
None
1638+
}
16321639
}
16331640

16341641
pub fn get_panic_strategy(data: &[u8]) -> PanicStrategy {

0 commit comments

Comments
 (0)