@@ -100,28 +100,28 @@ pub trait Context {
100
100
fn rustc_tables ( & mut self , f : & mut dyn FnMut ( & mut Tables < ' _ > ) ) ;
101
101
}
102
102
103
- thread_local ! {
104
- /// A thread local variable that stores a pointer to the tables mapping between TyCtxt
105
- /// datastructures and stable MIR datastructures.
106
- static TLV : Cell <* mut ( ) > = const { Cell :: new( std:: ptr:: null_mut( ) ) } ;
107
- }
103
+ // A thread local variable that stores a pointer to the tables mapping between TyCtxt
104
+ // datastructures and stable MIR datastructures
105
+ scoped_thread_local ! ( static TLV : Cell <* mut ( ) >) ;
108
106
109
107
pub fn run ( mut context : impl Context , f : impl FnOnce ( ) ) {
110
- assert ! ( TLV . get ( ) . is_null ( ) ) ;
108
+ assert ! ( ! TLV . is_set ( ) ) ;
111
109
fn g < ' a > ( mut context : & mut ( dyn Context + ' a ) , f : impl FnOnce ( ) ) {
112
- TLV . set ( & mut context as * mut & mut _ as _ ) ;
113
- f ( ) ;
114
- TLV . replace ( std:: ptr:: null_mut ( ) ) ;
110
+ let ptr: * mut ( ) = & mut context as * mut & mut _ as _ ;
111
+ TLV . set ( & Cell :: new ( ptr) , || {
112
+ f ( ) ;
113
+ } ) ;
115
114
}
116
115
g ( & mut context, f) ;
117
116
}
118
117
119
118
/// Loads the current context and calls a function with it.
120
119
/// Do not nest these, as that will ICE.
121
120
pub ( crate ) fn with < R > ( f : impl FnOnce ( & mut dyn Context ) -> R ) -> R {
122
- let ptr = TLV . replace ( std:: ptr:: null_mut ( ) ) as * mut & mut dyn Context ;
123
- assert ! ( !ptr. is_null( ) ) ;
124
- let ret = f ( unsafe { * ptr } ) ;
125
- TLV . set ( ptr as _ ) ;
126
- ret
121
+ assert ! ( TLV . is_set( ) ) ;
122
+ TLV . with ( |tlv| {
123
+ let ptr = tlv. get ( ) ;
124
+ assert ! ( !ptr. is_null( ) ) ;
125
+ f ( unsafe { * ( ptr as * mut & mut dyn Context ) } )
126
+ } )
127
127
}
0 commit comments