@@ -13,22 +13,21 @@ pub macro thread_local_inner {
13
13
( @key $t: ty, const $init: expr) => { {
14
14
#[ inline]
15
15
#[ deny( unsafe_op_in_unsafe_fn) ]
16
- // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
17
- #[ cfg_attr( bootstrap, allow( static_mut_ref) ) ]
18
- #[ cfg_attr( not( bootstrap) , allow( static_mut_refs) ) ]
19
16
unsafe fn __getit (
20
17
_init : $crate:: option:: Option < & mut $crate:: option:: Option < $t> > ,
21
18
) -> $crate:: option:: Option < & ' static $t> {
22
19
const INIT_EXPR : $t = $init;
23
20
// If the platform has support for `#[thread_local]`, use it.
24
21
#[ thread_local]
25
- static mut VAL : $t = INIT_EXPR ;
22
+ // We use `UnsafeCell` here instead of `static mut` to ensure any generated TLS shims
23
+ // have a nonnull attribute on their return value.
24
+ static VAL : $crate:: cell:: UnsafeCell < $t> = $crate:: cell:: UnsafeCell :: new ( INIT_EXPR ) ;
26
25
27
26
// If a dtor isn't needed we can do something "very raw" and
28
27
// just get going.
29
28
if !$crate:: mem:: needs_drop :: < $t> ( ) {
30
29
unsafe {
31
- return $crate:: option:: Option :: Some ( & VAL )
30
+ return $crate:: option:: Option :: Some ( & * VAL . get ( ) )
32
31
}
33
32
}
34
33
@@ -55,15 +54,15 @@ pub macro thread_local_inner {
55
54
// so now.
56
55
0 => {
57
56
$crate:: thread:: local_impl:: Key :: < $t> :: register_dtor (
58
- $crate :: ptr :: addr_of_mut! ( VAL ) as * mut $crate:: primitive:: u8 ,
57
+ VAL . get ( ) as * mut $crate:: primitive:: u8 ,
59
58
destroy,
60
59
) ;
61
60
STATE . set ( 1 ) ;
62
- $crate:: option:: Option :: Some ( & VAL )
61
+ $crate:: option:: Option :: Some ( & * VAL . get ( ) )
63
62
}
64
63
// 1 == the destructor is registered and the value
65
64
// is valid, so return the pointer.
66
- 1 => $crate:: option:: Option :: Some ( & VAL ) ,
65
+ 1 => $crate:: option:: Option :: Some ( & * VAL . get ( ) ) ,
67
66
// otherwise the destructor has already run, so we
68
67
// can't give access.
69
68
_ => $crate:: option:: Option :: None ,
0 commit comments