Skip to content

Commit 6c4f0bf

Browse files
committed
Stop using unsafe code in TLS macro expansion (fixes rust-lang#30756)
1 parent 9c30f12 commit 6c4f0bf

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/libstd/thread/local.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub struct LocalKey<T: 'static> {
9292
// trivially devirtualizable by LLVM because the value of `inner` never
9393
// changes and the constant should be readonly within a crate. This mainly
9494
// only runs into problems when TLS statics are exported across crates.
95-
inner: unsafe fn() -> Option<&'static UnsafeCell<Option<T>>>,
95+
inner: fn() -> Option<&'static UnsafeCell<Option<T>>>,
9696

9797
// initialization routine to invoke to create a value
9898
init: fn() -> T,
@@ -126,7 +126,7 @@ macro_rules! __thread_local_inner {
126126
($t:ty, $init:expr) => {{
127127
fn __init() -> $t { $init }
128128

129-
unsafe fn __getit() -> $crate::option::Option<
129+
fn __getit() -> $crate::option::Option<
130130
&'static $crate::cell::UnsafeCell<
131131
$crate::option::Option<$t>>>
132132
{
@@ -183,7 +183,7 @@ impl<T: 'static> LocalKey<T> {
183183
#[unstable(feature = "thread_local_internals",
184184
reason = "recently added to create a key",
185185
issue = "0")]
186-
pub const fn new(inner: unsafe fn() -> Option<&'static UnsafeCell<Option<T>>>,
186+
pub const fn new(inner: fn() -> Option<&'static UnsafeCell<Option<T>>>,
187187
init: fn() -> T) -> LocalKey<T> {
188188
LocalKey {
189189
inner: inner,
@@ -303,11 +303,13 @@ pub mod elf {
303303
}
304304
}
305305

306-
pub unsafe fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
307-
if intrinsics::needs_drop::<T>() && self.dtor_running.get() {
308-
return None
306+
pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
307+
unsafe {
308+
if intrinsics::needs_drop::<T>() && self.dtor_running.get() {
309+
return None
310+
}
311+
self.register_dtor();
309312
}
310-
self.register_dtor();
311313
Some(&self.inner)
312314
}
313315

@@ -452,24 +454,26 @@ pub mod os {
452454
}
453455
}
454456

455-
pub unsafe fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
456-
let ptr = self.os.get() as *mut Value<T>;
457-
if !ptr.is_null() {
458-
if ptr as usize == 1 {
459-
return None
457+
pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
458+
unsafe {
459+
let ptr = self.os.get() as *mut Value<T>;
460+
if !ptr.is_null() {
461+
if ptr as usize == 1 {
462+
return None
463+
}
464+
return Some(&(*ptr).value);
460465
}
461-
return Some(&(*ptr).value);
462-
}
463466

464-
// If the lookup returned null, we haven't initialized our own local
465-
// copy, so do that now.
466-
let ptr: Box<Value<T>> = box Value {
467-
key: self,
468-
value: UnsafeCell::new(None),
469-
};
470-
let ptr = Box::into_raw(ptr);
471-
self.os.set(ptr as *mut u8);
472-
Some(&(*ptr).value)
467+
// If the lookup returned null, we haven't initialized our own local
468+
// copy, so do that now.
469+
let ptr: Box<Value<T>> = box Value {
470+
key: self,
471+
value: UnsafeCell::new(None),
472+
};
473+
let ptr = Box::into_raw(ptr);
474+
self.os.set(ptr as *mut u8);
475+
Some(&(*ptr).value)
476+
}
473477
}
474478
}
475479

0 commit comments

Comments
 (0)