Skip to content

Commit

Permalink
make trait two ways turn usizes private
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Oct 10, 2023
1 parent 470f3af commit 59e2bb5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
37 changes: 20 additions & 17 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_middle::mir::interpret::AllocId;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::Span;
use stable_mir::ty::IndexToVal;
use stable_mir::ty::IndexedVal;
use stable_mir::CompilerError;
use std::fmt::Debug;
use std::hash::Hash;
Expand All @@ -23,7 +23,7 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {

#[inline(always)]
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
&self.def_ids.get_index_and_assert(index.0, index)
&self.def_ids[index]
}
}

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

#[inline(always)]
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
&self.spans.get_index_and_assert(index.0, index)
&self.spans[index]
}
}

Expand Down Expand Up @@ -98,15 +98,15 @@ impl<'tcx> Tables<'tcx> {
}

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

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

pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
self.spans.entry(span)
self.spans.create_or_fetch(span)
}
}

Expand Down Expand Up @@ -192,19 +192,22 @@ pub struct IndexMap<K, V> {
index_map: fx::FxIndexMap<K, V>,
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexToVal> IndexMap<K, V> {
/// Because we are using values as indexes, this first get's the
/// key using index of the value provided, then asserts that value
/// we got from IndexMap is equal to one we provided.
pub fn get_index_and_assert(&self, index: usize, value: V) -> &K {
let (k, v) = self.index_map.get_index(index).unwrap();
assert_eq!(*v, value, "Provided value doesn't match with indexed value");
k
}

pub fn entry(&mut self, key: K) -> V {
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
pub fn create_or_fetch(&mut self, key: K) -> V {
let len = self.index_map.len();
let v = self.index_map.entry(key).or_insert(V::to_val(len));
*v
}
}

impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
for IndexMap<K, V>
{
type Output = K;

fn index(&self, index: V) -> &Self::Output {
let (k, v) = self.index_map.get_index(index.to_index()).unwrap();
assert_eq!(*v, index, "Provided value doesn't match with indexed value");
k
}
}
27 changes: 14 additions & 13 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt;
use std::fmt::Debug;

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, IndexToVal, Span, TraitDecl, TraitDef, Ty,
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, Span, TraitDecl, TraitDef, Ty,
TyKind,
};

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

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

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

impl IndexToVal for DefId {
fn to_val(index: usize) -> Self
where
Self: std::marker::Sized,
{
impl IndexedVal for DefId {
fn to_val(index: usize) -> Self {
DefId(index)
}

fn to_index(&self) -> usize {
self.0
}
}

/// A unique identification number for each provenance
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AllocId(pub usize);
pub struct AllocId(usize);

impl IndexToVal for AllocId {
fn to_val(index: usize) -> Self
where
Self: std::marker::Sized,
{
impl IndexedVal for AllocId {
fn to_val(index: usize) -> Self {
AllocId(index)
}
fn to_index(&self) -> usize {
self.0
}
}

/// A list of crate items.
Expand Down
20 changes: 10 additions & 10 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct Placeholder<T> {
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span(pub usize);
pub struct Span(usize);

impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand All @@ -86,13 +86,13 @@ impl Debug for Span {
}
}

impl IndexToVal for Span {
fn to_val(index: usize) -> Self
where
Self: std::marker::Sized,
{
impl IndexedVal for Span {
fn to_val(index: usize) -> Self {
Span(index)
}
fn to_index(&self) -> usize {
self.0
}
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -575,8 +575,8 @@ pub enum ImplPolarity {
Reservation,
}

pub trait IndexToVal {
fn to_val(index: usize) -> Self
where
Self: std::marker::Sized;
pub trait IndexedVal {
fn to_val(index: usize) -> Self;

fn to_index(&self) -> usize;
}

0 comments on commit 59e2bb5

Please sign in to comment.