Add DefId::parent() accessor in rustc_public

This commit is contained in:
Jacob Greenfield
2025-07-15 21:52:43 -04:00
parent 42f4793e5a
commit 99049b3aa8
3 changed files with 19 additions and 0 deletions
@@ -249,6 +249,14 @@ pub(crate) fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol {
cx.def_name(did, trimmed)
}
/// Returns the parent of the given `DefId`.
pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
let mut tables = self.tables.borrow_mut();
let cx = &*self.cx.borrow();
let did = tables[def_id];
cx.def_parent(did).map(|did| tables.create_def_id(did))
}
/// Return registered tool attributes with the given attribute name.
///
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
+6
View File
@@ -29,6 +29,12 @@ pub fn name(&self) -> Symbol {
pub fn trimmed_name(&self) -> Symbol {
with(|cx| cx.def_name(*self, true))
}
/// Return the parent of this definition, or `None` if this is the root of a
/// crate.
pub fn parent(&self) -> Option<DefId> {
with(|cx| cx.def_parent(*self))
}
}
/// A trait for retrieving information about a particular definition.
@@ -268,6 +268,11 @@ pub fn def_name(&self, def_id: DefId, trimmed: bool) -> String {
}
}
/// Returns the parent of the given `DefId`.
pub fn def_parent(&self, def_id: DefId) -> Option<DefId> {
self.tcx.opt_parent(def_id)
}
/// Return registered tool attributes with the given attribute name.
///
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool