Skip to content

Commit 109e74a

Browse files
committed
refactor: move IndexMap to rustc_smir
we should no longer keep `IndexMap` in `rustc_internal`, as we've decided to migrate `rustc_internal` to `stable_mir` under a feature (#140532).
1 parent 3c4c145 commit 109e74a

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
//! until stable MIR is complete.
55
66
use std::cell::{Cell, RefCell};
7-
use std::fmt::Debug;
8-
use std::hash::Hash;
9-
use std::ops::Index;
107

11-
use rustc_data_structures::fx;
12-
use rustc_data_structures::fx::FxIndexMap;
138
use rustc_middle::ty::TyCtxt;
149
use rustc_span::def_id::CrateNum;
1510
use scoped_tls::scoped_thread_local;
1611
use stable_mir::Error;
1712
use stable_mir::convert::{RustcInternal, Stable};
1813

1914
use crate::rustc_smir::context::SmirCtxt;
20-
use crate::rustc_smir::{Bridge, IndexedVal, SmirContainer, Tables};
15+
use crate::rustc_smir::{Bridge, SmirContainer, Tables};
2116
use crate::stable_mir;
2217

2318
pub mod pretty;
@@ -273,35 +268,3 @@ macro_rules! run_driver {
273268
StableMir::new($callback).run($args)
274269
}};
275270
}
276-
277-
/// Similar to rustc's `FxIndexMap`, `IndexMap` with extra
278-
/// safety features added.
279-
pub struct IndexMap<K, V> {
280-
index_map: fx::FxIndexMap<K, V>,
281-
}
282-
283-
impl<K, V> Default for IndexMap<K, V> {
284-
fn default() -> Self {
285-
Self { index_map: FxIndexMap::default() }
286-
}
287-
}
288-
289-
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
290-
pub fn create_or_fetch(&mut self, key: K) -> V {
291-
let len = self.index_map.len();
292-
let v = self.index_map.entry(key).or_insert(V::to_val(len));
293-
*v
294-
}
295-
}
296-
297-
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
298-
for IndexMap<K, V>
299-
{
300-
type Output = K;
301-
302-
fn index(&self, index: V) -> &Self::Output {
303-
let (k, v) = self.index_map.get_index(index.to_index()).unwrap();
304-
assert_eq!(*v, index, "Provided value doesn't match with indexed value");
305-
k
306-
}
307-
}

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
1010
use std::cell::RefCell;
1111
use std::fmt::Debug;
12+
use std::hash::Hash;
1213
use std::ops::Index;
1314

1415
use bridge::*;
1516
use context::SmirCtxt;
17+
use rustc_data_structures::fx::{self, FxIndexMap};
1618
use rustc_middle::mir;
1719
use rustc_middle::mir::interpret::AllocId;
1820
use rustc_middle::ty::{self, Ty, TyCtxt};
1921
use rustc_span::Span;
2022
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
2123

22-
use crate::rustc_internal::IndexMap;
23-
2424
pub mod alloc;
2525
pub mod bridge;
2626
mod builder;
@@ -233,6 +233,38 @@ pub trait IndexedVal {
233233
fn to_index(&self) -> usize;
234234
}
235235

236+
/// Similar to rustc's `FxIndexMap`, `IndexMap` with extra
237+
/// safety features added.
238+
pub struct IndexMap<K, V> {
239+
index_map: fx::FxIndexMap<K, V>,
240+
}
241+
242+
impl<K, V> Default for IndexMap<K, V> {
243+
fn default() -> Self {
244+
Self { index_map: FxIndexMap::default() }
245+
}
246+
}
247+
248+
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
249+
pub fn create_or_fetch(&mut self, key: K) -> V {
250+
let len = self.index_map.len();
251+
let v = self.index_map.entry(key).or_insert(V::to_val(len));
252+
*v
253+
}
254+
}
255+
256+
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
257+
for IndexMap<K, V>
258+
{
259+
type Output = K;
260+
261+
fn index(&self, index: V) -> &Self::Output {
262+
let (k, v) = self.index_map.get_index(index.to_index()).unwrap();
263+
assert_eq!(*v, index, "Provided value doesn't match with indexed value");
264+
k
265+
}
266+
}
267+
236268
/// Iterate over the definitions of the given crate.
237269
pub(crate) fn filter_def_ids<F, T>(tcx: TyCtxt<'_>, krate: CrateNum, mut func: F) -> Vec<T>
238270
where

0 commit comments

Comments
 (0)