Skip to content
New issue

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

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

Already on GitHub? # to your account

More const {} init in thread_local #137194

Merged
merged 3 commits into from
Feb 23, 2025
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 library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type LocalStream = Arc<Mutex<Vec<u8>>>;

thread_local! {
/// Used by the test crate to capture the output of the print macros and panics.
static OUTPUT_CAPTURE: Cell<Option<LocalStream>> = {
static OUTPUT_CAPTURE: Cell<Option<LocalStream>> = const {
Cell::new(None)
}
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sync/mpmc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,3 +1382,6 @@ impl<T> fmt::Debug for Receiver<T> {
f.pad("Receiver { .. }")
}
}

#[cfg(test)]
mod tests;
14 changes: 14 additions & 0 deletions library/std/src/sync/mpmc/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Ensure that thread_local init with `const { 0 }` still has unique address at run-time
#[test]
fn waker_current_thread_id() {
let first = super::waker::current_thread_id();
let t = crate::thread::spawn(move || {
let second = super::waker::current_thread_id();
assert_ne!(first, second);
assert_eq!(second, super::waker::current_thread_id());
});

assert_eq!(first, super::waker::current_thread_id());
t.join().unwrap();
assert_eq!(first, super::waker::current_thread_id());
}
2 changes: 1 addition & 1 deletion library/std/src/sync/mpmc/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,6 @@ impl Drop for SyncWaker {
pub fn current_thread_id() -> usize {
// `u8` is not drop so this variable will be available during thread destruction,
// whereas `thread::current()` would not be
thread_local! { static DUMMY: u8 = 0 }
thread_local! { static DUMMY: u8 = const { 0 } }
DUMMY.with(|x| (x as *const u8).addr())
}
11 changes: 6 additions & 5 deletions library/std/src/thread/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ use crate::fmt;
/// use std::cell::Cell;
/// use std::thread;
///
/// thread_local!(static FOO: Cell<u32> = Cell::new(1));
/// // explicit `const {}` block enables more efficient initialization
/// thread_local!(static FOO: Cell<u32> = const { Cell::new(1) });
///
/// assert_eq!(FOO.get(), 1);
/// FOO.set(2);
Expand Down Expand Up @@ -138,7 +139,7 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// use std::cell::{Cell, RefCell};
///
/// thread_local! {
/// pub static FOO: Cell<u32> = Cell::new(1);
/// pub static FOO: Cell<u32> = const { Cell::new(1) };
///
/// static BAR: RefCell<Vec<f32>> = RefCell::new(vec![1.0, 2.0]);
/// }
Expand Down Expand Up @@ -394,7 +395,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
/// use std::cell::Cell;
///
/// thread_local! {
/// static X: Cell<i32> = Cell::new(1);
/// static X: Cell<i32> = const { Cell::new(1) };
/// }
///
/// assert_eq!(X.get(), 1);
Expand Down Expand Up @@ -423,7 +424,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
/// use std::cell::Cell;
///
/// thread_local! {
/// static X: Cell<Option<i32>> = Cell::new(Some(1));
/// static X: Cell<Option<i32>> = const { Cell::new(Some(1)) };
/// }
///
/// assert_eq!(X.take(), Some(1));
Expand Down Expand Up @@ -453,7 +454,7 @@ impl<T: 'static> LocalKey<Cell<T>> {
/// use std::cell::Cell;
///
/// thread_local! {
/// static X: Cell<i32> = Cell::new(1);
/// static X: Cell<i32> = const { Cell::new(1) };
/// }
///
/// assert_eq!(X.replace(2), 1);
Expand Down
Loading