Skip to content

Commit

Permalink
Use IndexMap for handling stable Ty
Browse files Browse the repository at this point in the history
  • Loading branch information
celinval committed Oct 24, 2023
1 parent 3f60165 commit 17f6df9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}));
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 18 additions & 19 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);

0 comments on commit 17f6df9

Please sign in to comment.