Skip to content

Commit c2f7232

Browse files
committed
Auto merge of #121303 - GrigorenkoPV:static_mut_refs, r=oli-obk,RalfJung
Get rid of some `#![allow(static_mut_refs)]`
2 parents c4c1126 + 1120433 commit c2f7232

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

tests/fail/tls/tls_static_dealloc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Ensure that thread-local statics get deallocated when the thread dies.
22
33
#![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;
66

77
#[thread_local]
88
static mut TLS: u8 = 0;
@@ -12,7 +12,7 @@ unsafe impl Send for SendRaw {}
1212

1313
fn main() {
1414
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();
1616
let _val = *dangling_ptr.0; //~ ERROR: has been freed
1717
}
1818
}

tests/pass/static_mut.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use std::ptr::addr_of;
2+
13
static mut FOO: i32 = 42;
24

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) });
66

77
#[allow(dead_code)]
88
struct Foo(*const i32);

tests/pass/tls/tls_static.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
//! test, we also check that thread-locals act as per-thread statics.
99
1010
#![feature(thread_local)]
11-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
12-
#![allow(static_mut_refs)]
1311

12+
use std::ptr::addr_of_mut;
1413
use std::thread;
1514

1615
#[thread_local]
@@ -23,8 +22,8 @@ static mut C: u8 = 0;
2322
#[thread_local]
2423
static READ_ONLY: u8 = 42;
2524

26-
unsafe fn get_a_ref() -> *mut u8 {
27-
&mut A
25+
unsafe fn get_a_ptr() -> *mut u8 {
26+
addr_of_mut!(A)
2827
}
2928

3029
struct Sender(*mut u8);
@@ -35,12 +34,12 @@ fn main() {
3534
let _val = READ_ONLY;
3635

3736
let ptr = unsafe {
38-
let x = get_a_ref();
37+
let x = get_a_ptr();
3938
*x = 5;
4039
assert_eq!(A, 5);
4140
B = 15;
4241
C = 25;
43-
Sender(&mut A)
42+
Sender(addr_of_mut!(A))
4443
};
4544

4645
thread::spawn(move || unsafe {
@@ -51,18 +50,18 @@ fn main() {
5150
assert_eq!(C, 25);
5251
B = 14;
5352
C = 24;
54-
let y = get_a_ref();
53+
let y = get_a_ptr();
5554
assert_eq!(*y, 0);
5655
*y = 4;
5756
assert_eq!(*ptr.0, 5);
5857
assert_eq!(A, 4);
59-
assert_eq!(*get_a_ref(), 4);
58+
assert_eq!(*get_a_ptr(), 4);
6059
})
6160
.join()
6261
.unwrap();
6362

6463
unsafe {
65-
assert_eq!(*get_a_ref(), 5);
64+
assert_eq!(*get_a_ptr(), 5);
6665
assert_eq!(A, 5);
6766
assert_eq!(B, 15);
6867
assert_eq!(C, 24);

0 commit comments

Comments
 (0)