Skip to content

Commit 17f6df9

Browse files
committedOct 24, 2023
Use IndexMap for handling stable Ty
1 parent 3f60165 commit 17f6df9

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed
 

‎compiler/rustc_smir/src/rustc_internal/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> RustcInternal<'tcx> for Region {
4646
impl<'tcx> RustcInternal<'tcx> for Ty {
4747
type T = InternalTy<'tcx>;
4848
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
49-
tables.types[self.0]
49+
tables.types[*self]
5050
}
5151
}
5252

‎compiler/rustc_smir/src/rustc_internal/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
167167
def_ids: IndexMap::default(),
168168
alloc_ids: IndexMap::default(),
169169
spans: IndexMap::default(),
170-
types: vec![],
170+
types: IndexMap::default(),
171171
instances: IndexMap::default(),
172172
constants: IndexMap::default(),
173173
}));

‎compiler/rustc_smir/src/rustc_smir/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
148148

149149
fn ty_kind(&self, ty: stable_mir::ty::Ty) -> TyKind {
150150
let mut tables = self.0.borrow_mut();
151-
tables.types[ty.0].kind().stable(&mut *tables)
151+
tables.types[ty].kind().stable(&mut *tables)
152152
}
153153

154154
fn generics_of(&self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
@@ -252,19 +252,14 @@ pub struct Tables<'tcx> {
252252
pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
253253
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
254254
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
255-
pub(crate) types: Vec<Ty<'tcx>>,
255+
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
256256
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
257257
pub(crate) constants: IndexMap<mir::Const<'tcx>, ConstId>,
258258
}
259259

260260
impl<'tcx> Tables<'tcx> {
261261
fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
262-
if let Some(id) = self.types.iter().position(|t| *t == ty) {
263-
return stable_mir::ty::Ty(id);
264-
}
265-
let id = self.types.len();
266-
self.types.push(ty);
267-
stable_mir::ty::Ty(id)
262+
self.types.create_or_fetch(ty)
268263
}
269264

270265
fn intern_const(&mut self, constant: mir::Const<'tcx>) -> ConstId {

‎compiler/stable_mir/src/ty.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
use crate::{Filename, Opaque};
77
use std::fmt::{self, Debug, Formatter};
88

9-
#[derive(Copy, Clone)]
9+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
1010
pub struct Ty(pub usize);
1111

1212
impl Debug for Ty {
@@ -52,15 +52,6 @@ impl Const {
5252
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5353
pub struct ConstId(pub usize);
5454

55-
impl IndexedVal for ConstId {
56-
fn to_val(index: usize) -> Self {
57-
ConstId(index)
58-
}
59-
fn to_index(&self) -> usize {
60-
self.0
61-
}
62-
}
63-
6455
type Ident = Opaque;
6556

6657
#[derive(Debug, Clone)]
@@ -136,15 +127,6 @@ pub struct LineInfo {
136127
pub end_col: usize,
137128
}
138129

139-
impl IndexedVal for Span {
140-
fn to_val(index: usize) -> Self {
141-
Span(index)
142-
}
143-
fn to_index(&self) -> usize {
144-
self.0
145-
}
146-
}
147-
148130
#[derive(Clone, Debug)]
149131
pub enum TyKind {
150132
RigidTy(RigidTy),
@@ -631,3 +613,20 @@ pub trait IndexedVal {
631613

632614
fn to_index(&self) -> usize;
633615
}
616+
617+
macro_rules! index_impl {
618+
($name:ident) => {
619+
impl IndexedVal for $name {
620+
fn to_val(index: usize) -> Self {
621+
$name(index)
622+
}
623+
fn to_index(&self) -> usize {
624+
self.0
625+
}
626+
}
627+
};
628+
}
629+
630+
index_impl!(ConstId);
631+
index_impl!(Ty);
632+
index_impl!(Span);

0 commit comments

Comments
 (0)