Skip to content

Commit e821cb8

Browse files
committed
Auto merge of #147928 - Zalathar:rollup-2lmpajs, r=Zalathar
Rollup of 5 pull requests Successful merges: - #147125 (move `once` module out of `poison`) - #147800 (Add `Cacheable` trait alias in `rustc_public_bridge`) - #147860 (rustdoc search: relax rules for identifiers) - #147916 (Update books) - #147924 (rustc-dev-guide subtree update) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 63dcdf7 + 866ed3a commit e821cb8

File tree

22 files changed

+218
-158
lines changed

22 files changed

+218
-158
lines changed

compiler/rustc_public_bridge/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![doc(rust_logo)]
2222
#![feature(rustdoc_internals)]
2323
#![feature(sized_hierarchy)]
24+
#![feature(trait_alias)]
2425
// tidy-alphabetical-end
2526

2627
use std::cell::RefCell;
@@ -45,6 +46,9 @@ pub mod context;
4546
#[deprecated(note = "please use `rustc_public::rustc_internal` instead")]
4647
pub mod rustc_internal {}
4748

49+
/// Trait alias for types that can be cached in [`Tables`].
50+
pub trait Cacheable = Copy + Debug + PartialEq + IndexedVal;
51+
4852
/// A container which is used for TLS.
4953
pub struct Container<'tcx, B: Bridge> {
5054
pub tables: RefCell<Tables<'tcx, B>>,
@@ -213,14 +217,14 @@ impl<'tcx, B: Bridge> Tables<'tcx, B> {
213217
/// A trait defining types that are used to emulate rustc_public components, which is really
214218
/// useful when programming in rustc_public-agnostic settings.
215219
pub trait Bridge: Sized {
216-
type DefId: Copy + Debug + PartialEq + IndexedVal;
217-
type AllocId: Copy + Debug + PartialEq + IndexedVal;
218-
type Span: Copy + Debug + PartialEq + IndexedVal;
219-
type Ty: Copy + Debug + PartialEq + IndexedVal;
220-
type InstanceDef: Copy + Debug + PartialEq + IndexedVal;
221-
type TyConstId: Copy + Debug + PartialEq + IndexedVal;
222-
type MirConstId: Copy + Debug + PartialEq + IndexedVal;
223-
type Layout: Copy + Debug + PartialEq + IndexedVal;
220+
type DefId: Cacheable;
221+
type AllocId: Cacheable;
222+
type Span: Cacheable;
223+
type Ty: Cacheable;
224+
type InstanceDef: Cacheable;
225+
type TyConstId: Cacheable;
226+
type MirConstId: Cacheable;
227+
type Layout: Cacheable;
224228

225229
type Error: Error;
226230
type CrateItem: CrateItem<Self>;
@@ -266,17 +270,15 @@ impl<K, V> Default for IndexMap<K, V> {
266270
}
267271
}
268272

269-
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
273+
impl<K: PartialEq + Hash + Eq, V: Cacheable> IndexMap<K, V> {
270274
pub fn create_or_fetch(&mut self, key: K) -> V {
271275
let len = self.index_map.len();
272276
let v = self.index_map.entry(key).or_insert(V::to_val(len));
273277
*v
274278
}
275279
}
276280

277-
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V>
278-
for IndexMap<K, V>
279-
{
281+
impl<K: PartialEq + Hash + Eq, V: Cacheable> Index<V> for IndexMap<K, V> {
280282
type Output = K;
281283

282284
fn index(&self, index: V) -> &Self::Output {

library/std/src/sync/lazy_lock.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::poison::once::ExclusiveState;
1+
use super::once::OnceExclusiveState;
22
use crate::cell::UnsafeCell;
33
use crate::mem::ManuallyDrop;
44
use crate::ops::{Deref, DerefMut};
@@ -140,14 +140,18 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
140140
pub fn into_inner(mut this: Self) -> Result<T, F> {
141141
let state = this.once.state();
142142
match state {
143-
ExclusiveState::Poisoned => panic_poisoned(),
143+
OnceExclusiveState::Poisoned => panic_poisoned(),
144144
state => {
145145
let this = ManuallyDrop::new(this);
146146
let data = unsafe { ptr::read(&this.data) }.into_inner();
147147
match state {
148-
ExclusiveState::Incomplete => Err(ManuallyDrop::into_inner(unsafe { data.f })),
149-
ExclusiveState::Complete => Ok(ManuallyDrop::into_inner(unsafe { data.value })),
150-
ExclusiveState::Poisoned => unreachable!(),
148+
OnceExclusiveState::Incomplete => {
149+
Err(ManuallyDrop::into_inner(unsafe { data.f }))
150+
}
151+
OnceExclusiveState::Complete => {
152+
Ok(ManuallyDrop::into_inner(unsafe { data.value }))
153+
}
154+
OnceExclusiveState::Poisoned => unreachable!(),
151155
}
152156
}
153157
}
@@ -189,7 +193,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
189193
impl<T, F> Drop for PoisonOnPanic<'_, T, F> {
190194
#[inline]
191195
fn drop(&mut self) {
192-
self.0.once.set_state(ExclusiveState::Poisoned);
196+
self.0.once.set_state(OnceExclusiveState::Poisoned);
193197
}
194198
}
195199

@@ -200,19 +204,19 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
200204
let guard = PoisonOnPanic(this);
201205
let data = f();
202206
guard.0.data.get_mut().value = ManuallyDrop::new(data);
203-
guard.0.once.set_state(ExclusiveState::Complete);
207+
guard.0.once.set_state(OnceExclusiveState::Complete);
204208
core::mem::forget(guard);
205209
// SAFETY: We put the value there above.
206210
unsafe { &mut this.data.get_mut().value }
207211
}
208212

209213
let state = this.once.state();
210214
match state {
211-
ExclusiveState::Poisoned => panic_poisoned(),
215+
OnceExclusiveState::Poisoned => panic_poisoned(),
212216
// SAFETY: The `Once` states we completed the initialization.
213-
ExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
217+
OnceExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
214218
// SAFETY: The state is `Incomplete`.
215-
ExclusiveState::Incomplete => unsafe { really_init_mut(this) },
219+
OnceExclusiveState::Incomplete => unsafe { really_init_mut(this) },
216220
}
217221
}
218222

@@ -293,7 +297,7 @@ impl<T, F> LazyLock<T, F> {
293297
match state {
294298
// SAFETY:
295299
// The closure has been run successfully, so `value` has been initialized.
296-
ExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
300+
OnceExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
297301
_ => None,
298302
}
299303
}
@@ -332,11 +336,13 @@ impl<T, F> LazyLock<T, F> {
332336
impl<T, F> Drop for LazyLock<T, F> {
333337
fn drop(&mut self) {
334338
match self.once.state() {
335-
ExclusiveState::Incomplete => unsafe { ManuallyDrop::drop(&mut self.data.get_mut().f) },
336-
ExclusiveState::Complete => unsafe {
339+
OnceExclusiveState::Incomplete => unsafe {
340+
ManuallyDrop::drop(&mut self.data.get_mut().f)
341+
},
342+
OnceExclusiveState::Complete => unsafe {
337343
ManuallyDrop::drop(&mut self.data.get_mut().value)
338344
},
339-
ExclusiveState::Poisoned => {}
345+
OnceExclusiveState::Poisoned => {}
340346
}
341347
}
342348
}

library/std/src/sync/mod.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
//! most one thread at a time is able to access some data.
143143
//!
144144
//! - [`Once`]: Used for a thread-safe, one-time global initialization routine.
145-
//! Mostly useful for implementing other types like `OnceLock`.
145+
//! Mostly useful for implementing other types like [`OnceLock`].
146146
//!
147147
//! - [`OnceLock`]: Used for thread-safe, one-time initialization of a
148148
//! variable, with potentially different initializers based on the caller.
@@ -181,7 +181,24 @@ pub use alloc_crate::sync::UniqueArc;
181181
#[stable(feature = "rust1", since = "1.0.0")]
182182
pub use alloc_crate::sync::{Arc, Weak};
183183

184-
// FIXME(sync_nonpoison,sync_poison_mod): remove all `#[doc(inline)]` once the modules are stabilized.
184+
#[unstable(feature = "mpmc_channel", issue = "126840")]
185+
pub mod mpmc;
186+
pub mod mpsc;
187+
188+
pub(crate) mod once; // `pub(crate)` for the `sys::sync::once` implementations and `LazyLock`.
189+
190+
#[stable(feature = "rust1", since = "1.0.0")]
191+
pub use self::once::{Once, OnceState};
192+
193+
#[stable(feature = "rust1", since = "1.0.0")]
194+
#[doc(inline)]
195+
#[expect(deprecated)]
196+
pub use self::once::ONCE_INIT;
197+
198+
mod barrier;
199+
mod lazy_lock;
200+
mod once_lock;
201+
mod reentrant_lock;
185202

186203
// These exist only in one flavor: no poisoning.
187204
#[stable(feature = "rust1", since = "1.0.0")]
@@ -193,48 +210,37 @@ pub use self::once_lock::OnceLock;
193210
#[unstable(feature = "reentrant_lock", issue = "121440")]
194211
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
195212

196-
// These make sense and exist only with poisoning.
213+
// Note: in the future we will change the default version in `std::sync` to the non-poisoning
214+
// version over an edition.
215+
// See https://github.com/rust-lang/rust/issues/134645#issuecomment-3324577500 for more details.
216+
217+
#[unstable(feature = "sync_nonpoison", issue = "134645")]
218+
pub mod nonpoison;
219+
#[unstable(feature = "sync_poison_mod", issue = "134646")]
220+
pub mod poison;
221+
222+
// FIXME(sync_poison_mod): remove all `#[doc(inline)]` once the modules are stabilized.
223+
224+
// These exist only with poisoning.
197225
#[stable(feature = "rust1", since = "1.0.0")]
198226
#[doc(inline)]
199227
pub use self::poison::{LockResult, PoisonError};
200228

201-
// These (should) exist in both flavors: with and without poisoning.
202-
// FIXME(sync_nonpoison): implement nonpoison versions:
203-
// * Mutex (nonpoison_mutex)
204-
// * Condvar (nonpoison_condvar)
205-
// * Once (nonpoison_once)
206-
// * RwLock (nonpoison_rwlock)
229+
// These exist in both flavors: with and without poisoning.
207230
// The historical default is the version with poisoning.
208231
#[stable(feature = "rust1", since = "1.0.0")]
209232
#[doc(inline)]
210233
pub use self::poison::{
211-
Mutex, MutexGuard, TryLockError, TryLockResult,
212-
Condvar,
213-
Once, OnceState,
234+
TryLockError, TryLockResult,
235+
Mutex, MutexGuard,
214236
RwLock, RwLockReadGuard, RwLockWriteGuard,
237+
Condvar,
215238
};
216-
#[stable(feature = "rust1", since = "1.0.0")]
217-
#[doc(inline)]
218-
#[expect(deprecated)]
219-
pub use self::poison::ONCE_INIT;
239+
220240
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
221241
#[doc(inline)]
222242
pub use self::poison::{MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard};
223243

224-
#[unstable(feature = "mpmc_channel", issue = "126840")]
225-
pub mod mpmc;
226-
pub mod mpsc;
227-
228-
#[unstable(feature = "sync_nonpoison", issue = "134645")]
229-
pub mod nonpoison;
230-
#[unstable(feature = "sync_poison_mod", issue = "134646")]
231-
pub mod poison;
232-
233-
mod barrier;
234-
mod lazy_lock;
235-
mod once_lock;
236-
mod reentrant_lock;
237-
238244
/// A type indicating whether a timed wait on a condition variable returned
239245
/// due to a time out or not.
240246
///

library/std/src/sync/poison/once.rs renamed to library/std/src/sync/once.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pub struct OnceState {
4949
pub(crate) inner: sys::OnceState,
5050
}
5151

52-
pub(crate) enum ExclusiveState {
52+
/// Used for the internal implementation of `sys::sync::once` on different platforms and the
53+
/// [`LazyLock`](crate::sync::LazyLock) implementation.
54+
pub(crate) enum OnceExclusiveState {
5355
Incomplete,
5456
Poisoned,
5557
Complete,
@@ -310,7 +312,7 @@ impl Once {
310312
/// be running, so the state must be either "incomplete", "poisoned" or
311313
/// "complete".
312314
#[inline]
313-
pub(crate) fn state(&mut self) -> ExclusiveState {
315+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
314316
self.inner.state()
315317
}
316318

@@ -320,7 +322,7 @@ impl Once {
320322
/// be running, so the state must be either "incomplete", "poisoned" or
321323
/// "complete".
322324
#[inline]
323-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
325+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
324326
self.inner.set_state(new_state);
325327
}
326328
}

library/std/src/sync/poison.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
//! the panics are recognized reliably or on a best-effort basis depend on the
1414
//! primitive. See [Overview](#overview) below.
1515
//!
16-
//! For the alternative implementations that do not employ poisoning,
17-
//! see [`std::sync::nonpoison`].
16+
//! The synchronization objects in this module have alternative implementations that do not employ
17+
//! poisoning in the [`std::sync::nonpoison`] module.
1818
//!
1919
//! [`std::sync::nonpoison`]: crate::sync::nonpoison
2020
//!
@@ -42,14 +42,6 @@
4242
//! [`Mutex::lock()`] returns a [`LockResult`], providing a way to deal with
4343
//! the poisoned state. See [`Mutex`'s documentation](Mutex#poisoning) for more.
4444
//!
45-
//! - [`Once`]: A thread-safe way to run a piece of code only once.
46-
//! Mostly useful for implementing one-time global initialization.
47-
//!
48-
//! [`Once`] is reliably poisoned if the piece of code passed to
49-
//! [`Once::call_once()`] or [`Once::call_once_force()`] panics.
50-
//! When in poisoned state, subsequent calls to [`Once::call_once()`] will panic too.
51-
//! [`Once::call_once_force()`] can be used to clear the poisoned state.
52-
//!
5345
//! - [`RwLock`]: Provides a mutual exclusion mechanism which allows
5446
//! multiple readers at the same time, while allowing only one
5547
//! writer at a time. In some cases, this can be more efficient than
@@ -59,6 +51,11 @@
5951
//! Note, however, that an `RwLock` may only be poisoned if a panic occurs
6052
//! while it is locked exclusively (write mode). If a panic occurs in any reader,
6153
//! then the lock will not be poisoned.
54+
//!
55+
//! Note that the [`Once`] type also employs poisoning, but since it has non-poisoning `force`
56+
//! methods available on it, there is no separate `nonpoison` and `poison` version.
57+
//!
58+
//! [`Once`]: crate::sync::Once
6259
6360
// If we are not unwinding, `PoisonError` is uninhabited.
6461
#![cfg_attr(not(panic = "unwind"), expect(unreachable_code))]
@@ -69,11 +66,6 @@ pub use self::condvar::Condvar;
6966
pub use self::mutex::MappedMutexGuard;
7067
#[stable(feature = "rust1", since = "1.0.0")]
7168
pub use self::mutex::{Mutex, MutexGuard};
72-
#[stable(feature = "rust1", since = "1.0.0")]
73-
#[expect(deprecated)]
74-
pub use self::once::ONCE_INIT;
75-
#[stable(feature = "rust1", since = "1.0.0")]
76-
pub use self::once::{Once, OnceState};
7769
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
7870
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
7971
#[stable(feature = "rust1", since = "1.0.0")]
@@ -88,7 +80,6 @@ use crate::thread;
8880
mod condvar;
8981
#[stable(feature = "rust1", since = "1.0.0")]
9082
mod mutex;
91-
pub(crate) mod once;
9283
mod rwlock;
9384

9485
pub(crate) struct Flag {

library/std/src/sys/sync/once/futex.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cell::Cell;
22
use crate::sync as public;
33
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
4-
use crate::sync::poison::once::ExclusiveState;
4+
use crate::sync::once::OnceExclusiveState;
55
use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all};
66

77
// On some platforms, the OS is very nice and handles the waiter queue for us.
@@ -83,21 +83,21 @@ impl Once {
8383
}
8484

8585
#[inline]
86-
pub(crate) fn state(&mut self) -> ExclusiveState {
86+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
8787
match *self.state_and_queued.get_mut() {
88-
INCOMPLETE => ExclusiveState::Incomplete,
89-
POISONED => ExclusiveState::Poisoned,
90-
COMPLETE => ExclusiveState::Complete,
88+
INCOMPLETE => OnceExclusiveState::Incomplete,
89+
POISONED => OnceExclusiveState::Poisoned,
90+
COMPLETE => OnceExclusiveState::Complete,
9191
_ => unreachable!("invalid Once state"),
9292
}
9393
}
9494

9595
#[inline]
96-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
96+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
9797
*self.state_and_queued.get_mut() = match new_state {
98-
ExclusiveState::Incomplete => INCOMPLETE,
99-
ExclusiveState::Poisoned => POISONED,
100-
ExclusiveState::Complete => COMPLETE,
98+
OnceExclusiveState::Incomplete => INCOMPLETE,
99+
OnceExclusiveState::Poisoned => POISONED,
100+
OnceExclusiveState::Complete => COMPLETE,
101101
};
102102
}
103103

0 commit comments

Comments
 (0)