Skip to content

Commit 4f90a52

Browse files
authored
Rollup merge of #115772 - ouz-a:smir_span2, r=oli-obk
Improve Span in smir Addressing rust-lang/project-stable-mir#31 r? ``@oli-obk``
2 parents 5e71913 + fa57a48 commit 4f90a52

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+16-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;
@@ -159,14 +160,28 @@ impl<'tcx> Tables<'tcx> {
159160
self.alloc_ids.push(aid);
160161
stable_mir::AllocId(id)
161162
}
163+
164+
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
165+
for (i, &sp) in self.spans.iter().enumerate() {
166+
if sp == span {
167+
return stable_mir::ty::Span(i);
168+
}
169+
}
170+
let id = self.spans.len();
171+
self.spans.push(span);
172+
stable_mir::ty::Span(id)
173+
}
162174
}
163175

164176
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
165177
item.id.into()
166178
}
167179

168180
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
169-
crate::stable_mir::run(Tables { tcx, def_ids: vec![], alloc_ids: vec![], types: vec![] }, f);
181+
crate::stable_mir::run(
182+
Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] },
183+
f,
184+
);
170185
}
171186

172187
/// 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

@@ -185,6 +187,7 @@ pub struct Tables<'tcx> {
185187
pub tcx: TyCtxt<'tcx>,
186188
pub def_ids: Vec<DefId>,
187189
pub alloc_ids: Vec<AllocId>,
190+
pub spans: Vec<rustc_span::Span>,
188191
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
189192
}
190193

@@ -1514,9 +1517,8 @@ impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
15141517
impl<'tcx> Stable<'tcx> for rustc_span::Span {
15151518
type T = stable_mir::ty::Span;
15161519

1517-
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1518-
// FIXME: add a real implementation of stable spans
1519-
opaque(self)
1520+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1521+
tables.create_span(*self)
15201522
}
15211523
}
15221524

compiler/rustc_smir/src/stable_mir/mod.rs

+1-1
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
}

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 {

0 commit comments

Comments
 (0)