Skip to content

Commit 548c108

Browse files
committed
Auto merge of rust-lang#90774 - alexcrichton:tweak-const, r=m-ou-se
std: Tweak expansion of thread-local const This commit tweaks the expansion of `thread_local!` when combined with a `const { ... }` value to help ensure that the rules which apply to `const { ... }` blocks will be the same as when they're stabilized. Previously with this invocation: thread_local!(static NAME: Type = const { init_expr }); this would generate (on supporting platforms): #[thread_local] static NAME: Type = init_expr; instead the macro now expands to: const INIT_EXPR: Type = init_expr; #[thread_local] static NAME: Type = INIT_EXPR; with the hope that because `init_expr` is defined as a `const` item then it's not accidentally allowing more behavior than if it were put into a `static`. For example on the stabilization issue [this example][ex] now gives the same error both ways. [ex]: rust-lang#84223 (comment)
2 parents cc946fc + 1ac5d7d commit 548c108

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

library/std/src/thread/local.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ macro_rules! __thread_local_inner {
165165
#[cfg_attr(not(windows), inline)] // see comments below
166166
unsafe fn __getit() -> $crate::option::Option<&'static $t> {
167167
const _REQUIRE_UNSTABLE: () = $crate::thread::require_unstable_const_init_thread_local();
168+
const INIT_EXPR: $t = $init;
168169

169170
// wasm without atomics maps directly to `static mut`, and dtors
170171
// aren't implemented because thread dtors aren't really a thing
@@ -174,7 +175,7 @@ macro_rules! __thread_local_inner {
174175
// block.
175176
#[cfg(all(target_family = "wasm", not(target_feature = "atomics")))]
176177
{
177-
static mut VAL: $t = $init;
178+
static mut VAL: $t = INIT_EXPR;
178179
Some(&VAL)
179180
}
180181

@@ -184,18 +185,17 @@ macro_rules! __thread_local_inner {
184185
not(all(target_family = "wasm", not(target_feature = "atomics"))),
185186
))]
186187
{
188+
#[thread_local]
189+
static mut VAL: $t = INIT_EXPR;
190+
187191
// If a dtor isn't needed we can do something "very raw" and
188192
// just get going.
189193
if !$crate::mem::needs_drop::<$t>() {
190-
#[thread_local]
191-
static mut VAL: $t = $init;
192194
unsafe {
193195
return Some(&VAL)
194196
}
195197
}
196198

197-
#[thread_local]
198-
static mut VAL: $t = $init;
199199
// 0 == dtor not registered
200200
// 1 == dtor registered, dtor not run
201201
// 2 == dtor registered and is running or has run
@@ -242,7 +242,7 @@ macro_rules! __thread_local_inner {
242242
))]
243243
{
244244
#[inline]
245-
const fn __init() -> $t { $init }
245+
const fn __init() -> $t { INIT_EXPR }
246246
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
247247
$crate::thread::__OsLocalKeyInner::new();
248248
#[allow(unused_unsafe)]

0 commit comments

Comments
 (0)