Rollup merge of #152912 - HKalbasi:rustc_public_def_span, r=makai410

Expose Span for all DefIds in rustc_public

Part of https://github.com/rust-lang/project-stable-mir/issues/118

To be maximally useful, `VariantDef` and `FieldDef` should be changed to be a wrapper around `DefId` instead of holding their parent and index. I can do this change in this PR or a follow-up if it is desired. For now, I added the missing `impl Stable for DefId` in internals so you can convert from rustc internals.
This commit is contained in:
Jonathan Brouwer
2026-02-20 22:00:59 +01:00
committed by GitHub
5 changed files with 23 additions and 8 deletions
@@ -562,12 +562,12 @@ pub(crate) fn mir_const_pretty(&self, cnst: &MirConst) -> String {
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 {
+5 -2
View File
@@ -34,6 +34,10 @@ pub fn trimmed_name(&self) -> Symbol {
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.
@@ -68,8 +72,7 @@ fn krate(&self) -> Crate {
/// 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.
+1 -1
View File
@@ -155,7 +155,7 @@ pub fn has_body(&self) -> bool {
}
pub fn span(&self) -> Span {
with(|cx| cx.span_of_an_item(self.0))
self.0.span()
}
pub fn kind(&self) -> ItemKind {
@@ -82,6 +82,18 @@ fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>)
}
}
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)
}
}
impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = crate::ty::Span;
@@ -554,8 +554,8 @@ pub fn def_ty_with_args(&self, item: DefId, args_ref: GenericArgsRef<'tcx>) -> T
)
}
/// `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)
}