Skip to content

Commit 535a09a

Browse files
authored
Rollup merge of rust-lang#66766 - RalfJung:panic-comments, r=SimonSapin
Panic machinery comments and tweaks This is mostly more comments, but I also renamed some things: * `BoxMeUp::box_me_up` is not terribly descriptive, and since this is a "take"-style method (the argument is `&mut self` but the return type is fully owned, even though you can't tell from the type) I chose a name involving "take". * `continue_panic_fmt` was very confusing as it was entirely unclear what was being continued -- for some time I thought "continue" might be the same as "resume" for a panic, but that's something entirely different. So I renamed this to `begin_panic_handler`, matching the `begin_panic*` theme of the other entry points. r? @Dylan-DPC @SimonSapin
2 parents 9933b80 + babe9fc commit 535a09a

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed

src/libcore/panic.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ impl fmt::Display for Location<'_> {
266266
#[unstable(feature = "std_internals", issue = "0")]
267267
#[doc(hidden)]
268268
pub unsafe trait BoxMeUp {
269-
fn box_me_up(&mut self) -> *mut (dyn Any + Send);
269+
/// Take full ownership of the contents.
270+
/// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in libcore.
271+
///
272+
/// After this method got called, only some dummy default value is left in `self`.
273+
/// Calling this method twice, or calling `get` after calling this method, is an error.
274+
///
275+
/// The argument is borrowed because the panic runtime (`__rust_start_panic`) only
276+
/// gets a borrowed `dyn BoxMeUp`.
277+
fn take_box(&mut self) -> *mut (dyn Any + Send);
278+
279+
/// Just borrow the contents.
270280
fn get(&mut self) -> &(dyn Any + Send);
271281
}

src/libcore/panicking.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
//! ```
1212
//!
1313
//! This definition allows for panicking with any general message, but it does not
14-
//! allow for failing with a `Box<Any>` value. The reason for this is that libcore
15-
//! is not allowed to allocate.
14+
//! allow for failing with a `Box<Any>` value. (`PanicInfo` just contains a `&(dyn Any + Send)`,
15+
//! for which we fill in a dummy value in `PanicInfo::internal_constructor`.)
16+
//! The reason for this is that libcore is not allowed to allocate.
1617
//!
1718
//! This module contains a few other panicking functions, but these are just the
1819
//! necessary lang items for the compiler. All panics are funneled through this
19-
//! one function. Currently, the actual symbol is declared in the standard
20-
//! library, but the location of this may change over time.
20+
//! one function. The actual symbol is declared through the `#[panic_handler]` attribute.
2121
2222
// ignore-tidy-undocumented-unsafe
2323

@@ -72,6 +72,7 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
7272
}
7373

7474
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
75+
// that gets resolved to the `#[panic_handler]` function.
7576
extern "Rust" {
7677
#[lang = "panic_impl"]
7778
fn panic_impl(pi: &PanicInfo<'_>) -> !;

src/libpanic_unwind/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
9494
#[unwind(allowed)]
9595
pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
9696
let payload = payload as *mut &mut dyn BoxMeUp;
97-
imp::panic(Box::from_raw((*payload).box_me_up()))
97+
imp::panic(Box::from_raw((*payload).take_box()))
9898
}

src/libstd/panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,5 +425,5 @@ pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
425425
/// ```
426426
#[stable(feature = "resume_unwind", since = "1.9.0")]
427427
pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
428-
panicking::update_count_then_panic(payload)
428+
panicking::rust_panic_without_hook(payload)
429429
}

src/libstd/panicking.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::sys_common::rwlock::RWLock;
2020
use crate::sys_common::{thread_info, util};
2121
use crate::sys_common::backtrace::{self, RustBacktrace};
2222
use crate::thread;
23+
use crate::process;
2324

2425
#[cfg(not(test))]
2526
use crate::io::set_panic;
@@ -46,6 +47,8 @@ extern {
4647
vtable_ptr: *mut usize) -> u32;
4748

4849
/// `payload` is actually a `*mut &mut dyn BoxMeUp` but that would cause FFI warnings.
50+
/// It cannot be `Box<dyn BoxMeUp>` because the other end of this call does not depend
51+
/// on liballoc, and thus cannot use `Box`.
4952
#[unwind(allowed)]
5053
fn __rust_start_panic(payload: usize) -> u32;
5154
}
@@ -296,14 +299,6 @@ pub fn panicking() -> bool {
296299
update_panic_count(0) != 0
297300
}
298301

299-
/// Entry point of panic from the libcore crate (`panic_impl` lang item).
300-
#[cfg(not(test))]
301-
#[panic_handler]
302-
#[unwind(allowed)]
303-
pub fn rust_begin_panic(info: &PanicInfo<'_>) -> ! {
304-
continue_panic_fmt(&info)
305-
}
306-
307302
/// The entry point for panicking with a formatted message.
308303
///
309304
/// This is designed to reduce the amount of code required at the call
@@ -324,13 +319,17 @@ pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>,
324319
unsafe { intrinsics::abort() }
325320
}
326321

322+
// Just package everything into a `PanicInfo` and continue like libcore panics.
327323
let (file, line, col) = *file_line_col;
328324
let location = Location::internal_constructor(file, line, col);
329325
let info = PanicInfo::internal_constructor(Some(msg), &location);
330-
continue_panic_fmt(&info)
326+
begin_panic_handler(&info)
331327
}
332328

333-
fn continue_panic_fmt(info: &PanicInfo<'_>) -> ! {
329+
/// Entry point of panics from the libcore crate (`panic_impl` lang item).
330+
#[cfg_attr(not(test), panic_handler)]
331+
#[unwind(allowed)]
332+
pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
334333
struct PanicPayload<'a> {
335334
inner: &'a fmt::Arguments<'a>,
336335
string: Option<String>,
@@ -345,6 +344,7 @@ fn continue_panic_fmt(info: &PanicInfo<'_>) -> ! {
345344
use crate::fmt::Write;
346345

347346
let inner = self.inner;
347+
// Lazily, the first time this gets called, run the actual string formatting.
348348
self.string.get_or_insert_with(|| {
349349
let mut s = String::new();
350350
drop(s.write_fmt(*inner));
@@ -354,7 +354,7 @@ fn continue_panic_fmt(info: &PanicInfo<'_>) -> ! {
354354
}
355355

356356
unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
357-
fn box_me_up(&mut self) -> *mut (dyn Any + Send) {
357+
fn take_box(&mut self) -> *mut (dyn Any + Send) {
358358
let contents = mem::take(self.fill());
359359
Box::into_raw(Box::new(contents))
360360
}
@@ -378,7 +378,9 @@ fn continue_panic_fmt(info: &PanicInfo<'_>) -> ! {
378378
&file_line_col);
379379
}
380380

381-
/// This is the entry point of panicking for panic!() and assert!().
381+
/// This is the entry point of panicking for the non-format-string variants of
382+
/// panic!() and assert!(). In particular, this is the only entry point that supports
383+
/// arbitrary payloads, not just format strings.
382384
#[unstable(feature = "libstd_sys_internals",
383385
reason = "used by the panic! macro",
384386
issue = "0")]
@@ -412,18 +414,18 @@ pub fn begin_panic<M: Any + Send>(msg: M, file_line_col: &(&'static str, u32, u3
412414
}
413415

414416
unsafe impl<A: Send + 'static> BoxMeUp for PanicPayload<A> {
415-
fn box_me_up(&mut self) -> *mut (dyn Any + Send) {
417+
fn take_box(&mut self) -> *mut (dyn Any + Send) {
416418
let data = match self.inner.take() {
417419
Some(a) => Box::new(a) as Box<dyn Any + Send>,
418-
None => Box::new(()),
420+
None => process::abort(),
419421
};
420422
Box::into_raw(data)
421423
}
422424

423425
fn get(&mut self) -> &(dyn Any + Send) {
424426
match self.inner {
425427
Some(ref a) => a,
426-
None => &(),
428+
None => process::abort(),
427429
}
428430
}
429431
}
@@ -457,9 +459,12 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
457459
let mut info = PanicInfo::internal_constructor(message, &location);
458460
HOOK_LOCK.read();
459461
match HOOK {
460-
// Some platforms know that printing to stderr won't ever actually
462+
// Some platforms (like wasm) know that printing to stderr won't ever actually
461463
// print anything, and if that's the case we can skip the default
462-
// hook.
464+
// hook. Since string formatting happens lazily when calling `payload`
465+
// methods, this means we avoid formatting the string at all!
466+
// (The panic runtime might still call `payload.take_box()` though and trigger
467+
// formatting.)
463468
Hook::Default if panic_output().is_none() => {}
464469
Hook::Default => {
465470
info.set_payload(payload.get());
@@ -486,14 +491,15 @@ fn rust_panic_with_hook(payload: &mut dyn BoxMeUp,
486491
rust_panic(payload)
487492
}
488493

489-
/// Shim around rust_panic. Called by resume_unwind.
490-
pub fn update_count_then_panic(msg: Box<dyn Any + Send>) -> ! {
494+
/// This is the entry point for `resume_unwind`.
495+
/// It just forwards the payload to the panic runtime.
496+
pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
491497
update_panic_count(1);
492498

493499
struct RewrapBox(Box<dyn Any + Send>);
494500

495501
unsafe impl BoxMeUp for RewrapBox {
496-
fn box_me_up(&mut self) -> *mut (dyn Any + Send) {
502+
fn take_box(&mut self) -> *mut (dyn Any + Send) {
497503
Box::into_raw(mem::replace(&mut self.0, Box::new(())))
498504
}
499505

@@ -502,7 +508,7 @@ pub fn update_count_then_panic(msg: Box<dyn Any + Send>) -> ! {
502508
}
503509
}
504510

505-
rust_panic(&mut RewrapBox(msg))
511+
rust_panic(&mut RewrapBox(payload))
506512
}
507513

508514
/// An unmangled function (through `rustc_std_internal_symbol`) on which to slap

0 commit comments

Comments
 (0)