@@ -22,10 +22,9 @@ use stable_mir::compiler_interface::SmirInterface;
22
22
use stable_mir:: ty:: IndexedVal ;
23
23
24
24
use crate :: rustc_smir:: context:: SmirCtxt ;
25
- use crate :: rustc_smir:: { Stable , Tables } ;
25
+ use crate :: rustc_smir:: { Bridge , IndexedVal , SmirContainer , Tables } ;
26
26
use crate :: stable_mir;
27
27
28
- mod internal;
29
28
pub mod pretty;
30
29
31
30
/// Convert an internal Rust compiler item into its stable counterpart, if one exists.
@@ -40,7 +39,7 @@ pub mod pretty;
40
39
///
41
40
/// This function will panic if StableMIR has not been properly initialized.
42
41
pub fn stable < ' tcx , S : Stable < ' tcx > > ( item : S ) -> S :: T {
43
- with_tables ( |tables| item. stable ( tables) )
42
+ with_container ( |tables, cx | item. stable ( tables, cx ) )
44
43
}
45
44
46
45
/// Convert a stable item into its internal Rust compiler counterpart, if one exists.
@@ -54,12 +53,11 @@ pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
54
53
/// # Panics
55
54
///
56
55
/// This function will panic if StableMIR has not been properly initialized.
57
- pub fn internal < ' tcx , S > ( tcx : TyCtxt < ' tcx > , item : S ) -> S :: T < ' tcx >
56
+ pub fn internal < ' tcx , S > ( item : S ) -> S :: T < ' tcx >
58
57
where
59
58
S : RustcInternal ,
60
59
{
61
- // The tcx argument ensures that the item won't outlive the type context.
62
- with_tables ( |tables| item. internal ( tables, tcx) )
60
+ with_container ( |tables, cx| item. internal ( tables, cx) )
63
61
}
64
62
65
63
impl < ' tcx , B : Bridge > Index < B :: DefId > for Tables < ' tcx , B > {
@@ -101,49 +99,39 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
101
99
// datastructures and stable MIR datastructures
102
100
scoped_thread_local ! ( static TLV : Cell <* const ( ) >) ;
103
101
104
- pub ( crate ) fn init < ' tcx , F , T > ( cx : & SmirCtxt < ' tcx > , f : F ) -> T
102
+ pub ( crate ) fn init < ' tcx , F , T , B : Bridge > ( container : & SmirContainer < ' tcx , B > , f : F ) -> T
105
103
where
106
104
F : FnOnce ( ) -> T ,
107
105
{
108
106
assert ! ( !TLV . is_set( ) ) ;
109
- let ptr = cx as * const _ as * const ( ) ;
107
+ let ptr = container as * const _ as * const ( ) ;
110
108
TLV . set ( & Cell :: new ( ptr) , || f ( ) )
111
109
}
112
110
113
111
/// Loads the current context and calls a function with it.
114
112
/// Do not nest these, as that will ICE.
115
- pub ( crate ) fn with_tables < R > ( f : impl for < ' tcx > FnOnce ( & mut Tables < ' tcx > ) -> R ) -> R {
113
+ pub ( crate ) fn with_container < ' tcx , R , B : Bridge > (
114
+ f : impl FnOnce ( & mut Tables < ' tcx , B > , & SmirCtxt < ' tcx , B > ) -> R ,
115
+ ) -> R {
116
116
assert ! ( TLV . is_set( ) ) ;
117
117
TLV . with ( |tlv| {
118
118
let ptr = tlv. get ( ) ;
119
119
assert ! ( !ptr. is_null( ) ) ;
120
- let context = ptr as * const SmirCtxt < ' _ > ;
121
- let mut tables = unsafe { ( * context) . 0 . borrow_mut ( ) } ;
122
- f ( & mut * tables)
120
+ let container = ptr as * const SmirContainer < ' tcx , B > ;
121
+ let mut tables = unsafe { ( * container) . tables . borrow_mut ( ) } ;
122
+ let cx = unsafe { ( * container) . cx . borrow ( ) } ;
123
+ f ( & mut * tables, & * cx)
123
124
} )
124
125
}
125
126
126
127
pub fn run < F , T > ( tcx : TyCtxt < ' _ > , f : F ) -> Result < T , Error >
127
128
where
128
129
F : FnOnce ( ) -> T ,
129
130
{
130
- let tables = SmirCtxt ( RefCell :: new ( Tables {
131
- tcx,
132
- def_ids : IndexMap :: default ( ) ,
133
- alloc_ids : IndexMap :: default ( ) ,
134
- spans : IndexMap :: default ( ) ,
135
- types : IndexMap :: default ( ) ,
136
- instances : IndexMap :: default ( ) ,
137
- ty_consts : IndexMap :: default ( ) ,
138
- mir_consts : IndexMap :: default ( ) ,
139
- layouts : IndexMap :: default ( ) ,
140
- } ) ) ;
141
-
142
- let interface = SmirInterface { cx : tables } ;
143
-
144
- // Pass the `SmirInterface` to compiler_interface::run
145
- // and initialize the rustc-specific TLS with tables.
146
- stable_mir:: compiler_interface:: run ( & interface, || init ( & interface. cx , f) )
131
+ let smir_cx = RefCell :: new ( SmirCtxt :: new ( tcx) ) ;
132
+ let container = SmirContainer { tables : RefCell :: new ( Tables :: new ( tcx) ) , cx : smir_cx } ;
133
+
134
+ stable_mir:: compiler_interface:: run ( & container, || init ( & container, f) )
147
135
}
148
136
149
137
/// Instantiate and run the compiler with the provided arguments and callback.
0 commit comments