From b58546d27dea9dc24ff101181d07d529ebc412a5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 7 Oct 2022 16:46:16 +0200 Subject: [PATCH 1/2] sync thread_local key conditions exactly with what the macro uses --- std/src/sys/unix/thread_local_dtor.rs | 1 + std/src/sys/unsupported/thread_local_dtor.rs | 1 + std/src/thread/local.rs | 7 +++-- std/src/thread/mod.rs | 33 +++++++++++++++----- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/std/src/sys/unix/thread_local_dtor.rs b/std/src/sys/unix/thread_local_dtor.rs index 6e8be2a91..d7fd2130f 100644 --- a/std/src/sys/unix/thread_local_dtor.rs +++ b/std/src/sys/unix/thread_local_dtor.rs @@ -17,6 +17,7 @@ target_os = "redox", target_os = "emscripten" ))] +#[cfg_attr(target_family = "wasm", allow(unused))] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten) pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { use crate::mem; use crate::sys_common::thread_local_dtor::register_dtor_fallback; diff --git a/std/src/sys/unsupported/thread_local_dtor.rs b/std/src/sys/unsupported/thread_local_dtor.rs index 85d660983..84660ea58 100644 --- a/std/src/sys/unsupported/thread_local_dtor.rs +++ b/std/src/sys/unsupported/thread_local_dtor.rs @@ -1,5 +1,6 @@ #![unstable(feature = "thread_local_internals", issue = "none")] +#[cfg_attr(target_family = "wasm", allow(unused))] // unused on wasm32-unknown-unknown pub unsafe fn register_dtor(_t: *mut u8, _dtor: unsafe extern "C" fn(*mut u8)) { // FIXME: right now there is no concept of "thread exit", but this is likely // going to show up at some point in the form of an exported symbol that the diff --git a/std/src/thread/local.rs b/std/src/thread/local.rs index ffd17dc99..5d267891b 100644 --- a/std/src/thread/local.rs +++ b/std/src/thread/local.rs @@ -901,7 +901,7 @@ pub mod statik { } #[doc(hidden)] -#[cfg(target_thread_local)] +#[cfg(all(target_thread_local, not(all(target_family = "wasm", not(target_feature = "atomics"))),))] pub mod fast { use super::lazy::LazyKeyInner; use crate::cell::Cell; @@ -1037,7 +1037,10 @@ pub mod fast { } #[doc(hidden)] -#[cfg(not(target_thread_local))] +#[cfg(all( + not(target_thread_local), + not(all(target_family = "wasm", not(target_feature = "atomics"))), +))] pub mod os { use super::lazy::LazyKeyInner; use crate::cell::Cell; diff --git a/std/src/thread/mod.rs b/std/src/thread/mod.rs index 2de7da379..4749f5c4a 100644 --- a/std/src/thread/mod.rs +++ b/std/src/thread/mod.rs @@ -192,32 +192,49 @@ pub use scoped::{scope, Scope, ScopedJoinHandle}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::local::{AccessError, LocalKey}; -// Select the type used by the thread_local! macro to access TLS keys. There -// are three types: "static", "fast", "OS". The "OS" thread local key +// Provide the type used by the thread_local! macro to access TLS keys. This +// needs to be kept in sync with the macro itself (in `local.rs`). +// There are three types: "static", "fast", "OS". The "OS" thread local key // type is accessed via platform-specific API calls and is slow, while the "fast" // key type is accessed via code generated via LLVM, where TLS keys are set up // by the elf linker. "static" is for single-threaded platforms where a global // static is sufficient. #[unstable(feature = "libstd_thread_internals", issue = "none")] -#[cfg(target_thread_local)] #[cfg(not(test))] +#[cfg(all( + target_thread_local, + not(all(target_family = "wasm", not(target_feature = "atomics"))), +))] #[doc(hidden)] pub use self::local::fast::Key as __FastLocalKeyInner; + +// when building for tests, use real std's type #[unstable(feature = "libstd_thread_internals", issue = "none")] -#[cfg(target_thread_local)] -#[cfg(test)] // when building for tests, use real std's key +#[cfg(test)] +#[cfg(all( + target_thread_local, + not(all(target_family = "wasm", not(target_feature = "atomics"))), +))] pub use realstd::thread::__FastLocalKeyInner; +// but import the local one anyway to silence 'unused' warnings #[unstable(feature = "libstd_thread_internals", issue = "none")] -#[cfg(target_thread_local)] #[cfg(test)] -pub use self::local::fast::Key as __FastLocalKeyInnerUnused; // we import this anyway to silence 'unused' warnings +#[cfg(all( + target_thread_local, + not(all(target_family = "wasm", not(target_feature = "atomics"))), +))] +pub use self::local::fast::Key as __FastLocalKeyInnerUnused; #[unstable(feature = "libstd_thread_internals", issue = "none")] +#[cfg(all( + not(target_thread_local), + not(all(target_family = "wasm", not(target_feature = "atomics"))), +))] #[doc(hidden)] -#[cfg(not(target_thread_local))] pub use self::local::os::Key as __OsLocalKeyInner; + #[unstable(feature = "libstd_thread_internals", issue = "none")] #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] #[doc(hidden)] From 1fd20367347d88feb228e73c28f929056e78704e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 8 Oct 2022 17:11:45 +0200 Subject: [PATCH 2/2] smarter way to avoid 'unused' warning when building for tests --- std/src/thread/mod.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/std/src/thread/mod.rs b/std/src/thread/mod.rs index 4749f5c4a..bbcc2f8b4 100644 --- a/std/src/thread/mod.rs +++ b/std/src/thread/mod.rs @@ -150,6 +150,8 @@ #![stable(feature = "rust1", since = "1.0.0")] #![deny(unsafe_op_in_unsafe_fn)] +// Under `test`, `__FastLocalKeyInner` seems unused. +#![cfg_attr(test, allow(dead_code))] #[cfg(all(test, not(target_os = "emscripten")))] mod tests; @@ -218,15 +220,6 @@ pub use self::local::fast::Key as __FastLocalKeyInner; ))] pub use realstd::thread::__FastLocalKeyInner; -// but import the local one anyway to silence 'unused' warnings -#[unstable(feature = "libstd_thread_internals", issue = "none")] -#[cfg(test)] -#[cfg(all( - target_thread_local, - not(all(target_family = "wasm", not(target_feature = "atomics"))), -))] -pub use self::local::fast::Key as __FastLocalKeyInnerUnused; - #[unstable(feature = "libstd_thread_internals", issue = "none")] #[cfg(all( not(target_thread_local),