Skip to content

Commit 7a32307

Browse files
Add DefId::parent() accessor in rustc_public
1 parent 3014e79 commit 7a32307

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

compiler/rustc_public/src/compiler_interface.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ pub(crate) trait SmirInterface {
111111
/// Returns the name of given `DefId`
112112
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
113113

114+
/// Returns the parent of the given `DefId`.
115+
fn def_parent(&self, def_id: DefId) -> Option<DefId>;
116+
114117
/// Return registered tool attributes with the given attribute name.
115118
///
116119
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
@@ -488,6 +491,14 @@ impl<'tcx> SmirInterface for SmirContainer<'tcx, BridgeTys> {
488491
cx.def_name(did, trimmed)
489492
}
490493

494+
/// Returns the parent of the given `DefId`.
495+
fn def_parent(&self, def_id: DefId) -> Option<DefId> {
496+
let mut tables = self.tables.borrow_mut();
497+
let cx = &*self.cx.borrow();
498+
let did = tables[def_id];
499+
cx.def_parent(did).map(|did| tables.create_def_id(did))
500+
}
501+
491502
/// Return registered tool attributes with the given attribute name.
492503
///
493504
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool

compiler/rustc_public/src/crate_def.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ impl DefId {
2929
pub fn trimmed_name(&self) -> Symbol {
3030
with(|cx| cx.def_name(*self, true))
3131
}
32+
33+
/// Return the parent of this definition, or `None` if this is the root of a
34+
/// crate.
35+
pub fn parent(&self) -> Option<DefId> {
36+
with(|cx| cx.def_parent(*self))
37+
}
3238
}
3339

3440
/// A trait for retrieving information about a particular definition.

compiler/rustc_public_bridge/src/context/impls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> {
267267
}
268268
}
269269

270+
/// Returns the parent of the given `DefId`.
271+
pub fn def_parent(&self, def_id: DefId) -> Option<DefId> {
272+
self.tcx.opt_parent(def_id)
273+
}
274+
270275
/// Return registered tool attributes with the given attribute name.
271276
///
272277
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool

0 commit comments

Comments
 (0)