File tree 3 files changed +24
-8
lines changed
3 files changed +24
-8
lines changed Original file line number Diff line number Diff line change 3
3
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
4
4
//! until stable MIR is complete.
5
5
6
+ use std:: sync:: RwLock ;
7
+
6
8
use crate :: stable_mir;
7
9
pub use rustc_span:: def_id:: { CrateNum , DefId } ;
8
10
11
+ static DEF_ID_MAP : RwLock < Vec < DefId > > = RwLock :: new ( Vec :: new ( ) ) ;
12
+
9
13
pub fn item_def_id ( item : & stable_mir:: CrateItem ) -> DefId {
10
- item. 0
14
+ DEF_ID_MAP . read ( ) . unwrap ( ) [ item. 0 ]
15
+ }
16
+
17
+ pub fn crate_item ( did : DefId ) -> stable_mir:: CrateItem {
18
+ // FIXME: this becomes inefficient when we have too many ids
19
+ let mut map = DEF_ID_MAP . write ( ) . unwrap ( ) ;
20
+ for ( i, & d) in map. iter ( ) . enumerate ( ) {
21
+ if d == did {
22
+ return stable_mir:: CrateItem ( i) ;
23
+ }
24
+ }
25
+ let id = map. len ( ) ;
26
+ map. push ( did) ;
27
+ stable_mir:: CrateItem ( id)
11
28
}
12
29
13
30
pub fn crate_num ( item : & stable_mir:: Crate ) -> CrateNum {
Original file line number Diff line number Diff line change 7
7
//!
8
8
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
9
9
10
- use crate :: stable_mir:: { self } ;
10
+ use crate :: {
11
+ rustc_internal:: crate_item,
12
+ stable_mir:: { self } ,
13
+ } ;
11
14
use rustc_middle:: ty:: { tls:: with, TyCtxt } ;
12
15
use rustc_span:: def_id:: { CrateNum , LOCAL_CRATE } ;
13
16
use tracing:: debug;
@@ -34,9 +37,7 @@ pub fn find_crate(name: &str) -> Option<stable_mir::Crate> {
34
37
35
38
/// Retrieve all items of the local crate that have a MIR associated with them.
36
39
pub fn all_local_items ( ) -> stable_mir:: CrateItems {
37
- with ( |tcx| {
38
- tcx. mir_keys ( ( ) ) . iter ( ) . map ( |item| stable_mir:: CrateItem ( item. to_def_id ( ) ) ) . collect ( )
39
- } )
40
+ with ( |tcx| tcx. mir_keys ( ( ) ) . iter ( ) . map ( |item| crate_item ( item. to_def_id ( ) ) ) . collect ( ) )
40
41
}
41
42
42
43
/// Build a stable mir crate from a given crate number.
Original file line number Diff line number Diff line change 11
11
//! There shouldn't be any direct references to internal compiler constructs in this module.
12
12
//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
13
13
14
- use crate :: rustc_internal;
15
-
16
14
/// Use String for now but we should replace it.
17
15
pub type Symbol = String ;
18
16
@@ -37,7 +35,7 @@ pub struct Crate {
37
35
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
38
36
/// use this item.
39
37
#[ derive( Clone , PartialEq , Eq , Debug ) ]
40
- pub struct CrateItem ( pub ( crate ) rustc_internal :: DefId ) ;
38
+ pub struct CrateItem ( pub ( crate ) DefId ) ;
41
39
42
40
/// Access to the local crate.
43
41
pub fn local_crate ( ) -> Crate {
You can’t perform that action at this time.
0 commit comments