Use IndexMap for handling stable Ty

This commit is contained in:
Celina G. Val 2023-10-24 10:38:05 -07:00
parent 3f60165d27
commit 17f6df9c63
4 changed files with 23 additions and 29 deletions

View File

@ -46,7 +46,7 @@ impl<'tcx> RustcInternal<'tcx> for Region {
impl<'tcx> RustcInternal<'tcx> for Ty {
type T = InternalTy<'tcx>;
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
tables.types[self.0]
tables.types[*self]
}
}

View File

@ -167,7 +167,7 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
def_ids: IndexMap::default(),
alloc_ids: IndexMap::default(),
spans: IndexMap::default(),
types: vec![],
types: IndexMap::default(),
instances: IndexMap::default(),
constants: IndexMap::default(),
}));

View File

@ -148,7 +148,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
fn ty_kind(&self, ty: stable_mir::ty::Ty) -> TyKind {
let mut tables = self.0.borrow_mut();
tables.types[ty.0].kind().stable(&mut *tables)
tables.types[ty].kind().stable(&mut *tables)
}
fn generics_of(&self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
@ -252,19 +252,14 @@ pub struct Tables<'tcx> {
pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
pub(crate) types: Vec<Ty<'tcx>>,
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
pub(crate) constants: IndexMap<mir::Const<'tcx>, ConstId>,
}
impl<'tcx> Tables<'tcx> {
fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
if let Some(id) = self.types.iter().position(|t| *t == ty) {
return stable_mir::ty::Ty(id);
}
let id = self.types.len();
self.types.push(ty);
stable_mir::ty::Ty(id)
self.types.create_or_fetch(ty)
}
fn intern_const(&mut self, constant: mir::Const<'tcx>) -> ConstId {

View File

@ -6,7 +6,7 @@ use super::{
use crate::{Filename, Opaque};
use std::fmt::{self, Debug, Formatter};
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Ty(pub usize);
impl Debug for Ty {
@ -52,15 +52,6 @@ impl Const {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ConstId(pub usize);
impl IndexedVal for ConstId {
fn to_val(index: usize) -> Self {
ConstId(index)
}
fn to_index(&self) -> usize {
self.0
}
}
type Ident = Opaque;
#[derive(Debug, Clone)]
@ -136,15 +127,6 @@ pub struct LineInfo {
pub end_col: usize,
}
impl IndexedVal for Span {
fn to_val(index: usize) -> Self {
Span(index)
}
fn to_index(&self) -> usize {
self.0
}
}
#[derive(Clone, Debug)]
pub enum TyKind {
RigidTy(RigidTy),
@ -631,3 +613,20 @@ pub trait IndexedVal {
fn to_index(&self) -> usize;
}
macro_rules! index_impl {
($name:ident) => {
impl IndexedVal for $name {
fn to_val(index: usize) -> Self {
$name(index)
}
fn to_index(&self) -> usize {
self.0
}
}
};
}
index_impl!(ConstId);
index_impl!(Ty);
index_impl!(Span);