Skip to content
Merged
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
6 changes: 3 additions & 3 deletions compiler/rustc_public/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,12 @@ impl<'tcx> CompilerInterface<'tcx> {
cnst.internal(&mut *tables, cx.tcx).to_string()
}

/// `Span` of an item.
pub(crate) fn span_of_an_item(&self, def_id: DefId) -> Span {
/// `Span` of a `DefId`.
pub(crate) fn span_of_a_def(&self, def_id: DefId) -> Span {
let mut tables = self.tables.borrow_mut();
let cx = &*self.cx.borrow();
let did = tables[def_id];
cx.span_of_an_item(did).stable(&mut *tables, cx)
cx.span_of_a_def(did).stable(&mut *tables, cx)
}

pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_public/src/crate_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl DefId {
pub fn parent(&self) -> Option<DefId> {
with(|cx| cx.def_parent(*self))
}

pub fn span(&self) -> Span {
with(|cx| cx.span_of_a_def(*self))
}
}

/// A trait for retrieving information about a particular definition.
Expand Down Expand Up @@ -68,8 +72,7 @@ pub trait CrateDef {

/// Return the span of this definition.
fn span(&self) -> Span {
let def_id = self.def_id();
with(|cx| cx.span_of_an_item(def_id))
self.def_id().span()
}

/// Return registered tool attributes with the given attribute name.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_public/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl CrateItem {
}

pub fn span(&self) -> Span {
with(|cx| cx.span_of_an_item(self.0))
self.0.span()
}

pub fn kind(&self) -> ItemKind {
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_public/src/unstable/convert/stable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ impl<'tcx> Stable<'tcx> for rustc_span::Symbol {
}
}

impl<'tcx> Stable<'tcx> for rustc_span::def_id::DefId {
type T = crate::DefId;

fn stable<'cx>(
&self,
tables: &mut Tables<'cx, BridgeTys>,
_: &CompilerCtxt<'cx, BridgeTys>,
) -> Self::T {
tables.create_def_id(*self)
}
}

Comment on lines +85 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we don't really need this impl. We can just use create_def_id everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_def_id is not publicly available. This impl enables rustc_public::rustc_internal::stable for creating DefId for variants, which is not possible using current rustc_public api. Is there any downside for having this impl?

Copy link
Member

@makai410 makai410 Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Stable trait is also not publicly available and shouldn't be exposed to users, so adding this impl here doesn't make sense for getting the DefId of a variant. I think we probably could add an api to the CompilerInterface for retrieving the DefId of a VariantDef given that we have the Internal trait implemented for VariantDef, then we don't have to store a def_id in that struct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rustc_public::unstable::Stable trait is not public, but the rustc_public::rustc_internal::stable function is public for users who mix rustc_public with unstable rustc apis (like me).

Adding an api for retrieving DefId from a variant is needed for the completeness of rustc_public, but even with that, I think this impl is useful for the stable function users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha.

impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = crate::ty::Span;

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_public_bridge/src/context/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
)
}

/// `Span` of an item.
pub fn span_of_an_item(&self, def_id: DefId) -> Span {
/// `Span` of a `DefId`.
pub fn span_of_a_def(&self, def_id: DefId) -> Span {
self.tcx.def_span(def_id)
}

Expand Down
Loading