diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index 2f2dac3c06d5..78144524ac50 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -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] } } diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 876422b945fb..7edb71c0c9c2 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -132,7 +132,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(), }, diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 506f910d5816..64da8d1ab024 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -127,7 +127,7 @@ impl<'tcx> Context for Tables<'tcx> { } fn ty_kind(&mut self, ty: stable_mir::ty::Ty) -> TyKind { - self.types[ty.0].kind().stable(self) + self.types[ty].kind().stable(self) } fn generics_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics { @@ -213,19 +213,14 @@ pub struct Tables<'tcx> { pub def_ids: IndexMap, pub alloc_ids: IndexMap, pub spans: IndexMap, - pub types: Vec>, + pub types: IndexMap, stable_mir::ty::Ty>, pub instances: IndexMap, InstanceDef>, pub constants: IndexMap, 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 { diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index f1af09fac66d..0dbf6fe23aaa 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -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);