diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs index d1f5cf81c038d..69395001bbfdc 100644 --- a/src/libstd/thread/local.rs +++ b/src/libstd/thread/local.rs @@ -92,7 +92,7 @@ pub struct LocalKey { // trivially devirtualizable by LLVM because the value of `inner` never // changes and the constant should be readonly within a crate. This mainly // only runs into problems when TLS statics are exported across crates. - inner: unsafe fn() -> Option<&'static UnsafeCell>>, + inner: fn() -> Option<&'static UnsafeCell>>, // initialization routine to invoke to create a value init: fn() -> T, @@ -126,7 +126,7 @@ macro_rules! __thread_local_inner { ($t:ty, $init:expr) => {{ fn __init() -> $t { $init } - unsafe fn __getit() -> $crate::option::Option< + fn __getit() -> $crate::option::Option< &'static $crate::cell::UnsafeCell< $crate::option::Option<$t>>> { @@ -183,7 +183,7 @@ impl LocalKey { #[unstable(feature = "thread_local_internals", reason = "recently added to create a key", issue = "0")] - pub const fn new(inner: unsafe fn() -> Option<&'static UnsafeCell>>, + pub const fn new(inner: fn() -> Option<&'static UnsafeCell>>, init: fn() -> T) -> LocalKey { LocalKey { inner: inner, @@ -303,11 +303,13 @@ pub mod elf { } } - pub unsafe fn get(&'static self) -> Option<&'static UnsafeCell>> { - if intrinsics::needs_drop::() && self.dtor_running.get() { - return None + pub fn get(&'static self) -> Option<&'static UnsafeCell>> { + unsafe { + if intrinsics::needs_drop::() && self.dtor_running.get() { + return None + } + self.register_dtor(); } - self.register_dtor(); Some(&self.inner) } @@ -452,24 +454,26 @@ pub mod os { } } - pub unsafe fn get(&'static self) -> Option<&'static UnsafeCell>> { - let ptr = self.os.get() as *mut Value; - if !ptr.is_null() { - if ptr as usize == 1 { - return None + pub fn get(&'static self) -> Option<&'static UnsafeCell>> { + unsafe { + let ptr = self.os.get() as *mut Value; + if !ptr.is_null() { + if ptr as usize == 1 { + return None + } + return Some(&(*ptr).value); } - return Some(&(*ptr).value); - } - // If the lookup returned null, we haven't initialized our own local - // copy, so do that now. - let ptr: Box> = box Value { - key: self, - value: UnsafeCell::new(None), - }; - let ptr = Box::into_raw(ptr); - self.os.set(ptr as *mut u8); - Some(&(*ptr).value) + // If the lookup returned null, we haven't initialized our own local + // copy, so do that now. + let ptr: Box> = box Value { + key: self, + value: UnsafeCell::new(None), + }; + let ptr = Box::into_raw(ptr); + self.os.set(ptr as *mut u8); + Some(&(*ptr).value) + } } } diff --git a/src/test/run-pass/issue-30756.rs b/src/test/run-pass/issue-30756.rs new file mode 100644 index 0000000000000..d21b42f8c876e --- /dev/null +++ b/src/test/run-pass/issue-30756.rs @@ -0,0 +1,16 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![deny(unsafe_code)] + +thread_local!(static FOO: u8 = 1); + +fn main() { +}