Skip to content

Commit bb93450

Browse files
committed
Auto merge of rust-lang#102915 - JohnTitor:rollup-5ht99y1, r=JohnTitor
Rollup of 7 pull requests Successful merges: - rust-lang#102258 (Remove unused variable in float formatting.) - rust-lang#102277 (Consistently write `RwLock`) - rust-lang#102412 (Never panic in `thread::park` and `thread::park_timeout`) - rust-lang#102589 (scoped threads: pass closure through MaybeUninit to avoid invalid dangling references) - rust-lang#102625 (fix backtrace small typo) - rust-lang#102859 (Move lifetime resolution module to rustc_hir_analysis.) - rust-lang#102898 (rustdoc: remove unneeded `<div>` wrapper from sidebar DOM) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1e926f0 + 3011538 commit bb93450

File tree

16 files changed

+128
-109
lines changed

16 files changed

+128
-109
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::iter;
4646

4747
mod generics_of;
4848
mod item_bounds;
49+
mod lifetimes;
4950
mod predicates_of;
5051
mod type_of;
5152

@@ -57,6 +58,7 @@ fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
5758
}
5859

5960
pub fn provide(providers: &mut Providers) {
61+
lifetimes::provide(providers);
6062
*providers = Providers {
6163
opt_const_param_of: type_of::opt_const_param_of,
6264
type_of: type_of::type_of,

compiler/rustc_resolve/src/late/lifetimes.rs renamed to compiler/rustc_hir_analysis/src/collect/lifetimes.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ trait RegionExt {
3232
fn id(&self) -> Option<DefId>;
3333

3434
fn shifted(self, amount: u32) -> Region;
35-
36-
fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region;
3735
}
3836

3937
impl RegionExt for Region {
@@ -69,15 +67,6 @@ impl RegionExt for Region {
6967
_ => self,
7068
}
7169
}
72-
73-
fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region {
74-
match self {
75-
Region::LateBound(debruijn, index, id) => {
76-
Region::LateBound(debruijn.shifted_out_to_binder(binder), index, id)
77-
}
78-
_ => self,
79-
}
80-
}
8170
}
8271

8372
/// Maps the id of each lifetime reference to the lifetime decl
@@ -101,8 +90,8 @@ struct NamedRegionMap {
10190
late_bound_vars: HirIdMap<Vec<ty::BoundVariableKind>>,
10291
}
10392

104-
pub(crate) struct LifetimeContext<'a, 'tcx> {
105-
pub(crate) tcx: TyCtxt<'tcx>,
93+
struct LifetimeContext<'a, 'tcx> {
94+
tcx: TyCtxt<'tcx>,
10695
map: &'a mut NamedRegionMap,
10796
scope: ScopeRef<'a>,
10897

@@ -234,7 +223,7 @@ type ScopeRef<'a> = &'a Scope<'a>;
234223

235224
const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
236225

237-
pub fn provide(providers: &mut ty::query::Providers) {
226+
pub(crate) fn provide(providers: &mut ty::query::Providers) {
238227
*providers = ty::query::Providers {
239228
resolve_lifetimes_trait_definition,
240229
resolve_lifetimes,

compiler/rustc_interface/src/passes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,6 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
739739
ty::provide(providers);
740740
traits::provide(providers);
741741
rustc_passes::provide(providers);
742-
rustc_resolve::provide(providers);
743742
rustc_traits::provide(providers);
744743
rustc_ty_utils::provide(providers);
745744
rustc_metadata::provide(providers);

compiler/rustc_resolve/src/late.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::collections::{hash_map::Entry, BTreeSet};
3434
use std::mem::{replace, take};
3535

3636
mod diagnostics;
37-
pub(crate) mod lifetimes;
3837

3938
type Res = def::Res<NodeId>;
4039

compiler/rustc_resolve/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use rustc_metadata::creader::{CStore, CrateLoader};
4242
use rustc_middle::metadata::ModChild;
4343
use rustc_middle::middle::privacy::AccessLevels;
4444
use rustc_middle::span_bug;
45-
use rustc_middle::ty::query::Providers;
4645
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, ResolverOutputs};
4746
use rustc_query_system::ich::StableHashingContext;
4847
use rustc_session::cstore::{CrateStore, CrateStoreDyn, MetadataLoaderDyn};
@@ -2082,7 +2081,3 @@ impl Finalize {
20822081
Finalize { node_id, path_span, root_span, report_private: true }
20832082
}
20842083
}
2085-
2086-
pub fn provide(providers: &mut Providers) {
2087-
late::lifetimes::provide(providers);
2088-
}

library/core/src/num/flt2dec/strategy/grisu.rs

-4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ pub fn format_shortest_opt<'a>(
253253
let delta1frac = delta1 & ((1 << e) - 1);
254254

255255
// render integral parts, while checking for the accuracy at each step.
256-
let mut kappa = max_kappa as i16;
257256
let mut ten_kappa = max_ten_kappa; // 10^kappa
258257
let mut remainder = plus1int; // digits yet to be rendered
259258
loop {
@@ -290,12 +289,10 @@ pub fn format_shortest_opt<'a>(
290289
// the exact number of digits is `max_kappa + 1` as `plus1 < 10^(max_kappa+1)`.
291290
if i > max_kappa as usize {
292291
debug_assert_eq!(ten_kappa, 1);
293-
debug_assert_eq!(kappa, 0);
294292
break;
295293
}
296294

297295
// restore invariants
298-
kappa -= 1;
299296
ten_kappa /= 10;
300297
remainder = r;
301298
}
@@ -338,7 +335,6 @@ pub fn format_shortest_opt<'a>(
338335
}
339336

340337
// restore invariants
341-
kappa -= 1;
342338
remainder = r;
343339
}
344340

library/std/src/backtrace.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
//! Backtraces are attempted to be as accurate as possible, but no guarantees
1515
//! are provided about the exact accuracy of a backtrace. Instruction pointers,
1616
//! symbol names, filenames, line numbers, etc, may all be incorrect when
17-
//! reported. Accuracy is attempted on a best-effort basis, however, and bugs
18-
//! are always welcome to indicate areas of improvement!
17+
//! reported. Accuracy is attempted on a best-effort basis, however, any bug
18+
//! reports are always welcome to indicate areas of improvement!
1919
//!
2020
//! For most platforms a backtrace with a filename/line number requires that
2121
//! programs be compiled with debug information. Without debug information
@@ -39,7 +39,7 @@
3939
//! default. Its behavior is governed by two environment variables:
4040
//!
4141
//! * `RUST_LIB_BACKTRACE` - if this is set to `0` then `Backtrace::capture`
42-
//! will never capture a backtrace. Any other value this is set to will enable
42+
//! will never capture a backtrace. Any other value set will enable
4343
//! `Backtrace::capture`.
4444
//!
4545
//! * `RUST_BACKTRACE` - if `RUST_LIB_BACKTRACE` is not set, then this variable

library/std/src/sync/rwlock.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl<T> RwLock<T> {
167167
}
168168

169169
impl<T: ?Sized> RwLock<T> {
170-
/// Locks this rwlock with shared read access, blocking the current thread
170+
/// Locks this `RwLock` with shared read access, blocking the current thread
171171
/// until it can be acquired.
172172
///
173173
/// The calling thread will be blocked until there are no more writers which
@@ -181,9 +181,10 @@ impl<T: ?Sized> RwLock<T> {
181181
///
182182
/// # Errors
183183
///
184-
/// This function will return an error if the RwLock is poisoned. An RwLock
185-
/// is poisoned whenever a writer panics while holding an exclusive lock.
186-
/// The failure will occur immediately after the lock has been acquired.
184+
/// This function will return an error if the `RwLock` is poisoned. An
185+
/// `RwLock` is poisoned whenever a writer panics while holding an exclusive
186+
/// lock. The failure will occur immediately after the lock has been
187+
/// acquired.
187188
///
188189
/// # Panics
189190
///
@@ -215,7 +216,7 @@ impl<T: ?Sized> RwLock<T> {
215216
}
216217
}
217218

218-
/// Attempts to acquire this rwlock with shared read access.
219+
/// Attempts to acquire this `RwLock` with shared read access.
219220
///
220221
/// If the access could not be granted at this time, then `Err` is returned.
221222
/// Otherwise, an RAII guard is returned which will release the shared access
@@ -228,13 +229,13 @@ impl<T: ?Sized> RwLock<T> {
228229
///
229230
/// # Errors
230231
///
231-
/// This function will return the [`Poisoned`] error if the RwLock is poisoned.
232-
/// An RwLock is poisoned whenever a writer panics while holding an exclusive
233-
/// lock. `Poisoned` will only be returned if the lock would have otherwise been
234-
/// acquired.
232+
/// This function will return the [`Poisoned`] error if the `RwLock` is
233+
/// poisoned. An `RwLock` is poisoned whenever a writer panics while holding
234+
/// an exclusive lock. `Poisoned` will only be returned if the lock would
235+
/// have otherwise been acquired.
235236
///
236-
/// This function will return the [`WouldBlock`] error if the RwLock could not
237-
/// be acquired because it was already locked exclusively.
237+
/// This function will return the [`WouldBlock`] error if the `RwLock` could
238+
/// not be acquired because it was already locked exclusively.
238239
///
239240
/// [`Poisoned`]: TryLockError::Poisoned
240241
/// [`WouldBlock`]: TryLockError::WouldBlock
@@ -263,20 +264,20 @@ impl<T: ?Sized> RwLock<T> {
263264
}
264265
}
265266

266-
/// Locks this rwlock with exclusive write access, blocking the current
267+
/// Locks this `RwLock` with exclusive write access, blocking the current
267268
/// thread until it can be acquired.
268269
///
269270
/// This function will not return while other writers or other readers
270271
/// currently have access to the lock.
271272
///
272-
/// Returns an RAII guard which will drop the write access of this rwlock
273+
/// Returns an RAII guard which will drop the write access of this `RwLock`
273274
/// when dropped.
274275
///
275276
/// # Errors
276277
///
277-
/// This function will return an error if the RwLock is poisoned. An RwLock
278-
/// is poisoned whenever a writer panics while holding an exclusive lock.
279-
/// An error will be returned when the lock is acquired.
278+
/// This function will return an error if the `RwLock` is poisoned. An
279+
/// `RwLock` is poisoned whenever a writer panics while holding an exclusive
280+
/// lock. An error will be returned when the lock is acquired.
280281
///
281282
/// # Panics
282283
///
@@ -303,7 +304,7 @@ impl<T: ?Sized> RwLock<T> {
303304
}
304305
}
305306

306-
/// Attempts to lock this rwlock with exclusive write access.
307+
/// Attempts to lock this `RwLock` with exclusive write access.
307308
///
308309
/// If the lock could not be acquired at this time, then `Err` is returned.
309310
/// Otherwise, an RAII guard is returned which will release the lock when
@@ -316,13 +317,13 @@ impl<T: ?Sized> RwLock<T> {
316317
///
317318
/// # Errors
318319
///
319-
/// This function will return the [`Poisoned`] error if the RwLock is
320-
/// poisoned. An RwLock is poisoned whenever a writer panics while holding
321-
/// an exclusive lock. `Poisoned` will only be returned if the lock would have
322-
/// otherwise been acquired.
320+
/// This function will return the [`Poisoned`] error if the `RwLock` is
321+
/// poisoned. An `RwLock` is poisoned whenever a writer panics while holding
322+
/// an exclusive lock. `Poisoned` will only be returned if the lock would
323+
/// have otherwise been acquired.
323324
///
324-
/// This function will return the [`WouldBlock`] error if the RwLock could not
325-
/// be acquired because it was already locked exclusively.
325+
/// This function will return the [`WouldBlock`] error if the `RwLock` could
326+
/// not be acquired because it was already locked exclusively.
326327
///
327328
/// [`Poisoned`]: TryLockError::Poisoned
328329
/// [`WouldBlock`]: TryLockError::WouldBlock
@@ -422,10 +423,10 @@ impl<T: ?Sized> RwLock<T> {
422423
///
423424
/// # Errors
424425
///
425-
/// This function will return an error if the RwLock is poisoned. An RwLock
426-
/// is poisoned whenever a writer panics while holding an exclusive lock. An
427-
/// error will only be returned if the lock would have otherwise been
428-
/// acquired.
426+
/// This function will return an error if the `RwLock` is poisoned. An
427+
/// `RwLock` is poisoned whenever a writer panics while holding an exclusive
428+
/// lock. An error will only be returned if the lock would have otherwise
429+
/// been acquired.
429430
///
430431
/// # Examples
431432
///
@@ -455,10 +456,10 @@ impl<T: ?Sized> RwLock<T> {
455456
///
456457
/// # Errors
457458
///
458-
/// This function will return an error if the RwLock is poisoned. An RwLock
459-
/// is poisoned whenever a writer panics while holding an exclusive lock. An
460-
/// error will only be returned if the lock would have otherwise been
461-
/// acquired.
459+
/// This function will return an error if the `RwLock` is poisoned. An
460+
/// `RwLock` is poisoned whenever a writer panics while holding an exclusive
461+
/// lock. An error will only be returned if the lock would have otherwise
462+
/// been acquired.
462463
///
463464
/// # Examples
464465
///

library/std/src/thread/mod.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ use crate::ffi::{CStr, CString};
160160
use crate::fmt;
161161
use crate::io;
162162
use crate::marker::PhantomData;
163-
use crate::mem;
163+
use crate::mem::{self, forget};
164164
use crate::num::NonZeroU64;
165165
use crate::num::NonZeroUsize;
166166
use crate::panic;
@@ -499,13 +499,40 @@ impl Builder {
499499
let output_capture = crate::io::set_output_capture(None);
500500
crate::io::set_output_capture(output_capture.clone());
501501

502+
// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
503+
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
504+
// To prevent leaks we use a wrapper that drops its contents.
505+
#[repr(transparent)]
506+
struct MaybeDangling<T>(mem::MaybeUninit<T>);
507+
impl<T> MaybeDangling<T> {
508+
fn new(x: T) -> Self {
509+
MaybeDangling(mem::MaybeUninit::new(x))
510+
}
511+
fn into_inner(self) -> T {
512+
// SAFETY: we are always initiailized.
513+
let ret = unsafe { self.0.assume_init_read() };
514+
// Make sure we don't drop.
515+
mem::forget(self);
516+
ret
517+
}
518+
}
519+
impl<T> Drop for MaybeDangling<T> {
520+
fn drop(&mut self) {
521+
// SAFETY: we are always initiailized.
522+
unsafe { self.0.assume_init_drop() };
523+
}
524+
}
525+
526+
let f = MaybeDangling::new(f);
502527
let main = move || {
503528
if let Some(name) = their_thread.cname() {
504529
imp::Thread::set_name(name);
505530
}
506531

507532
crate::io::set_output_capture(output_capture);
508533

534+
// SAFETY: we constructed `f` initialized.
535+
let f = f.into_inner();
509536
// SAFETY: the stack guard passed is the one for the current thread.
510537
// This means the current thread's stack and the new thread's stack
511538
// are properly set and protected from each other.
@@ -518,6 +545,12 @@ impl Builder {
518545
// same `JoinInner` as this closure meaning the mutation will be
519546
// safe (not modify it and affect a value far away).
520547
unsafe { *their_packet.result.get() = Some(try_result) };
548+
// Here `their_packet` gets dropped, and if this is the last `Arc` for that packet that
549+
// will call `decrement_num_running_threads` and therefore signal that this thread is
550+
// done.
551+
drop(their_packet);
552+
// Here, the lifetime `'a` and even `'scope` can end. `main` keeps running for a bit
553+
// after that before returning itself.
521554
};
522555

523556
if let Some(scope_data) = &my_packet.scope {
@@ -851,10 +884,22 @@ pub fn sleep(dur: Duration) {
851884
imp::Thread::sleep(dur)
852885
}
853886

887+
/// Used to ensure that `park` and `park_timeout` do not unwind, as that can
888+
/// cause undefined behaviour if not handled correctly (see #102398 for context).
889+
struct PanicGuard;
890+
891+
impl Drop for PanicGuard {
892+
fn drop(&mut self) {
893+
rtabort!("an irrecoverable error occurred while synchronizing threads")
894+
}
895+
}
896+
854897
/// Blocks unless or until the current thread's token is made available.
855898
///
856899
/// A call to `park` does not guarantee that the thread will remain parked
857-
/// forever, and callers should be prepared for this possibility.
900+
/// forever, and callers should be prepared for this possibility. However,
901+
/// it is guaranteed that this function will not panic (it may abort the
902+
/// process if the implementation encounters some rare errors).
858903
///
859904
/// # park and unpark
860905
///
@@ -939,10 +984,13 @@ pub fn sleep(dur: Duration) {
939984
/// [`thread::park_timeout`]: park_timeout
940985
#[stable(feature = "rust1", since = "1.0.0")]
941986
pub fn park() {
987+
let guard = PanicGuard;
942988
// SAFETY: park_timeout is called on the parker owned by this thread.
943989
unsafe {
944990
current().inner.as_ref().parker().park();
945991
}
992+
// No panic occurred, do not abort.
993+
forget(guard);
946994
}
947995

948996
/// Use [`park_timeout`].
@@ -1003,10 +1051,13 @@ pub fn park_timeout_ms(ms: u32) {
10031051
/// ```
10041052
#[stable(feature = "park_timeout", since = "1.4.0")]
10051053
pub fn park_timeout(dur: Duration) {
1054+
let guard = PanicGuard;
10061055
// SAFETY: park_timeout is called on the parker owned by this thread.
10071056
unsafe {
10081057
current().inner.as_ref().parker().park_timeout(dur);
10091058
}
1059+
// No panic occurred, do not abort.
1060+
forget(guard);
10101061
}
10111062

10121063
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)