mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
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 https://github.com/rust-lang/rust/pull/43931#issuecomment-323534214
This commit is contained in:
commit
d7d75eff30
@ -159,8 +159,9 @@ macro_rules! thread_local {
|
||||
#[allow_internal_unstable]
|
||||
#[allow_internal_unsafe]
|
||||
macro_rules! __thread_local_inner {
|
||||
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
$(#[$attr])* $vis static $name: $crate::thread::LocalKey<$t> = {
|
||||
(@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
{
|
||||
#[inline]
|
||||
fn __init() -> $t { $init }
|
||||
|
||||
unsafe fn __getit() -> $crate::option::Option<
|
||||
@ -182,7 +183,16 @@ macro_rules! __thread_local_inner {
|
||||
unsafe {
|
||||
$crate::thread::LocalKey::new(__getit, __init)
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
|
||||
#[cfg(stage0)]
|
||||
$(#[$attr])* $vis static $name: $crate::thread::LocalKey<$t> =
|
||||
__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
$(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
|
||||
__thread_local_inner!(@key $(#[$attr])* $vis $name, $t, $init);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,5 +16,5 @@ mod bar {
|
||||
|
||||
fn main() {
|
||||
bar::baz.with(|_| ());
|
||||
//~^ ERROR static `baz` is private
|
||||
//~^ ERROR `baz` is private
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user