From fc75a4e14607a9b6445fd0c6685fb6999fa69de8 Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Tue, 3 Oct 2023 14:44:41 -0400 Subject: [PATCH] Allow any expression blocks in `thread_local!` --- library/std/src/thread/local.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index 338567777f7..4c5e0e91394 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -166,7 +166,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// ``` /// use std::cell::Cell; /// thread_local! { -/// pub static FOO: Cell = const { Cell::new(1) }; +/// pub static FOO: Cell = const { +/// let value = 1; +/// Cell::new(value) +/// }; /// } /// /// assert_eq!(FOO.get(), 1); @@ -186,12 +189,12 @@ macro_rules! thread_local { // empty (base case for the recursion) () => {}; - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( + ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block; $($rest:tt)*) => ( $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); $crate::thread_local!($($rest)*); ); - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => ( + ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block) => ( $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); );