Skip to content

Commit 470f3af

Browse files
committed
add new wrapper for FxIndexMap
1 parent 77df2cd commit 470f3af

File tree

4 files changed

+74
-33
lines changed

4 files changed

+74
-33
lines changed

Diff for: compiler/rustc_smir/src/rustc_internal/mod.rs

+35-28
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44
//! until stable MIR is complete.
55
6-
use std::ops::{ControlFlow, Index};
7-
86
use crate::rustc_internal;
97
use crate::rustc_smir::Tables;
108
use rustc_data_structures::fx;
@@ -14,14 +12,18 @@ use rustc_middle::mir::interpret::AllocId;
1412
use rustc_middle::ty::TyCtxt;
1513
use rustc_span::def_id::{CrateNum, DefId};
1614
use rustc_span::Span;
15+
use stable_mir::ty::IndexToVal;
1716
use stable_mir::CompilerError;
17+
use std::fmt::Debug;
18+
use std::hash::Hash;
19+
use std::ops::{ControlFlow, Index};
1820

1921
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
2022
type Output = DefId;
2123

2224
#[inline(always)]
2325
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
24-
&self.def_ids.get_index(index.0).unwrap().0
26+
&self.def_ids.get_index_and_assert(index.0, index)
2527
}
2628
}
2729

@@ -30,7 +32,7 @@ impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {
3032

3133
#[inline(always)]
3234
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
33-
&self.spans.get_index(index.0).unwrap().0
35+
&self.spans.get_index_and_assert(index.0, index)
3436
}
3537
}
3638

@@ -96,33 +98,15 @@ impl<'tcx> Tables<'tcx> {
9698
}
9799

98100
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
99-
if let Some(i) = self.def_ids.get(&did) {
100-
return *i;
101-
} else {
102-
let id = self.def_ids.len();
103-
self.def_ids.insert(did, stable_mir::DefId(id));
104-
stable_mir::DefId(id)
105-
}
101+
self.def_ids.entry(did)
106102
}
107103

108104
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
109-
if let Some(i) = self.alloc_ids.get(&aid) {
110-
return *i;
111-
} else {
112-
let id = self.def_ids.len();
113-
self.alloc_ids.insert(aid, stable_mir::AllocId(id));
114-
stable_mir::AllocId(id)
115-
}
105+
self.alloc_ids.entry(aid)
116106
}
117107

118108
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
119-
if let Some(i) = self.spans.get(&span) {
120-
return *i;
121-
} else {
122-
let id = self.spans.len();
123-
self.spans.insert(span, stable_mir::ty::Span(id));
124-
stable_mir::ty::Span(id)
125-
}
109+
self.spans.entry(span)
126110
}
127111
}
128112

@@ -134,9 +118,9 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
134118
stable_mir::run(
135119
Tables {
136120
tcx,
137-
def_ids: fx::FxIndexMap::default(),
138-
alloc_ids: fx::FxIndexMap::default(),
139-
spans: fx::FxIndexMap::default(),
121+
def_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
122+
alloc_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
123+
spans: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
140124
types: vec![],
141125
},
142126
f,
@@ -201,3 +185,26 @@ where
201185
})
202186
}
203187
}
188+
189+
/// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra
190+
/// safety features added.
191+
pub struct IndexMap<K, V> {
192+
index_map: fx::FxIndexMap<K, V>,
193+
}
194+
195+
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexToVal> IndexMap<K, V> {
196+
/// Because we are using values as indexes, this first get's the
197+
/// key using index of the value provided, then asserts that value
198+
/// we got from IndexMap is equal to one we provided.
199+
pub fn get_index_and_assert(&self, index: usize, value: V) -> &K {
200+
let (k, v) = self.index_map.get_index(index).unwrap();
201+
assert_eq!(*v, value, "Provided value doesn't match with indexed value");
202+
k
203+
}
204+
205+
pub fn entry(&mut self, key: K) -> V {
206+
let len = self.index_map.len();
207+
let v = self.index_map.entry(key).or_insert(V::to_val(len));
208+
*v
209+
}
210+
}

Diff for: compiler/rustc_smir/src/rustc_smir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10+
use crate::rustc_internal::IndexMap;
1011
use crate::rustc_smir::hir::def::DefKind;
1112
use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
12-
use rustc_data_structures::fx::FxIndexMap;
1313
use rustc_hir as hir;
1414
use rustc_middle::mir;
1515
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@@ -195,9 +195,9 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
195195

196196
pub struct Tables<'tcx> {
197197
pub tcx: TyCtxt<'tcx>,
198-
pub def_ids: FxIndexMap<DefId, stable_mir::DefId>,
199-
pub alloc_ids: FxIndexMap<AllocId, stable_mir::AllocId>,
200-
pub spans: FxIndexMap<rustc_span::Span, Span>,
198+
pub def_ids: IndexMap<DefId, stable_mir::DefId>,
199+
pub alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
200+
pub spans: IndexMap<rustc_span::Span, Span>,
201201
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
202202
}
203203

Diff for: compiler/stable_mir/src/lib.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use std::fmt;
2222
use std::fmt::Debug;
2323

2424
use self::ty::{
25-
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
25+
GenericPredicates, Generics, ImplDef, ImplTrait, IndexToVal, Span, TraitDecl, TraitDef, Ty,
26+
TyKind,
2627
};
2728

2829
#[macro_use]
@@ -52,10 +53,28 @@ impl Debug for DefId {
5253
}
5354
}
5455

56+
impl IndexToVal for DefId {
57+
fn to_val(index: usize) -> Self
58+
where
59+
Self: std::marker::Sized,
60+
{
61+
DefId(index)
62+
}
63+
}
64+
5565
/// A unique identification number for each provenance
5666
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
5767
pub struct AllocId(pub usize);
5868

69+
impl IndexToVal for AllocId {
70+
fn to_val(index: usize) -> Self
71+
where
72+
Self: std::marker::Sized,
73+
{
74+
AllocId(index)
75+
}
76+
}
77+
5978
/// A list of crate items.
6079
pub type CrateItems = Vec<CrateItem>;
6180

Diff for: compiler/stable_mir/src/ty.rs

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ impl Debug for Span {
8686
}
8787
}
8888

89+
impl IndexToVal for Span {
90+
fn to_val(index: usize) -> Self
91+
where
92+
Self: std::marker::Sized,
93+
{
94+
Span(index)
95+
}
96+
}
97+
8998
#[derive(Clone, Debug)]
9099
pub enum TyKind {
91100
RigidTy(RigidTy),
@@ -565,3 +574,9 @@ pub enum ImplPolarity {
565574
Negative,
566575
Reservation,
567576
}
577+
578+
pub trait IndexToVal {
579+
fn to_val(index: usize) -> Self
580+
where
581+
Self: std::marker::Sized;
582+
}

0 commit comments

Comments
 (0)