Rollup merge of #147289 - Jules-Bertholet:thread_local-shadow-mitigate, r=joboet

Mitigate `thread_local!` shadowing issues

Mitigates https://github.com/rust-lang/rust/issues/147006 and https://github.com/rust-lang/rust/issues/99018.

`@rustbot` label T-libs A-macros A-thread-locals A-hygiene
This commit is contained in:
dianqk 2025-10-11 07:05:56 +08:00 committed by GitHub
commit b01780801a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 25 deletions

View file

@ -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)
}
}
})

View file

@ -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)
})
}
}},

View file

@ -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)
})
}
}},