Skip to content

Commit 99049b3

Browse files
Add DefId::parent() accessor in rustc_public
1 parent 42f4793 commit 99049b3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

compiler/rustc_public/src/compiler_interface.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ impl<'tcx> CompilerInterface<'tcx> {
249249
cx.def_name(did, trimmed)
250250
}
251251

252+
/// Returns the parent of the given `DefId`.
253+
pub(crate) fn def_parent(&self, def_id: DefId) -> Option<DefId> {
254+
let mut tables = self.tables.borrow_mut();
255+
let cx = &*self.cx.borrow();
256+
let did = tables[def_id];
257+
cx.def_parent(did).map(|did| tables.create_def_id(did))
258+
}
259+
252260
/// Return registered tool attributes with the given attribute name.
253261
///
254262
/// 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
@@ -268,6 +268,11 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
268268
}
269269
}
270270

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

0 commit comments

Comments
 (0)