@@ -22,10 +22,9 @@ use stable_mir::compiler_interface::SmirInterface;
2222use stable_mir:: ty:: IndexedVal ;
2323
2424use crate :: rustc_smir:: context:: SmirCtxt ;
25- use crate :: rustc_smir:: { Stable , Tables } ;
25+ use crate :: rustc_smir:: { Bridge , IndexedVal , SmirContainer , Tables } ;
2626use crate :: stable_mir;
2727
28- mod internal;
2928pub mod pretty;
3029
3130/// Convert an internal Rust compiler item into its stable counterpart, if one exists.
@@ -40,7 +39,7 @@ pub mod pretty;
4039///
4140/// This function will panic if StableMIR has not been properly initialized.
4241pub 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 ) )
4443}
4544
4645/// 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 {
5453/// # Panics
5554///
5655/// 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 >
5857where
5958 S : RustcInternal ,
6059{
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) )
6361}
6462
6563impl < ' tcx , B : Bridge > Index < B :: DefId > for Tables < ' tcx , B > {
@@ -101,49 +99,39 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
10199// datastructures and stable MIR datastructures
102100scoped_thread_local ! ( static TLV : Cell <* const ( ) >) ;
103101
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
105103where
106104 F : FnOnce ( ) -> T ,
107105{
108106 assert ! ( !TLV . is_set( ) ) ;
109- let ptr = cx as * const _ as * const ( ) ;
107+ let ptr = container as * const _ as * const ( ) ;
110108 TLV . set ( & Cell :: new ( ptr) , || f ( ) )
111109}
112110
113111/// Loads the current context and calls a function with it.
114112/// 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 {
116116 assert ! ( TLV . is_set( ) ) ;
117117 TLV . with ( |tlv| {
118118 let ptr = tlv. get ( ) ;
119119 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)
123124 } )
124125}
125126
126127pub fn run < F , T > ( tcx : TyCtxt < ' _ > , f : F ) -> Result < T , Error >
127128where
128129 F : FnOnce ( ) -> T ,
129130{
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) )
147135}
148136
149137/// Instantiate and run the compiler with the provided arguments and callback.
0 commit comments