@@ -55,16 +55,16 @@ mod tlv {
55
55
/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs.
56
56
/// This is used to get the pointer to the current `ImplicitCtxt`.
57
57
#[ inline]
58
- pub ( super ) fn get_tlv ( ) -> usize {
59
- rayon_core:: tlv:: get ( )
58
+ pub ( super ) fn get_tlv ( ) -> * const ( ) {
59
+ ptr :: from_exposed_addr ( rayon_core:: tlv:: get ( ) )
60
60
}
61
61
62
62
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
63
63
/// to `value` during the call to `f`. It is restored to its previous value after.
64
64
/// This is used to set the pointer to the new `ImplicitCtxt`.
65
65
#[ inline]
66
- pub ( super ) fn with_tlv < F : FnOnce ( ) -> R , R > ( value : usize , f : F ) -> R {
67
- rayon_core:: tlv:: with ( value, f)
66
+ pub ( super ) fn with_tlv < F : FnOnce ( ) -> R , R > ( value : * const ( ) , f : F ) -> R {
67
+ rayon_core:: tlv:: with ( value. expose_addr ( ) , f)
68
68
}
69
69
}
70
70
@@ -75,34 +75,44 @@ mod tlv {
75
75
76
76
thread_local ! {
77
77
/// A thread local variable that stores a pointer to the current `ImplicitCtxt`.
78
- static TLV : Cell <usize > = const { Cell :: new( 0 ) } ;
78
+ static TLV : Cell <* const ( ) > = const { Cell :: new( ptr :: null ( ) ) } ;
79
79
}
80
80
81
81
/// Gets the pointer to the current `ImplicitCtxt`.
82
82
#[ inline]
83
- pub ( super ) fn get_tlv ( ) -> usize {
83
+ pub ( super ) fn get_tlv ( ) -> * const ( ) {
84
84
TLV . with ( |tlv| tlv. get ( ) )
85
85
}
86
86
87
87
/// Sets TLV to `value` during the call to `f`.
88
88
/// It is restored to its previous value after.
89
89
/// This is used to set the pointer to the new `ImplicitCtxt`.
90
90
#[ inline]
91
- pub ( super ) fn with_tlv < F : FnOnce ( ) -> R , R > ( value : usize , f : F ) -> R {
91
+ pub ( super ) fn with_tlv < F : FnOnce ( ) -> R , R > ( value : * const ( ) , f : F ) -> R {
92
92
let old = get_tlv ( ) ;
93
93
let _reset = rustc_data_structures:: OnDrop ( move || TLV . with ( |tlv| tlv. set ( old) ) ) ;
94
94
TLV . with ( |tlv| tlv. set ( value) ) ;
95
95
f ( )
96
96
}
97
97
}
98
98
99
+ #[ inline]
100
+ fn erase ( context : & ImplicitCtxt < ' _ , ' _ > ) -> * const ( ) {
101
+ context as * const _ as * const ( )
102
+ }
103
+
104
+ #[ inline]
105
+ unsafe fn downcast < ' a , ' tcx > ( context : * const ( ) ) -> & ' a ImplicitCtxt < ' a , ' tcx > {
106
+ & * ( context as * const ImplicitCtxt < ' a , ' tcx > )
107
+ }
108
+
99
109
/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
100
110
#[ inline]
101
111
pub fn enter_context < ' a , ' tcx , F , R > ( context : & ImplicitCtxt < ' a , ' tcx > , f : F ) -> R
102
112
where
103
113
F : FnOnce ( & ImplicitCtxt < ' a , ' tcx > ) -> R ,
104
114
{
105
- tlv:: with_tlv ( context as * const _ as usize , || f ( & context) )
115
+ tlv:: with_tlv ( erase ( context) , || f ( & context) )
106
116
}
107
117
108
118
/// Allows access to the current `ImplicitCtxt` in a closure if one is available.
@@ -112,14 +122,14 @@ where
112
122
F : for <' a , ' tcx > FnOnce ( Option < & ImplicitCtxt < ' a , ' tcx > > ) -> R ,
113
123
{
114
124
let context = tlv:: get_tlv ( ) ;
115
- if context == 0 {
125
+ if context. is_null ( ) {
116
126
f ( None )
117
127
} else {
118
128
// We could get an `ImplicitCtxt` pointer from another thread.
119
129
// Ensure that `ImplicitCtxt` is `Sync`.
120
130
sync:: assert_sync :: < ImplicitCtxt < ' _ , ' _ > > ( ) ;
121
131
122
- unsafe { f ( Some ( & * ( context as * const ImplicitCtxt < ' _ , ' _ > ) ) ) }
132
+ unsafe { f ( Some ( downcast ( context) ) ) }
123
133
}
124
134
}
125
135
0 commit comments