Skip to content

Add DefId::parent() accessor for rustc_public #144000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/rustc_public/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefId>;

/// Return registered tool attributes with the given attribute name.
///
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
Expand Down Expand Up @@ -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<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
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_public/src/crate_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefId> {
with(|cx| cx.def_parent(*self))
}
}

/// A trait for retrieving information about a particular definition.
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_public_bridge/src/context/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand Down
Loading