From 4e2be14986b022dfdf5efa73293566cc6faf570e Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sun, 13 Aug 2017 16:42:10 +0300 Subject: [PATCH] Make the LocalKey facade of thread_local! inlineable cross-crate. --- src/libstd/thread/local.rs | 16 +++++++++++++--- .../compile-fail/macro-local-data-key-priv.rs | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs index 02347bf4906a6..4ee8132f55cee 100644 --- a/src/libstd/thread/local.rs +++ b/src/libstd/thread/local.rs @@ -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); } } diff --git a/src/test/compile-fail/macro-local-data-key-priv.rs b/src/test/compile-fail/macro-local-data-key-priv.rs index 3818c8f7754a6..3ae629cd89529 100644 --- a/src/test/compile-fail/macro-local-data-key-priv.rs +++ b/src/test/compile-fail/macro-local-data-key-priv.rs @@ -16,5 +16,5 @@ mod bar { fn main() { bar::baz.with(|_| ()); - //~^ ERROR static `baz` is private + //~^ ERROR `baz` is private }