Skip to content

Commit

Permalink
Rollup merge of rust-lang#90774 - alexcrichton:tweak-const, r=m-ou-se
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
matthiaskrgr authored Nov 17, 2021
2 parents 227c239 + 1ac5d7d commit 3a55f2f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ macro_rules! __thread_local_inner {
#[cfg_attr(not(windows), inline)] // see comments below
unsafe fn __getit() -> $crate::option::Option<&'static $t> {
const _REQUIRE_UNSTABLE: () = $crate::thread::require_unstable_const_init_thread_local();
const INIT_EXPR: $t = $init;

// wasm without atomics maps directly to `static mut`, and dtors
// aren't implemented because thread dtors aren't really a thing
Expand All @@ -174,7 +175,7 @@ macro_rules! __thread_local_inner {
// block.
#[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
{
static mut VAL: $t = $init;
static mut VAL: $t = INIT_EXPR;
Some(&VAL)
}

Expand All @@ -184,18 +185,17 @@ macro_rules! __thread_local_inner {
not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
))]
{
#[thread_local]
static mut VAL: $t = INIT_EXPR;

// If a dtor isn't needed we can do something "very raw" and
// just get going.
if !$crate::mem::needs_drop::<$t>() {
#[thread_local]
static mut VAL: $t = $init;
unsafe {
return Some(&VAL)
}
}

#[thread_local]
static mut VAL: $t = $init;
// 0 == dtor not registered
// 1 == dtor registered, dtor not run
// 2 == dtor registered and is running or has run
Expand Down Expand Up @@ -242,7 +242,7 @@ macro_rules! __thread_local_inner {
))]
{
#[inline]
const fn __init() -> $t { $init }
const fn __init() -> $t { INIT_EXPR }
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
$crate::thread::__OsLocalKeyInner::new();
#[allow(unused_unsafe)]
Expand Down

0 comments on commit 3a55f2f

Please sign in to comment.