span is index

This commit is contained in:
ouz-a
2023-09-12 11:50:50 +03:00
parent 6c03617142
commit fa57a48cf5
4 changed files with 34 additions and 8 deletions
+16 -1
View File
@@ -17,6 +17,7 @@
use rustc_middle::mir::interpret::AllocId;
use rustc_middle::ty::TyCtxt;
pub use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::Span;
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
let mut ret = None;
@@ -159,6 +160,17 @@ fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
self.alloc_ids.push(aid);
stable_mir::AllocId(id)
}
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
for (i, &sp) in self.spans.iter().enumerate() {
if sp == span {
return stable_mir::ty::Span(i);
}
}
let id = self.spans.len();
self.spans.push(span);
stable_mir::ty::Span(id)
}
}
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
@@ -166,7 +178,10 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
}
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
crate::stable_mir::run(Tables { tcx, def_ids: vec![], alloc_ids: vec![], types: vec![] }, f);
crate::stable_mir::run(
Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] },
f,
);
}
/// A type that provides internal information but that can still be used for debug purpose.
+7 -5
View File
@@ -9,7 +9,9 @@
use crate::rustc_internal::{self, opaque};
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
use crate::stable_mir::ty::{FloatTy, GenericParamDef, IntTy, Movability, RigidTy, TyKind, UintTy};
use crate::stable_mir::ty::{
FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy,
};
use crate::stable_mir::{self, CompilerError, Context};
use rustc_hir as hir;
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@@ -42,7 +44,7 @@ fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
self.tcx.def_path_str(self[def_id])
}
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Span {
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
self.tcx.def_span(self[def_id]).stable(self)
}
@@ -168,6 +170,7 @@ pub struct Tables<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub def_ids: Vec<DefId>,
pub alloc_ids: Vec<AllocId>,
pub spans: Vec<rustc_span::Span>,
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
}
@@ -1497,9 +1500,8 @@ fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
impl<'tcx> Stable<'tcx> for rustc_span::Span {
type T = stable_mir::ty::Span;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
// FIXME: add a real implementation of stable spans
opaque(self)
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
tables.create_span(*self)
}
}
+1 -1
View File
@@ -90,7 +90,7 @@ pub fn body(&self) -> mir::Body {
with(|cx| cx.mir_body(self.0))
}
pub fn span(&self) -> ty::Span {
pub fn span(&self) -> Span {
with(|cx| cx.span_of_an_item(self.0))
}
}
+10 -1
View File
@@ -35,7 +35,16 @@ pub struct Const {
type Ident = Opaque;
pub(crate) type Region = Opaque;
pub(crate) type Span = Opaque;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span(pub(crate) usize);
impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut span = None;
with(|context| context.rustc_tables(&mut |tables| span = Some(tables.spans[self.0])));
f.write_fmt(format_args!("{:?}", &span.unwrap()))
}
}
#[derive(Clone, Debug)]
pub enum TyKind {