Skip to content

Commit 59e2bb5

Browse files
committed
make trait two ways turn usizes private
1 parent 470f3af commit 59e2bb5

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::mir::interpret::AllocId;
1212
use rustc_middle::ty::TyCtxt;
1313
use rustc_span::def_id::{CrateNum, DefId};
1414
use rustc_span::Span;
15-
use stable_mir::ty::IndexToVal;
15+
use stable_mir::ty::IndexedVal;
1616
use stable_mir::CompilerError;
1717
use std::fmt::Debug;
1818
use std::hash::Hash;
@@ -23,7 +23,7 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
2323

2424
#[inline(always)]
2525
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
26-
&self.def_ids.get_index_and_assert(index.0, index)
26+
&self.def_ids[index]
2727
}
2828
}
2929

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

3333
#[inline(always)]
3434
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
35-
&self.spans.get_index_and_assert(index.0, index)
35+
&self.spans[index]
3636
}
3737
}
3838

@@ -98,15 +98,15 @@ impl<'tcx> Tables<'tcx> {
9898
}
9999

100100
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
101-
self.def_ids.entry(did)
101+
self.def_ids.create_or_fetch(did)
102102
}
103103

104104
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
105-
self.alloc_ids.entry(aid)
105+
self.alloc_ids.create_or_fetch(aid)
106106
}
107107

108108
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
109-
self.spans.entry(span)
109+
self.spans.create_or_fetch(span)
110110
}
111111
}
112112

@@ -192,19 +192,22 @@ pub struct IndexMap<K, V> {
192192
index_map: fx::FxIndexMap<K, V>,
193193
}
194194

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 {
195+
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
196+
pub fn create_or_fetch(&mut self, key: K) -> V {
206197
let len = self.index_map.len();
207198
let v = self.index_map.entry(key).or_insert(V::to_val(len));
208199
*v
209200
}
210201
}
202+
203+
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
204+
for IndexMap<K, V>
205+
{
206+
type Output = K;
207+
208+
fn index(&self, index: V) -> &Self::Output {
209+
let (k, v) = self.index_map.get_index(index.to_index()).unwrap();
210+
assert_eq!(*v, index, "Provided value doesn't match with indexed value");
211+
k
212+
}
213+
}

compiler/stable_mir/src/lib.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::fmt;
2222
use std::fmt::Debug;
2323

2424
use self::ty::{
25-
GenericPredicates, Generics, ImplDef, ImplTrait, IndexToVal, Span, TraitDecl, TraitDef, Ty,
25+
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty,
2626
TyKind,
2727
};
2828

@@ -42,7 +42,7 @@ pub type CrateNum = usize;
4242

4343
/// A unique identification number for each item accessible for the current compilation unit.
4444
#[derive(Clone, Copy, PartialEq, Eq)]
45-
pub struct DefId(pub usize);
45+
pub struct DefId(usize);
4646

4747
impl Debug for DefId {
4848
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -53,26 +53,27 @@ impl Debug for DefId {
5353
}
5454
}
5555

56-
impl IndexToVal for DefId {
57-
fn to_val(index: usize) -> Self
58-
where
59-
Self: std::marker::Sized,
60-
{
56+
impl IndexedVal for DefId {
57+
fn to_val(index: usize) -> Self {
6158
DefId(index)
6259
}
60+
61+
fn to_index(&self) -> usize {
62+
self.0
63+
}
6364
}
6465

6566
/// A unique identification number for each provenance
6667
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
67-
pub struct AllocId(pub usize);
68+
pub struct AllocId(usize);
6869

69-
impl IndexToVal for AllocId {
70-
fn to_val(index: usize) -> Self
71-
where
72-
Self: std::marker::Sized,
73-
{
70+
impl IndexedVal for AllocId {
71+
fn to_val(index: usize) -> Self {
7472
AllocId(index)
7573
}
74+
fn to_index(&self) -> usize {
75+
self.0
76+
}
7677
}
7778

7879
/// A list of crate items.

compiler/stable_mir/src/ty.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub struct Placeholder<T> {
7575
}
7676

7777
#[derive(Clone, Copy, PartialEq, Eq)]
78-
pub struct Span(pub usize);
78+
pub struct Span(usize);
7979

8080
impl Debug for Span {
8181
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@@ -86,13 +86,13 @@ 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-
{
89+
impl IndexedVal for Span {
90+
fn to_val(index: usize) -> Self {
9491
Span(index)
9592
}
93+
fn to_index(&self) -> usize {
94+
self.0
95+
}
9696
}
9797

9898
#[derive(Clone, Debug)]
@@ -575,8 +575,8 @@ pub enum ImplPolarity {
575575
Reservation,
576576
}
577577

578-
pub trait IndexToVal {
579-
fn to_val(index: usize) -> Self
580-
where
581-
Self: std::marker::Sized;
578+
pub trait IndexedVal {
579+
fn to_val(index: usize) -> Self;
580+
581+
fn to_index(&self) -> usize;
582582
}

0 commit comments

Comments
 (0)