Skip to content

Commit 8f8915d

Browse files
committed
span is index
1 parent 6c03617 commit 8f8915d

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_interface::{interface, Queries};
1717
use rustc_middle::mir::interpret::AllocId;
1818
use rustc_middle::ty::TyCtxt;
1919
pub use rustc_span::def_id::{CrateNum, DefId};
20+
use rustc_span::Span;
2021

2122
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
2223
let mut ret = None;
@@ -72,6 +73,10 @@ pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
7273
with_tables(|t| t.impl_def(did))
7374
}
7475

76+
pub fn get_rustc_span(span: stable_mir::ty::Span) -> rustc_span::Span {
77+
with_tables(|t| t.spans[span.0])
78+
}
79+
7580
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
7681
type Output = DefId;
7782

@@ -159,14 +164,28 @@ impl<'tcx> Tables<'tcx> {
159164
self.alloc_ids.push(aid);
160165
stable_mir::AllocId(id)
161166
}
167+
168+
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
169+
for (i, &sp) in self.spans.iter().enumerate() {
170+
if sp == span {
171+
return stable_mir::ty::Span(i);
172+
}
173+
}
174+
let id = self.spans.len();
175+
self.spans.push(span);
176+
stable_mir::ty::Span(id)
177+
}
162178
}
163179

164180
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
165181
item.id.into()
166182
}
167183

168184
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
169-
crate::stable_mir::run(Tables { tcx, def_ids: vec![], alloc_ids: vec![], types: vec![] }, f);
185+
crate::stable_mir::run(
186+
Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] },
187+
f,
188+
);
170189
}
171190

172191
/// A type that provides internal information but that can still be used for debug purpose.

compiler/rustc_smir/src/rustc_smir/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
12-
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
12+
use crate::stable_mir::ty::{
13+
FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy,
14+
};
1315
use crate::stable_mir::{self, CompilerError, Context};
1416
use rustc_hir as hir;
1517
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@@ -42,7 +44,7 @@ impl<'tcx> Context for Tables<'tcx> {
4244
self.tcx.def_path_str(self[def_id])
4345
}
4446

45-
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Span {
47+
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
4648
self.tcx.def_span(self[def_id]).stable(self)
4749
}
4850

@@ -168,6 +170,7 @@ pub struct Tables<'tcx> {
168170
pub tcx: TyCtxt<'tcx>,
169171
pub def_ids: Vec<DefId>,
170172
pub alloc_ids: Vec<AllocId>,
173+
pub spans: Vec<rustc_span::Span>,
171174
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
172175
}
173176

@@ -1497,9 +1500,8 @@ impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
14971500
impl<'tcx> Stable<'tcx> for rustc_span::Span {
14981501
type T = stable_mir::ty::Span;
14991502

1500-
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1501-
// FIXME: add a real implementation of stable spans
1502-
opaque(self)
1503+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1504+
tables.create_span(*self)
15031505
}
15041506
}
15051507

compiler/rustc_smir/src/stable_mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl CrateItem {
9090
with(|cx| cx.mir_body(self.0))
9191
}
9292

93-
pub fn span(&self) -> ty::Span {
93+
pub fn span(&self) -> Span {
9494
with(|cx| cx.span_of_an_item(self.0))
9595
}
9696
}
@@ -160,7 +160,7 @@ pub trait Context {
160160
/// Prints the name of given `DefId`
161161
fn name_of_def_id(&self, def_id: DefId) -> String;
162162

163-
/// `Span` of an item
163+
/// 'Span' of an item
164164
fn span_of_an_item(&mut self, def_id: DefId) -> Span;
165165

166166
/// Obtain the representation of a type.

compiler/rustc_smir/src/stable_mir/ty.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,16 @@ pub struct Const {
3535

3636
type Ident = Opaque;
3737
pub(crate) type Region = Opaque;
38-
pub(crate) type Span = Opaque;
38+
#[derive(Clone, Copy, PartialEq, Eq)]
39+
pub struct Span(pub(crate) usize);
40+
41+
impl Debug for Span {
42+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
43+
let mut span = None;
44+
with(|context| context.rustc_tables(&mut |tables| span = Some(tables.spans[self.0])));
45+
f.write_fmt(format_args!("{:?}", &span.unwrap()))
46+
}
47+
}
3948

4049
#[derive(Clone, Debug)]
4150
pub enum TyKind {

tests/ui-fulldeps/stable-mir/crate-info.rs

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
3636

3737
// Find items in the local crate.
3838
let items = stable_mir::all_local_items();
39+
items.iter().for_each(|item|
40+
if 1 == 1{
41+
panic!("item span {:#?}",item.span());
42+
}
43+
);
3944
assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some());
4045

4146
// Find the `std` crate.

0 commit comments

Comments
 (0)