Skip to content

Commit 65a9cb6

Browse files
committed
std: use ptr::dangling_mut() in thread locals and related code
1 parent 21d085a commit 65a9cb6

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/libstd/io/lazy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<T: Send + Sync + 'static> Lazy<T> {
3737
let ptr = self.ptr.get();
3838
let ret = if ptr.is_null() {
3939
Some(self.init())
40-
} else if ptr as usize == 1 {
40+
} else if ptr == ptr::dangling_mut() {
4141
None
4242
} else {
4343
Some((*ptr).clone())
@@ -55,7 +55,7 @@ impl<T: Send + Sync + 'static> Lazy<T> {
5555
let registered = sys_common::at_exit(move || {
5656
self.lock.lock();
5757
let ptr = self.ptr.get();
58-
self.ptr.set(1 as *mut _);
58+
self.ptr.set(ptr::dangling_mut());
5959
self.lock.unlock();
6060
drop(Box::from_raw(ptr))
6161
});

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301
#![feature(placement_in_syntax)]
302302
#![feature(placement_new_protocol)]
303303
#![feature(prelude_import)]
304+
#![feature(ptr_dangling)]
304305
#![feature(rand)]
305306
#![feature(raw)]
306307
#![feature(repr_align)]

src/libstd/sys_common/thread_local.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! ```ignore (cannot-doctest-private-modules)
3737
//! let key = Key::new(None);
3838
//! assert!(key.get().is_null());
39-
//! key.set(1 as *mut u8);
39+
//! key.set(std::ptr::dangling_mut::<u8>());
4040
//! assert!(!key.get().is_null());
4141
//!
4242
//! drop(key); // deallocate this TLS slot.
@@ -50,7 +50,7 @@
5050
//!
5151
//! unsafe {
5252
//! assert!(KEY.get().is_null());
53-
//! KEY.set(1 as *mut u8);
53+
//! KEY.set(std::ptr::dangling_mut::<u8>());
5454
//! }
5555
//! ```
5656
@@ -81,7 +81,7 @@ use sys_common::mutex::Mutex;
8181
///
8282
/// unsafe {
8383
/// assert!(KEY.get().is_null());
84-
/// KEY.set(1 as *mut u8);
84+
/// KEY.set(std::ptr::dangling_mut::<u8>());
8585
/// }
8686
/// ```
8787
pub struct StaticKey {
@@ -110,7 +110,7 @@ pub struct StaticKey {
110110
///
111111
/// let key = Key::new(None);
112112
/// assert!(key.get().is_null());
113-
/// key.set(1 as *mut u8);
113+
/// key.set(std::ptr::dangling_mut::<u8>());
114114
/// assert!(!key.get().is_null());
115115
///
116116
/// drop(key); // deallocate this TLS slot.

src/libstd/thread/local.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub mod os {
492492
pub unsafe fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
493493
let ptr = self.os.get() as *mut Value<T>;
494494
if !ptr.is_null() {
495-
if ptr as usize == 1 {
495+
if ptr == ptr::dangling_mut() {
496496
return None
497497
}
498498
return Some(&(*ptr).value);
@@ -520,7 +520,7 @@ pub mod os {
520520
// before we return from the destructor ourselves.
521521
let ptr = Box::from_raw(ptr as *mut Value<T>);
522522
let key = ptr.key;
523-
key.os.set(1 as *mut u8);
523+
key.os.set(ptr::dangling_mut());
524524
drop(ptr);
525525
key.os.set(ptr::null_mut());
526526
}

0 commit comments

Comments
 (0)