Skip to content

Commit 7655d93

Browse files
Rollup merge of #152912 - HKalbasi:rustc_public_def_span, r=makai410
Expose Span for all DefIds in rustc_public Part of rust-lang/project-stable-mir#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.
2 parents 47e2fec + 1ded3a6 commit 7655d93

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

compiler/rustc_public/src/compiler_interface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,12 @@ impl<'tcx> CompilerInterface<'tcx> {
562562
cnst.internal(&mut *tables, cx.tcx).to_string()
563563
}
564564

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

573573
pub(crate) fn ty_const_pretty(&self, ct: TyConstId) -> String {

compiler/rustc_public/src/crate_def.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ impl DefId {
3434
pub fn parent(&self) -> Option<DefId> {
3535
with(|cx| cx.def_parent(*self))
3636
}
37+
38+
pub fn span(&self) -> Span {
39+
with(|cx| cx.span_of_a_def(*self))
40+
}
3741
}
3842

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

6973
/// Return the span of this definition.
7074
fn span(&self) -> Span {
71-
let def_id = self.def_id();
72-
with(|cx| cx.span_of_an_item(def_id))
75+
self.def_id().span()
7376
}
7477

7578
/// Return registered tool attributes with the given attribute name.

compiler/rustc_public/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl CrateItem {
155155
}
156156

157157
pub fn span(&self) -> Span {
158-
with(|cx| cx.span_of_an_item(self.0))
158+
self.0.span()
159159
}
160160

161161
pub fn kind(&self) -> ItemKind {

compiler/rustc_public/src/unstable/convert/stable/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ impl<'tcx> Stable<'tcx> for rustc_span::Symbol {
8282
}
8383
}
8484

85+
impl<'tcx> Stable<'tcx> for rustc_span::def_id::DefId {
86+
type T = crate::DefId;
87+
88+
fn stable<'cx>(
89+
&self,
90+
tables: &mut Tables<'cx, BridgeTys>,
91+
_: &CompilerCtxt<'cx, BridgeTys>,
92+
) -> Self::T {
93+
tables.create_def_id(*self)
94+
}
95+
}
96+
8597
impl<'tcx> Stable<'tcx> for rustc_span::Span {
8698
type T = crate::ty::Span;
8799

compiler/rustc_public_bridge/src/context/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> {
554554
)
555555
}
556556

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

0 commit comments

Comments
 (0)