diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index d15438c2b800e..66667d8d3d885 100644 --- a/compiler/rustc_public/src/compiler_interface.rs +++ b/compiler/rustc_public/src/compiler_interface.rs @@ -111,6 +111,9 @@ pub(crate) trait SmirInterface { /// Returns the name of given `DefId` fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol; + /// Returns the parent of the given `DefId`. + fn def_parent(&self, def_id: DefId) -> Option; + /// Return registered tool attributes with the given attribute name. /// /// 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> { cx.def_name(did, trimmed) } + /// Returns the parent of the given `DefId`. + fn def_parent(&self, def_id: DefId) -> Option { + 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 diff --git a/compiler/rustc_public/src/crate_def.rs b/compiler/rustc_public/src/crate_def.rs index 75228135e4cb3..7ab123544e3b6 100644 --- a/compiler/rustc_public/src/crate_def.rs +++ b/compiler/rustc_public/src/crate_def.rs @@ -29,6 +29,12 @@ impl DefId { 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 { + with(|cx| cx.def_parent(*self)) + } } /// A trait for retrieving information about a particular definition. diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index fdefad2821bb2..2fd977ab41d12 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -267,6 +267,11 @@ impl<'tcx, B: Bridge> SmirCtxt<'tcx, B> { } } + /// Returns the parent of the given `DefId`. + pub fn def_parent(&self, def_id: DefId) -> Option { + 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