File tree 3 files changed +14
-15
lines changed
3 files changed +14
-15
lines changed Original file line number Diff line number Diff line change 1
1
//! Ensure that thread-local statics get deallocated when the thread dies.
2
2
3
3
#![ feature( thread_local) ]
4
- // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
5
- #! [ allow ( static_mut_refs ) ]
4
+
5
+ use std :: ptr :: addr_of ;
6
6
7
7
#[ thread_local]
8
8
static mut TLS : u8 = 0 ;
@@ -12,7 +12,7 @@ unsafe impl Send for SendRaw {}
12
12
13
13
fn main ( ) {
14
14
unsafe {
15
- let dangling_ptr = std:: thread:: spawn ( || SendRaw ( & TLS as * const u8 ) ) . join ( ) . unwrap ( ) ;
15
+ let dangling_ptr = std:: thread:: spawn ( || SendRaw ( addr_of ! ( TLS ) ) ) . join ( ) . unwrap ( ) ;
16
16
let _val = * dangling_ptr. 0 ; //~ ERROR: has been freed
17
17
}
18
18
}
Original file line number Diff line number Diff line change
1
+ use std:: ptr:: addr_of;
2
+
1
3
static mut FOO : i32 = 42 ;
2
4
3
- // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
4
- #[ allow( static_mut_refs) ]
5
- static BAR : Foo = Foo ( unsafe { & FOO as * const _ } ) ;
5
+ static BAR : Foo = Foo ( unsafe { addr_of ! ( FOO ) } ) ;
6
6
7
7
#[ allow( dead_code) ]
8
8
struct Foo ( * const i32 ) ;
Original file line number Diff line number Diff line change 8
8
//! test, we also check that thread-locals act as per-thread statics.
9
9
10
10
#![ feature( thread_local) ]
11
- // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
12
- #![ allow( static_mut_refs) ]
13
11
12
+ use std:: ptr:: addr_of_mut;
14
13
use std:: thread;
15
14
16
15
#[ thread_local]
@@ -23,8 +22,8 @@ static mut C: u8 = 0;
23
22
#[ thread_local]
24
23
static READ_ONLY : u8 = 42 ;
25
24
26
- unsafe fn get_a_ref ( ) -> * mut u8 {
27
- & mut A
25
+ unsafe fn get_a_ptr ( ) -> * mut u8 {
26
+ addr_of_mut ! ( A )
28
27
}
29
28
30
29
struct Sender ( * mut u8 ) ;
@@ -35,12 +34,12 @@ fn main() {
35
34
let _val = READ_ONLY ;
36
35
37
36
let ptr = unsafe {
38
- let x = get_a_ref ( ) ;
37
+ let x = get_a_ptr ( ) ;
39
38
* x = 5 ;
40
39
assert_eq ! ( A , 5 ) ;
41
40
B = 15 ;
42
41
C = 25 ;
43
- Sender ( & mut A )
42
+ Sender ( addr_of_mut ! ( A ) )
44
43
} ;
45
44
46
45
thread:: spawn ( move || unsafe {
@@ -51,18 +50,18 @@ fn main() {
51
50
assert_eq ! ( C , 25 ) ;
52
51
B = 14 ;
53
52
C = 24 ;
54
- let y = get_a_ref ( ) ;
53
+ let y = get_a_ptr ( ) ;
55
54
assert_eq ! ( * y, 0 ) ;
56
55
* y = 4 ;
57
56
assert_eq ! ( * ptr. 0 , 5 ) ;
58
57
assert_eq ! ( A , 4 ) ;
59
- assert_eq ! ( * get_a_ref ( ) , 4 ) ;
58
+ assert_eq ! ( * get_a_ptr ( ) , 4 ) ;
60
59
} )
61
60
. join ( )
62
61
. unwrap ( ) ;
63
62
64
63
unsafe {
65
- assert_eq ! ( * get_a_ref ( ) , 5 ) ;
64
+ assert_eq ! ( * get_a_ptr ( ) , 5 ) ;
66
65
assert_eq ! ( A , 5 ) ;
67
66
assert_eq ! ( B , 15 ) ;
68
67
assert_eq ! ( C , 24 ) ;
You can’t perform that action at this time.
0 commit comments