diff options
| author | David Tolnay <dtolnay@gmail.com> | 2024-01-20 18:48:41 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2024-01-20 19:00:27 -0800 |
| commit | c43344e839906624a388847906f6a9b27dcd35c4 (patch) | |
| tree | 184f609aab9e21bb72dae455784b31ffb6194574 | |
| parent | f52b88e91f2d185a8be2bf4243827f7b720cd513 (diff) | |
| download | rust-c43344e839906624a388847906f6a9b27dcd35c4.tar.gz rust-c43344e839906624a388847906f6a9b27dcd35c4.zip | |
Add test of thread_local containing multiline const block
Before making thread_local accept statements inside the const block,
this test would fail to compile as follows:
error: no rules expected the token `let`
--> library/std/tests/thread.rs:26:13
|
26 | let value = 1;
| ^^^ no rules expected this token in macro call
|
note: while trying to match meta-variable `$init:expr`
--> library/std/src/thread/local.rs:189:69
|
189 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => (
| ^^^^^^^^^^
| -rw-r--r-- | library/std/tests/thread.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/library/std/tests/thread.rs b/library/std/tests/thread.rs index 754b264c6ad..4ce81f2846e 100644 --- a/library/std/tests/thread.rs +++ b/library/std/tests/thread.rs @@ -1,3 +1,4 @@ +use std::cell::{Cell, RefCell}; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -14,3 +15,24 @@ fn sleep() { thread::sleep(Duration::from_millis(100)); assert_eq!(*finished.lock().unwrap(), false); } + +#[test] +fn thread_local_containing_const_statements() { + // This exercises the `const $init:block` cases of the thread_local macro. + // Despite overlapping with expression syntax, the `const { ... }` is not + // parsed as `$init:expr`. + thread_local! { + static CELL: Cell<u32> = const { + let value = 1; + Cell::new(value) + }; + + static REFCELL: RefCell<u32> = const { + let value = 1; + RefCell::new(value) + }; + } + + assert_eq!(CELL.get(), 1); + assert_eq!(REFCELL.take(), 1); +} |
