From ddb2466f6a1bb66f22824334022a4cee61c73bdc Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 14 Nov 2014 09:18:10 -0800 Subject: librustc: Always parse `macro!()`/`macro![]` as expressions if not followed by a semicolon. This allows code like `vec![1i, 2, 3].len();` to work. This breaks code that uses macros as statements without putting semicolons after them, such as: fn main() { ... assert!(a == b) assert!(c == d) println(...); } It also breaks code that uses macros as items without semicolons: local_data_key!(foo) fn main() { println("hello world") } Add semicolons to fix this code. Those two examples can be fixed as follows: fn main() { ... assert!(a == b); assert!(c == d); println(...); } local_data_key!(foo); fn main() { println("hello world") } RFC #378. Closes #18635. [breaking-change] --- src/libstd/thread_local/mod.rs | 32 ++++++++++++++++---------------- src/libstd/thread_local/scoped.rs | 18 +++++++++--------- 2 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src/libstd/thread_local') diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs index 76fb703514b..1268ab8e0cf 100644 --- a/src/libstd/thread_local/mod.rs +++ b/src/libstd/thread_local/mod.rs @@ -109,7 +109,7 @@ pub struct Key { /// Declare a new thread local storage key of type `std::thread_local::Key`. #[macro_export] #[doc(hidden)] -macro_rules! thread_local( +macro_rules! thread_local { (static $name:ident: $t:ty = $init:expr) => ( static $name: ::std::thread_local::Key<$t> = { use std::cell::UnsafeCell as __UnsafeCell; @@ -119,7 +119,7 @@ macro_rules! thread_local( __thread_local_inner!(static __KEY: __UnsafeCell<__Option<$t>> = { __UnsafeCell { value: __None } - }) + }); fn __init() -> $t { $init } fn __getit() -> &'static __KeyInner<__UnsafeCell<__Option<$t>>> { &__KEY @@ -136,7 +136,7 @@ macro_rules! thread_local( __thread_local_inner!(static __KEY: __UnsafeCell<__Option<$t>> = { __UnsafeCell { value: __None } - }) + }); fn __init() -> $t { $init } fn __getit() -> &'static __KeyInner<__UnsafeCell<__Option<$t>>> { &__KEY @@ -144,7 +144,7 @@ macro_rules! thread_local( ::std::thread_local::Key { inner: __getit, init: __init } }; ); -) +} // Macro pain #4586: // @@ -167,7 +167,7 @@ macro_rules! thread_local( // itself. Woohoo. #[macro_export] -macro_rules! __thread_local_inner( +macro_rules! __thread_local_inner { (static $name:ident: $t:ty = $init:expr) => ( #[cfg_attr(any(target_os = "macos", target_os = "linux"), thread_local)] static $name: ::std::thread_local::KeyInner<$t> = @@ -204,7 +204,7 @@ macro_rules! __thread_local_inner( INIT }); -) +} impl Key { /// Acquire a reference to the value in this TLS key. @@ -459,7 +459,7 @@ mod tests { #[test] fn smoke_no_dtor() { - thread_local!(static FOO: UnsafeCell = UnsafeCell { value: 1 }) + thread_local!(static FOO: UnsafeCell = UnsafeCell { value: 1 }); FOO.with(|f| unsafe { assert_eq!(*f.get(), 1); @@ -483,7 +483,7 @@ mod tests { fn smoke_dtor() { thread_local!(static FOO: UnsafeCell> = UnsafeCell { value: None - }) + }); let (tx, rx) = channel(); spawn(move|| unsafe { @@ -501,10 +501,10 @@ mod tests { struct S2; thread_local!(static K1: UnsafeCell> = UnsafeCell { value: None - }) + }); thread_local!(static K2: UnsafeCell> = UnsafeCell { value: None - }) + }); static mut HITS: uint = 0; impl Drop for S1 { @@ -544,7 +544,7 @@ mod tests { struct S1; thread_local!(static K1: UnsafeCell> = UnsafeCell { value: None - }) + }); impl Drop for S1 { fn drop(&mut self) { @@ -562,10 +562,10 @@ mod tests { struct S1(Sender<()>); thread_local!(static K1: UnsafeCell> = UnsafeCell { value: None - }) + }); thread_local!(static K2: UnsafeCell> = UnsafeCell { value: None - }) + }); impl Drop for S1 { fn drop(&mut self) { @@ -597,7 +597,7 @@ mod dynamic_tests { #[test] fn smoke() { fn square(i: int) -> int { i * i } - thread_local!(static FOO: int = square(3)) + thread_local!(static FOO: int = square(3)); FOO.with(|f| { assert_eq!(*f, 9); @@ -611,7 +611,7 @@ mod dynamic_tests { m.insert(1, 2); RefCell::new(m) } - thread_local!(static FOO: RefCell> = map()) + thread_local!(static FOO: RefCell> = map()); FOO.with(|map| { assert_eq!(map.borrow()[1], 2); @@ -620,7 +620,7 @@ mod dynamic_tests { #[test] fn refcell_vec() { - thread_local!(static FOO: RefCell> = RefCell::new(vec![1, 2, 3])) + thread_local!(static FOO: RefCell> = RefCell::new(vec![1, 2, 3])); FOO.with(|vec| { assert_eq!(vec.borrow().len(), 3); diff --git a/src/libstd/thread_local/scoped.rs b/src/libstd/thread_local/scoped.rs index ee742ab8375..7762d225b9a 100644 --- a/src/libstd/thread_local/scoped.rs +++ b/src/libstd/thread_local/scoped.rs @@ -24,7 +24,7 @@ //! # Example //! //! ``` -//! scoped_thread_local!(static FOO: uint) +//! scoped_thread_local!(static FOO: uint); //! //! // Initially each scoped slot is empty. //! assert!(!FOO.is_set()); @@ -60,18 +60,18 @@ pub struct Key { #[doc(hidden)] pub inner: KeyInner } /// This macro declares a `static` item on which methods are used to get and /// set the value stored within. #[macro_export] -macro_rules! scoped_thread_local( +macro_rules! scoped_thread_local { (static $name:ident: $t:ty) => ( __scoped_thread_local_inner!(static $name: $t) ); (pub static $name:ident: $t:ty) => ( __scoped_thread_local_inner!(pub static $name: $t) ); -) +} #[macro_export] #[doc(hidden)] -macro_rules! __scoped_thread_local_inner( +macro_rules! __scoped_thread_local_inner { (static $name:ident: $t:ty) => ( #[cfg_attr(not(any(windows, target_os = "android", target_os = "ios")), thread_local)] @@ -104,7 +104,7 @@ macro_rules! __scoped_thread_local_inner( INIT }) -) +} impl Key { /// Insert a value into this scoped thread local storage slot for a @@ -119,7 +119,7 @@ impl Key { /// # Example /// /// ``` - /// scoped_thread_local!(static FOO: uint) + /// scoped_thread_local!(static FOO: uint); /// /// FOO.set(&100, || { /// let val = FOO.with(|v| *v); @@ -171,7 +171,7 @@ impl Key { /// # Example /// /// ```no_run - /// scoped_thread_local!(static FOO: uint) + /// scoped_thread_local!(static FOO: uint); /// /// FOO.with(|slot| { /// // work with `slot` @@ -239,7 +239,7 @@ mod tests { #[test] fn smoke() { - scoped_thread_local!(static BAR: uint) + scoped_thread_local!(static BAR: uint); assert!(!BAR.is_set()); BAR.set(&1, || { @@ -253,7 +253,7 @@ mod tests { #[test] fn cell_allowed() { - scoped_thread_local!(static BAR: Cell) + scoped_thread_local!(static BAR: Cell); BAR.set(&Cell::new(1), || { BAR.with(|slot| { -- cgit 1.4.1-3-g733a5