This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +24
-8
lines changed Expand file tree Collapse file tree 3 files changed +24
-8
lines changed Original file line number Diff line number Diff line change 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:: sync:: RwLock ;
7+
68use crate :: stable_mir;
79pub use rustc_span:: def_id:: { CrateNum , DefId } ;
810
11+ static DEF_ID_MAP : RwLock < Vec < DefId > > = RwLock :: new ( Vec :: new ( ) ) ;
12+
913pub 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)
1128}
1229
1330pub fn crate_num ( item : & stable_mir:: Crate ) -> CrateNum {
Original file line number Diff line number Diff line change 77//!
88//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10- use crate :: stable_mir:: { self } ;
10+ use crate :: {
11+ rustc_internal:: crate_item,
12+ stable_mir:: { self } ,
13+ } ;
1114use rustc_middle:: ty:: { tls:: with, TyCtxt } ;
1215use rustc_span:: def_id:: { CrateNum , LOCAL_CRATE } ;
1316use tracing:: debug;
@@ -34,9 +37,7 @@ pub fn find_crate(name: &str) -> Option<stable_mir::Crate> {
3437
3538/// Retrieve all items of the local crate that have a MIR associated with them.
3639pub 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 ( ) )
4041}
4142
4243/// Build a stable mir crate from a given crate number.
Original file line number Diff line number Diff line change 1111//! There shouldn't be any direct references to internal compiler constructs in this module.
1212//! If you need an internal construct, consider using `rustc_internal` or `rustc_smir`.
1313
14- use crate :: rustc_internal;
15-
1614/// Use String for now but we should replace it.
1715pub type Symbol = String ;
1816
@@ -37,7 +35,7 @@ pub struct Crate {
3735/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
3836/// use this item.
3937#[ derive( Clone , PartialEq , Eq , Debug ) ]
40- pub struct CrateItem ( pub ( crate ) rustc_internal :: DefId ) ;
38+ pub struct CrateItem ( pub ( crate ) DefId ) ;
4139
4240/// Access to the local crate.
4341pub fn local_crate ( ) -> Crate {
You can’t perform that action at this time.
0 commit comments