From d6fdadb8e93401b4c98763e90c2e181390d0bfd5 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 3 Oct 2025 00:00:39 -0400 Subject: [PATCH] Mitigate `thread_local!` shadowing issues Mitigates https://github.com/rust-lang/rust/issues/147006 and https://github.com/rust-lang/rust/issues/99018 --- .../std/src/sys/thread_local/native/mod.rs | 26 +++++++++---------- .../std/src/sys/thread_local/no_threads.rs | 16 ++++++------ library/std/src/sys/thread_local/os.rs | 8 +++--- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/library/std/src/sys/thread_local/native/mod.rs b/library/std/src/sys/thread_local/native/mod.rs index 5dc142408047e..38b373be56c9d 100644 --- a/library/std/src/sys/thread_local/native/mod.rs +++ b/library/std/src/sys/thread_local/native/mod.rs @@ -55,7 +55,7 @@ pub macro thread_local_inner { // Used to generate the `LocalKey` value for const-initialized thread locals. (@key $t:ty, $(#[$align_attr:meta])*, const $init:expr) => {{ - const __INIT: $t = $init; + const __RUST_STD_INTERNAL_INIT: $t = $init; unsafe { $crate::thread::LocalKey::new(const { @@ -63,16 +63,16 @@ pub macro thread_local_inner { |_| { #[thread_local] $(#[$align_attr])* - static VAL: $crate::thread::local_impl::EagerStorage<$t> - = $crate::thread::local_impl::EagerStorage::new(__INIT); - VAL.get() + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::EagerStorage<$t> + = $crate::thread::local_impl::EagerStorage::new(__RUST_STD_INTERNAL_INIT); + __RUST_STD_INTERNAL_VAL.get() } } else { |_| { #[thread_local] $(#[$align_attr])* - static VAL: $t = __INIT; - &VAL + static __RUST_STD_INTERNAL_VAL: $t = __RUST_STD_INTERNAL_INIT; + &__RUST_STD_INTERNAL_VAL } } }) @@ -82,27 +82,27 @@ pub macro thread_local_inner { // used to generate the `LocalKey` value for `thread_local!` (@key $t:ty, $(#[$align_attr:meta])*, $init:expr) => {{ #[inline] - fn __init() -> $t { + fn __rust_std_internal_init_fn() -> $t { $init } unsafe { $crate::thread::LocalKey::new(const { if $crate::mem::needs_drop::<$t>() { - |init| { + |__rust_std_internal_init| { #[thread_local] $(#[$align_attr])* - static VAL: $crate::thread::local_impl::LazyStorage<$t, ()> + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t, ()> = $crate::thread::local_impl::LazyStorage::new(); - VAL.get_or_init(init, __init) + __RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init, __rust_std_internal_init_fn) } } else { - |init| { + |__rust_std_internal_init| { #[thread_local] $(#[$align_attr])* - static VAL: $crate::thread::local_impl::LazyStorage<$t, !> + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t, !> = $crate::thread::local_impl::LazyStorage::new(); - VAL.get_or_init(init, __init) + __RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init, __rust_std_internal_init_fn) } } }) diff --git a/library/std/src/sys/thread_local/no_threads.rs b/library/std/src/sys/thread_local/no_threads.rs index 409dfb19518d9..936d464be9f1c 100644 --- a/library/std/src/sys/thread_local/no_threads.rs +++ b/library/std/src/sys/thread_local/no_threads.rs @@ -13,15 +13,15 @@ use crate::ptr; pub macro thread_local_inner { // used to generate the `LocalKey` value for const-initialized thread locals (@key $t:ty, $(#[$align_attr:meta])*, const $init:expr) => {{ - const __INIT: $t = $init; + const __RUST_STD_INTERNAL_INIT: $t = $init; // NOTE: Please update the shadowing test in `tests/thread.rs` if these types are renamed. unsafe { $crate::thread::LocalKey::new(|_| { $(#[$align_attr])* - static VAL: $crate::thread::local_impl::EagerStorage<$t> = - $crate::thread::local_impl::EagerStorage { value: __INIT }; - &VAL.value + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::EagerStorage<$t> = + $crate::thread::local_impl::EagerStorage { value: __RUST_STD_INTERNAL_INIT }; + &__RUST_STD_INTERNAL_VAL.value }) } }}, @@ -29,13 +29,13 @@ pub macro thread_local_inner { // used to generate the `LocalKey` value for `thread_local!` (@key $t:ty, $(#[$align_attr:meta])*, $init:expr) => {{ #[inline] - fn __init() -> $t { $init } + fn __rust_std_internal_init_fn() -> $t { $init } unsafe { - $crate::thread::LocalKey::new(|init| { + $crate::thread::LocalKey::new(|__rust_std_internal_init| { $(#[$align_attr])* - static VAL: $crate::thread::local_impl::LazyStorage<$t> = $crate::thread::local_impl::LazyStorage::new(); - VAL.get(init, __init) + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t> = $crate::thread::local_impl::LazyStorage::new(); + __RUST_STD_INTERNAL_VAL.get(__rust_std_internal_init, __rust_std_internal_init_fn) }) } }}, diff --git a/library/std/src/sys/thread_local/os.rs b/library/std/src/sys/thread_local/os.rs index 88bb5ae7c650d..9f7a29236e926 100644 --- a/library/std/src/sys/thread_local/os.rs +++ b/library/std/src/sys/thread_local/os.rs @@ -21,14 +21,14 @@ pub macro thread_local_inner { // used to generate the `LocalKey` value for `thread_local!`. (@key $t:ty, $($(#[$($align_attr:tt)*])+)?, $init:expr) => {{ #[inline] - fn __init() -> $t { $init } + fn __rust_std_internal_init_fn() -> $t { $init } // NOTE: this cannot import `LocalKey` or `Storage` with a `use` because that can shadow // user provided type or type alias with a matching name. Please update the shadowing test // in `tests/thread.rs` if these types are renamed. unsafe { - $crate::thread::LocalKey::new(|init| { - static VAL: $crate::thread::local_impl::Storage<$t, { + $crate::thread::LocalKey::new(|__rust_std_internal_init| { + static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::Storage<$t, { $({ // Ensure that attributes have valid syntax // and that the proper feature gate is enabled @@ -43,7 +43,7 @@ pub macro thread_local_inner { final_align }> = $crate::thread::local_impl::Storage::new(); - VAL.get(init, __init) + __RUST_STD_INTERNAL_VAL.get(__rust_std_internal_init, __rust_std_internal_init_fn) }) } }},