Skip to content

Document const-initialization for thread_local! macro #109908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// # fn main() {}
/// ```
///
/// This macro also supports wrapping initializing expression in `const` block
/// if it can be evaluated in const context:
///
/// ```
/// use std::cell::Cell;
/// thread_local! {
/// static INIT_FLAG: Cell<bool> = const { Cell::new(false) };
/// }
/// ```
///
/// Doing so does not change the behavior, but might enable more efficient implementation
/// of [`LocalKey`][`std::thread::LocalKey`] on supported platforms.
Comment on lines +150 to +151
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Then I would narrow my concerns to the following suggestion:

Suggested change
/// Doing so does not change the behavior, but might enable more efficient implementation
/// of [`LocalKey`][`std::thread::LocalKey`] on supported platforms.
/// Doing so might enable more efficient implementation
/// of [`LocalKey`][`std::thread::LocalKey`] on supported platforms.

///
/// If initializing expression can not be evaluated in const context, wrapping it in
/// `const` block is a compile error.
///
/// ```compile_fail
/// thread_local! {
/// static FOO: Vec<i32> = const { vec![0] };
/// }
/// ```
///
/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
/// information.
///
Expand Down