diff options
| author | bors <bors@rust-lang.org> | 2025-01-21 02:23:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-01-21 02:23:15 +0000 |
| commit | b605c65b6eb5fa71783f8e26df69975f9f1680ee (patch) | |
| tree | 91f28516d70d3879f1cd32653417e6986223ed4c /library/std/src/thread | |
| parent | a42d5ecf34c1d4ec8f7e35059b647b576cb42d93 (diff) | |
| parent | 8ec7bae57b29f9bac22d6b8cdd810c49cb2a3785 (diff) | |
| download | rust-b605c65b6eb5fa71783f8e26df69975f9f1680ee.tar.gz rust-b605c65b6eb5fa71783f8e26df69975f9f1680ee.zip | |
Auto merge of #135224 - wyfo:tls-panic-outline, r=cuviper
Outline panicking code for `LocalKey::with` See https://github.com/rust-lang/rust/pull/115491 for prior related modifications. https://godbolt.org/z/MTsz87jGj shows a reduction of the code size for TLS accesses.
Diffstat (limited to 'library/std/src/thread')
| -rw-r--r-- | library/std/src/thread/local.rs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index 2313f4b5beb..c003503ca8b 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -230,6 +230,14 @@ impl fmt::Display for AccessError { #[stable(feature = "thread_local_try_with", since = "1.26.0")] impl Error for AccessError {} +// This ensures the panicking code is outlined from `with` for `LocalKey`. +#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] +#[track_caller] +#[cold] +fn panic_access_error(err: AccessError) -> ! { + panic!("cannot access a Thread Local Storage value during or after destruction: {err:?}") +} + impl<T: 'static> LocalKey<T> { #[doc(hidden)] #[unstable( @@ -269,10 +277,10 @@ impl<T: 'static> LocalKey<T> { where F: FnOnce(&T) -> R, { - self.try_with(f).expect( - "cannot access a Thread Local Storage value \ - during or after destruction", - ) + match self.try_with(f) { + Ok(r) => r, + Err(err) => panic_access_error(err), + } } /// Acquires a reference to the value in this TLS key. @@ -327,10 +335,10 @@ impl<T: 'static> LocalKey<T> { let mut init = Some(init); let reference = unsafe { - (self.inner)(Some(&mut init)).as_ref().expect( - "cannot access a Thread Local Storage value \ - during or after destruction", - ) + match (self.inner)(Some(&mut init)).as_ref() { + Some(r) => r, + None => panic_access_error(AccessError), + } }; f(init, reference) |
