Skip to content

Commit b017808

Browse files
authored
Rollup merge of #147289 - Jules-Bertholet:thread_local-shadow-mitigate, r=joboet
Mitigate `thread_local!` shadowing issues Mitigates #147006 and #99018. `@rustbot` label T-libs A-macros A-thread-locals A-hygiene
2 parents e1af13b + d6fdadb commit b017808

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

library/std/src/sys/thread_local/native/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,24 @@ pub macro thread_local_inner {
5555

5656
// Used to generate the `LocalKey` value for const-initialized thread locals.
5757
(@key $t:ty, $(#[$align_attr:meta])*, const $init:expr) => {{
58-
const __INIT: $t = $init;
58+
const __RUST_STD_INTERNAL_INIT: $t = $init;
5959

6060
unsafe {
6161
$crate::thread::LocalKey::new(const {
6262
if $crate::mem::needs_drop::<$t>() {
6363
|_| {
6464
#[thread_local]
6565
$(#[$align_attr])*
66-
static VAL: $crate::thread::local_impl::EagerStorage<$t>
67-
= $crate::thread::local_impl::EagerStorage::new(__INIT);
68-
VAL.get()
66+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::EagerStorage<$t>
67+
= $crate::thread::local_impl::EagerStorage::new(__RUST_STD_INTERNAL_INIT);
68+
__RUST_STD_INTERNAL_VAL.get()
6969
}
7070
} else {
7171
|_| {
7272
#[thread_local]
7373
$(#[$align_attr])*
74-
static VAL: $t = __INIT;
75-
&VAL
74+
static __RUST_STD_INTERNAL_VAL: $t = __RUST_STD_INTERNAL_INIT;
75+
&__RUST_STD_INTERNAL_VAL
7676
}
7777
}
7878
})
@@ -82,27 +82,27 @@ pub macro thread_local_inner {
8282
// used to generate the `LocalKey` value for `thread_local!`
8383
(@key $t:ty, $(#[$align_attr:meta])*, $init:expr) => {{
8484
#[inline]
85-
fn __init() -> $t {
85+
fn __rust_std_internal_init_fn() -> $t {
8686
$init
8787
}
8888

8989
unsafe {
9090
$crate::thread::LocalKey::new(const {
9191
if $crate::mem::needs_drop::<$t>() {
92-
|init| {
92+
|__rust_std_internal_init| {
9393
#[thread_local]
9494
$(#[$align_attr])*
95-
static VAL: $crate::thread::local_impl::LazyStorage<$t, ()>
95+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t, ()>
9696
= $crate::thread::local_impl::LazyStorage::new();
97-
VAL.get_or_init(init, __init)
97+
__RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init, __rust_std_internal_init_fn)
9898
}
9999
} else {
100-
|init| {
100+
|__rust_std_internal_init| {
101101
#[thread_local]
102102
$(#[$align_attr])*
103-
static VAL: $crate::thread::local_impl::LazyStorage<$t, !>
103+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t, !>
104104
= $crate::thread::local_impl::LazyStorage::new();
105-
VAL.get_or_init(init, __init)
105+
__RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init, __rust_std_internal_init_fn)
106106
}
107107
}
108108
})

library/std/src/sys/thread_local/no_threads.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ use crate::ptr;
1313
pub macro thread_local_inner {
1414
// used to generate the `LocalKey` value for const-initialized thread locals
1515
(@key $t:ty, $(#[$align_attr:meta])*, const $init:expr) => {{
16-
const __INIT: $t = $init;
16+
const __RUST_STD_INTERNAL_INIT: $t = $init;
1717

1818
// NOTE: Please update the shadowing test in `tests/thread.rs` if these types are renamed.
1919
unsafe {
2020
$crate::thread::LocalKey::new(|_| {
2121
$(#[$align_attr])*
22-
static VAL: $crate::thread::local_impl::EagerStorage<$t> =
23-
$crate::thread::local_impl::EagerStorage { value: __INIT };
24-
&VAL.value
22+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::EagerStorage<$t> =
23+
$crate::thread::local_impl::EagerStorage { value: __RUST_STD_INTERNAL_INIT };
24+
&__RUST_STD_INTERNAL_VAL.value
2525
})
2626
}
2727
}},
2828

2929
// used to generate the `LocalKey` value for `thread_local!`
3030
(@key $t:ty, $(#[$align_attr:meta])*, $init:expr) => {{
3131
#[inline]
32-
fn __init() -> $t { $init }
32+
fn __rust_std_internal_init_fn() -> $t { $init }
3333

3434
unsafe {
35-
$crate::thread::LocalKey::new(|init| {
35+
$crate::thread::LocalKey::new(|__rust_std_internal_init| {
3636
$(#[$align_attr])*
37-
static VAL: $crate::thread::local_impl::LazyStorage<$t> = $crate::thread::local_impl::LazyStorage::new();
38-
VAL.get(init, __init)
37+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::LazyStorage<$t> = $crate::thread::local_impl::LazyStorage::new();
38+
__RUST_STD_INTERNAL_VAL.get(__rust_std_internal_init, __rust_std_internal_init_fn)
3939
})
4040
}
4141
}},

library/std/src/sys/thread_local/os.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ pub macro thread_local_inner {
2121
// used to generate the `LocalKey` value for `thread_local!`.
2222
(@key $t:ty, $($(#[$($align_attr:tt)*])+)?, $init:expr) => {{
2323
#[inline]
24-
fn __init() -> $t { $init }
24+
fn __rust_std_internal_init_fn() -> $t { $init }
2525

2626
// NOTE: this cannot import `LocalKey` or `Storage` with a `use` because that can shadow
2727
// user provided type or type alias with a matching name. Please update the shadowing test
2828
// in `tests/thread.rs` if these types are renamed.
2929
unsafe {
30-
$crate::thread::LocalKey::new(|init| {
31-
static VAL: $crate::thread::local_impl::Storage<$t, {
30+
$crate::thread::LocalKey::new(|__rust_std_internal_init| {
31+
static __RUST_STD_INTERNAL_VAL: $crate::thread::local_impl::Storage<$t, {
3232
$({
3333
// Ensure that attributes have valid syntax
3434
// and that the proper feature gate is enabled
@@ -43,7 +43,7 @@ pub macro thread_local_inner {
4343
final_align
4444
}>
4545
= $crate::thread::local_impl::Storage::new();
46-
VAL.get(init, __init)
46+
__RUST_STD_INTERNAL_VAL.get(__rust_std_internal_init, __rust_std_internal_init_fn)
4747
})
4848
}
4949
}},

0 commit comments

Comments
 (0)