Skip to content

Commit d7d75ef

Browse files
committed
Auto merge of #43931 - eddyb:const-local-key, r=alexcrichton
Make the LocalKey facade of thread_local! inlineable cross-crate. Fixes (almost*) #25088 by changing the `LocalKey` `static` `thread_local!` generates to a `const`. This can be done because a `LocalKey` value holds no actual TLS data, only function pointers to get at said data, and it could even be made `Copy` without any negative consequences. The recent stabilization of rvalue promotion to `'static` allows doing this without changing the API. r? @alexcrichton *almost because we can't yet inline `__getit` because it breaks on MSVC, see #43931 (comment)
2 parents a6a9d4c + 4e2be14 commit d7d75ef

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/libstd/thread/local.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ macro_rules! thread_local {
159159
#[allow_internal_unstable]
160160
#[allow_internal_unsafe]
161161
macro_rules! __thread_local_inner {
162-
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
163-
$(#[$attr])* $vis static $name: $crate::thread::LocalKey<$t> = {
162+
(@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
163+
{
164+
#[inline]
164165
fn __init() -> $t { $init }
165166

166167
unsafe fn __getit() -> $crate::option::Option<
@@ -182,7 +183,16 @@ macro_rules! __thread_local_inner {
182183
unsafe {
183184
$crate::thread::LocalKey::new(__getit, __init)
184185
}
185-
};
186+
}
187+
};
188+
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
189+
#[cfg(stage0)]
190+
$(#[$attr])* $vis static $name: $crate::thread::LocalKey<$t> =
191+
__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
192+
193+
#[cfg(not(stage0))]
194+
$(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
195+
__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
186196
}
187197
}
188198

src/test/compile-fail/macro-local-data-key-priv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ mod bar {
1616

1717
fn main() {
1818
bar::baz.with(|_| ());
19-
//~^ ERROR static `baz` is private
19+
//~^ ERROR `baz` is private
2020
}

0 commit comments

Comments
 (0)