Skip to content

Commit 7b349d2

Browse files
committed
support pub(restricted) in thread_local!
1 parent 5516bcc commit 7b349d2

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

src/libstd/thread/local.rs

+43-32
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
111111
/// # Syntax
112112
///
113113
/// The macro wraps any number of static declarations and makes them thread local.
114-
/// Each static may be public or private, and attributes are allowed. Example:
114+
/// Publicity and attributes for each static are allowed. Example:
115115
///
116116
/// ```
117117
/// use std::cell::RefCell;
@@ -130,31 +130,40 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
130130
#[stable(feature = "rust1", since = "1.0.0")]
131131
#[allow_internal_unstable]
132132
macro_rules! thread_local {
133-
// rule 0: empty (base case for the recursion)
133+
// empty (base case for the recursion)
134134
() => {};
135135

136-
// rule 1: process multiple declarations where the first one is private
136+
// process multiple declarations where the first one is private
137137
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
138-
thread_local!($(#[$attr])* static $name: $t = $init); // go to rule 2
138+
__thread_local_inner!($(#[$attr])* [] $name, $t, $init);
139139
thread_local!($($rest)*);
140140
);
141141

142-
// rule 2: handle a single private declaration
142+
// handle a single private declaration
143143
($(#[$attr:meta])* static $name:ident: $t:ty = $init:expr) => (
144-
$(#[$attr])* static $name: $crate::thread::LocalKey<$t> =
145-
__thread_local_inner!($t, $init);
144+
__thread_local_inner!($(#[$attr])* [] $name, $t, $init);
146145
);
147146

148-
// rule 3: handle multiple declarations where the first one is public
147+
// handle multiple declarations where the first one is public
149148
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
150-
thread_local!($(#[$attr])* pub static $name: $t = $init); // go to rule 4
149+
__thread_local_inner!($(#[$attr])* [pub] $name, $t, $init);
151150
thread_local!($($rest)*);
152151
);
153152

154-
// rule 4: handle a single public declaration
153+
// handle a single public declaration
155154
($(#[$attr:meta])* pub static $name:ident: $t:ty = $init:expr) => (
156-
$(#[$attr])* pub static $name: $crate::thread::LocalKey<$t> =
157-
__thread_local_inner!($t, $init);
155+
__thread_local_inner!($(#[$attr])* [pub] $name, $t, $init);
156+
);
157+
158+
// handle multiple declarations where the first one is restricted public
159+
($(#[$attr:meta])* pub $vis:tt static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
160+
__thread_local_inner!($(#[$attr])* [pub $vis] $name, $t, $init);
161+
thread_local!($($rest)*);
162+
);
163+
164+
// handle a single restricted public declaration
165+
($(#[$attr:meta])* pub $vis:tt static $name:ident: $t:ty = $init:expr) => (
166+
__thread_local_inner!($(#[$attr])* [pub $vis] $name, $t, $init);
158167
);
159168
}
160169

@@ -165,27 +174,29 @@ macro_rules! thread_local {
165174
#[macro_export]
166175
#[allow_internal_unstable]
167176
macro_rules! __thread_local_inner {
168-
($t:ty, $init:expr) => {{
169-
fn __init() -> $t { $init }
170-
171-
fn __getit() -> $crate::option::Option<
172-
&'static $crate::cell::UnsafeCell<
173-
$crate::option::Option<$t>>>
174-
{
175-
#[thread_local]
176-
#[cfg(target_thread_local)]
177-
static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
178-
$crate::thread::__FastLocalKeyInner::new();
179-
180-
#[cfg(not(target_thread_local))]
181-
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
182-
$crate::thread::__OsLocalKeyInner::new();
183-
184-
__KEY.get()
185-
}
177+
($(#[$attr:meta])* [$($vis:tt)*] $name:ident, $t:ty, $init:expr) => {
178+
$(#[$attr])* $($vis)* static $name: $crate::thread::LocalKey<$t> = {
179+
fn __init() -> $t { $init }
180+
181+
fn __getit() -> $crate::option::Option<
182+
&'static $crate::cell::UnsafeCell<
183+
$crate::option::Option<$t>>>
184+
{
185+
#[thread_local]
186+
#[cfg(target_thread_local)]
187+
static __KEY: $crate::thread::__FastLocalKeyInner<$t> =
188+
$crate::thread::__FastLocalKeyInner::new();
189+
190+
#[cfg(not(target_thread_local))]
191+
static __KEY: $crate::thread::__OsLocalKeyInner<$t> =
192+
$crate::thread::__OsLocalKeyInner::new();
193+
194+
__KEY.get()
195+
}
186196

187-
$crate::thread::LocalKey::new(__getit, __init)
188-
}}
197+
$crate::thread::LocalKey::new(__getit, __init)
198+
};
199+
}
189200
}
190201

191202
/// Indicator of the state of a thread local storage key.

src/test/run-pass/thread-local-syntax.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@
1111
#![deny(missing_docs)]
1212
//! this tests the syntax of `thread_local!`
1313
14-
thread_local! {
15-
// no docs
16-
#[allow(unused)]
17-
static FOO: i32 = 42;
18-
/// docs
19-
pub static BAR: String = String::from("bar");
14+
mod foo {
15+
mod bar {
16+
thread_local! {
17+
// no docs
18+
#[allow(unused)]
19+
static FOO: i32 = 42;
20+
/// docs
21+
pub static BAR: String = String::from("bar");
22+
23+
// look at these restrictions!!
24+
pub(crate) static BAZ: usize = 0;
25+
pub(in foo) static QUUX: usize = 0;
26+
}
27+
thread_local!(static SPLOK: u32 = 0);
28+
}
2029
}
21-
thread_local!(static BAZ: u32 = 0);
2230

2331
fn main() {}

0 commit comments

Comments
 (0)