Skip to content
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

constify a couple thread_local statics #120976

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/sync/worker_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Registry(Arc<RegistryData>);
thread_local! {
/// The registry associated with the thread.
/// This allows the `WorkerLocal` type to clone the registry in its constructor.
static REGISTRY: OnceCell<Registry> = OnceCell::new();
static REGISTRY: OnceCell<Registry> = const { OnceCell::new() };
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the benefit of doing this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

A more practical demonstration: https://godbolt.org/z/K1Yxe8193 (You can see that the first variant does lazy initialization and the second does not).

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha, I missed that this is a thread_local! particular thing.

}

struct ThreadData {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/markdown/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const DEFAULT_COLUMN_WIDTH: usize = 140;

thread_local! {
/// Track the position of viewable characters in our buffer
static CURSOR: Cell<usize> = Cell::new(0);
static CURSOR: Cell<usize> = const { Cell::new(0) };
/// Width of the terminal
static WIDTH: Cell<usize> = Cell::new(DEFAULT_COLUMN_WIDTH);
static WIDTH: Cell<usize> = const { Cell::new(DEFAULT_COLUMN_WIDTH) };
}

/// Print to terminal output to a buffer
Expand Down
2 changes: 1 addition & 1 deletion library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL {

thread_local! {
static BRIDGE_STATE: scoped_cell::ScopedCell<BridgeStateL> =
scoped_cell::ScopedCell::new(BridgeState::NotConnected);
const { scoped_cell::ScopedCell::new(BridgeState::NotConnected) };
}

impl BridgeState<'_> {
Expand Down
2 changes: 1 addition & 1 deletion library/proc_macro/src/bridge/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ thread_local! {
/// This is required as the thread-local state in the proc_macro client does
/// not handle being re-entered, and will invalidate all `Symbol`s when
/// entering a nested macro.
static ALREADY_RUNNING_SAME_THREAD: Cell<bool> = Cell::new(false);
static ALREADY_RUNNING_SAME_THREAD: Cell<bool> = const { Cell::new(false) };
}

/// Keep `ALREADY_RUNNING_SAME_THREAD` (see also its documentation)
Expand Down
Loading