Skip to content

Commit 9d2f0a1

Browse files
committed
Improved support for visibility restrictions
1 parent 0f4821b commit 9d2f0a1

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

src/lib.rs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,12 @@ pub use core::ops::Deref as __Deref;
116116
#[cfg_attr(feature="nightly", allow_internal_unstable)]
117117
#[doc(hidden)]
118118
macro_rules! __lazy_static_internal {
119-
($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
120-
__lazy_static_internal!(@PRIV, $(#[$attr])* static ref $N : $T = $e; $($t)*);
121-
};
122-
($(#[$attr:meta])* pub(in $pub_in:path) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
123-
__lazy_static_internal!(@PUB_IN, $pub_in, $(#[$attr])* static ref $N : $T = $e; $($t)*);
124-
};
125-
($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
126-
__lazy_static_internal!(@PUB, $(#[$attr])* static ref $N : $T = $e; $($t)*);
127-
};
128-
(@PUB_IN, $pub_in:path, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
129-
__lazy_static_internal!(@MAKE TY, PUB_IN, $pub_in, $(#[$attr])*, $N);
119+
// optional visibility restrictions are wrapped in `()` to allow for
120+
// explicitly passing otherwise implicit information about private items
121+
($(#[$attr:meta])* ($($vis:tt)*) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
122+
__lazy_static_internal!(@MAKE TY, $(#[$attr])*, ($($vis)*), $N);
130123
__lazy_static_internal!(@TAIL, $N : $T = $e);
131-
__lazy_static_internal!($($t)*);
132-
};
133-
(@$VIS:ident, $(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
134-
__lazy_static_internal!(@MAKE TY, $VIS, $(#[$attr])*, $N);
135-
__lazy_static_internal!(@TAIL, $N : $T = $e);
136-
__lazy_static_internal!($($t)*);
124+
lazy_static!($($t)*);
137125
};
138126
(@TAIL, $N:ident : $T:ty = $e:expr) => {
139127
impl $crate::__Deref for $N {
@@ -159,32 +147,15 @@ macro_rules! __lazy_static_internal {
159147
}
160148
}
161149
};
162-
(@MAKE TY, PUB, $(#[$attr:meta])*, $N:ident) => {
150+
// `vis` is wrapped in `()` to prevent parsing ambiguity
151+
(@MAKE TY, $(#[$attr:meta])*, ($($vis:tt)*), $N:ident) => {
163152
#[allow(missing_copy_implementations)]
164153
#[allow(non_camel_case_types)]
165154
#[allow(dead_code)]
166155
$(#[$attr])*
167-
pub struct $N {__private_field: ()}
156+
$($vis)* struct $N {__private_field: ()}
168157
#[doc(hidden)]
169-
pub static $N: $N = $N {__private_field: ()};
170-
};
171-
(@MAKE TY, PUB_IN, $pub_in:path, $(#[$attr:meta])*, $N:ident) => {
172-
#[allow(missing_copy_implementations)]
173-
#[allow(non_camel_case_types)]
174-
#[allow(dead_code)]
175-
$(#[$attr])*
176-
pub(in $pub_in) struct $N {__private_field: ()}
177-
#[doc(hidden)]
178-
pub(in $pub_in) static $N: $N = $N {__private_field: ()};
179-
};
180-
(@MAKE TY, PRIV, $(#[$attr:meta])*, $N:ident) => {
181-
#[allow(missing_copy_implementations)]
182-
#[allow(non_camel_case_types)]
183-
#[allow(dead_code)]
184-
$(#[$attr])*
185-
struct $N {__private_field: ()}
186-
#[doc(hidden)]
187-
static $N: $N = $N {__private_field: ()};
158+
$($vis)* static $N: $N = $N {__private_field: ()};
188159
};
189160
() => ()
190161
}
@@ -193,13 +164,14 @@ macro_rules! __lazy_static_internal {
193164
#[cfg_attr(feature="nightly", allow_internal_unstable)]
194165
macro_rules! lazy_static {
195166
($(#[$attr:meta])* static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
196-
__lazy_static_internal!(@PRIV, $(#[$attr])* static ref $N : $T = $e; $($t)*);
197-
};
198-
($(#[$attr:meta])* pub (in $pub_in:path) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
199-
__lazy_static_internal!(@PUB_IN, $pub_in, $(#[$attr])* static ref $N : $T = $e; $($t)*);
167+
// use `()` to explicitly forward the information about private items
168+
__lazy_static_internal!($(#[$attr])* () static ref $N : $T = $e; $($t)*);
200169
};
201170
($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
202-
__lazy_static_internal!(@PUB, $(#[$attr])* static ref $N : $T = $e; $($t)*);
171+
__lazy_static_internal!($(#[$attr])* (pub) static ref $N : $T = $e; $($t)*);
172+
};
173+
($(#[$attr:meta])* pub ($($vis:tt)+) static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
174+
__lazy_static_internal!($(#[$attr])* (pub ($($vis)+)) static ref $N : $T = $e; $($t)*);
203175
};
204176
() => ()
205177
}

tests/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,32 @@ mod visibility {
8484
static ref BAR: Box<u32> = Box::new(98);
8585
}
8686

87+
pub mod inner {
88+
lazy_static! {
89+
pub(in visibility) static ref BAZ: Box<u32> = Box::new(42);
90+
pub(crate) static ref BAG: Box<u32> = Box::new(37);
91+
// if uncommented this should cause a compiler error
92+
// error: incorrect visibility restriction
93+
// pub(nonsense) static ref BIG: Box<u32> = Box::new(6);
94+
}
95+
}
96+
8797
#[test]
8898
fn sub_test() {
8999
assert_eq!(**FOO, 0);
90100
assert_eq!(**BAR, 98);
101+
assert_eq!(**inner::BAZ, 42);
102+
assert_eq!(**inner::BAG, 37);
91103
}
92104
}
93105

94106
#[test]
95107
fn test_visibility() {
96108
assert_eq!(*visibility::FOO, Box::new(0));
109+
// if uncommented this should cause a compiler error:
110+
// error[E0603]: static `BAZ` is private
111+
// assert_eq!(*visibility::inner::BAZ, Box::new(42));
112+
assert_eq!(*visibility::inner::BAG, Box::new(37));
97113
}
98114

99115
// This should not cause a warning about a missing Copy implementation

0 commit comments

Comments
 (0)