2014-10-09 15:17:22 -04:00
|
|
|
//! Panic support for libcore
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! The core library cannot define panicking, but it does *declare* panicking. This
|
|
|
|
|
//! means that the functions inside of libcore are allowed to panic, but to be
|
|
|
|
|
//! useful an upstream crate must define panicking for libcore to use. The current
|
|
|
|
|
//! interface for panicking is:
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2017-06-20 15:15:16 +08:00
|
|
|
//! ```
|
2019-11-24 13:15:22 +01:00
|
|
|
//! fn panic_impl(pi: &core::panic::PanicInfo<'_>) -> !
|
2017-06-20 15:15:16 +08:00
|
|
|
//! # { loop {} }
|
2014-06-18 01:04:35 -07:00
|
|
|
//! ```
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! This definition allows for panicking with any general message, but it does not
|
2019-11-25 12:24:39 +01:00
|
|
|
//! allow for failing with a `Box<Any>` value. (`PanicInfo` just contains a `&(dyn Any + Send)`,
|
|
|
|
|
//! for which we fill in a dummy value in `PanicInfo::internal_constructor`.)
|
|
|
|
|
//! The reason for this is that libcore is not allowed to allocate.
|
2014-05-10 13:46:05 -07:00
|
|
|
//!
|
2014-10-09 15:17:22 -04:00
|
|
|
//! This module contains a few other panicking functions, but these are just the
|
|
|
|
|
//! necessary lang items for the compiler. All panics are funneled through this
|
2019-11-25 12:24:39 +01:00
|
|
|
//! one function. The actual symbol is declared through the `#[panic_handler]` attribute.
|
2014-05-01 10:47:18 -07:00
|
|
|
|
2014-10-30 09:13:02 -07:00
|
|
|
#![allow(dead_code, missing_docs)]
|
2019-12-06 20:18:12 -08:00
|
|
|
#![unstable(
|
|
|
|
|
feature = "core_panic",
|
2020-06-15 13:15:47 +00:00
|
|
|
reason = "internal details of the implementation of the `panic!` and related macros",
|
2019-12-21 13:16:18 +02:00
|
|
|
issue = "none"
|
2019-12-06 20:18:12 -08:00
|
|
|
)]
|
2014-05-01 10:47:18 -07:00
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::fmt;
|
|
|
|
|
use crate::panic::{Location, PanicInfo};
|
2018-04-30 10:55:24 +02:00
|
|
|
|
2020-03-10 10:31:03 +01:00
|
|
|
/// The underlying implementation of libcore's `panic!` macro when no formatting is used.
|
2019-10-23 19:30:21 -07:00
|
|
|
#[cold]
|
|
|
|
|
// never inline unless panic_immediate_abort to avoid code
|
|
|
|
|
// bloat at the call sites as much as possible
|
2019-12-06 20:18:12 -08:00
|
|
|
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
2021-10-25 17:07:16 +01:00
|
|
|
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
2019-12-07 08:37:08 -08:00
|
|
|
#[track_caller]
|
2021-11-08 00:55:00 -05:00
|
|
|
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
|
2019-11-24 11:26:07 +01:00
|
|
|
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
|
2021-10-25 17:07:16 +01:00
|
|
|
pub const fn panic(expr: &'static str) -> ! {
|
2022-02-12 23:16:17 +04:00
|
|
|
// Use Arguments::new_v1 instead of format_args!("{expr}") to potentially
|
2019-10-23 19:30:21 -07:00
|
|
|
// reduce size overhead. The format_args! macro uses str's Display trait to
|
|
|
|
|
// write expr, which calls Formatter::pad, which must accommodate string
|
|
|
|
|
// truncation and padding (even though none is used here). Using
|
|
|
|
|
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
|
|
|
|
|
// output binary, saving up to a few kilobytes.
|
2021-09-21 00:56:45 -05:00
|
|
|
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
|
2019-10-23 19:30:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 17:26:11 +02:00
|
|
|
#[inline]
|
|
|
|
|
#[track_caller]
|
2022-01-23 14:57:49 +01:00
|
|
|
#[rustc_diagnostic_item = "panic_str"]
|
2021-12-17 21:52:22 +08:00
|
|
|
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
|
2021-10-25 17:07:16 +01:00
|
|
|
pub const fn panic_str(expr: &str) -> ! {
|
|
|
|
|
panic_display(&expr);
|
2020-09-05 17:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-21 23:04:06 +01:00
|
|
|
#[inline]
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[rustc_diagnostic_item = "unreachable_display"] // needed for `non-fmt-panics` lint
|
|
|
|
|
pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
|
|
|
|
|
panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 19:14:37 +01:00
|
|
|
#[inline]
|
|
|
|
|
#[track_caller]
|
2021-10-19 09:27:59 +02:00
|
|
|
#[lang = "panic_display"] // needed for const-evaluated panics
|
2021-10-25 17:07:16 +01:00
|
|
|
#[rustc_do_not_const_check] // hooked by const-eval
|
2021-12-17 21:52:22 +08:00
|
|
|
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
|
2021-10-25 17:07:16 +01:00
|
|
|
pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
|
2021-09-14 19:14:37 +01:00
|
|
|
panic_fmt(format_args!("{}", *x));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 11:16:23 +01:00
|
|
|
#[cold]
|
|
|
|
|
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
|
|
|
|
|
fn panic_bounds_check(index: usize, len: usize) -> ! {
|
|
|
|
|
if cfg!(feature = "panic_immediate_abort") {
|
2020-06-03 15:15:53 -04:00
|
|
|
super::intrinsics::abort()
|
2020-03-09 11:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-12 23:16:17 +04:00
|
|
|
panic!("index out of bounds: the len is {len} but the index is {index}")
|
2020-03-09 11:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 08:24:20 -05:00
|
|
|
// This function is called directly by the codegen backend, and must not have
|
|
|
|
|
// any extra arguments (including those synthesized by track_caller).
|
2022-01-12 20:45:31 +00:00
|
|
|
#[cold]
|
2022-01-14 23:54:26 +00:00
|
|
|
#[inline(never)]
|
2022-01-12 20:45:31 +00:00
|
|
|
#[lang = "panic_no_unwind"] // needed by codegen for panic in nounwind function
|
|
|
|
|
fn panic_no_unwind() -> ! {
|
|
|
|
|
if cfg!(feature = "panic_immediate_abort") {
|
|
|
|
|
super::intrinsics::abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
|
|
|
|
|
// that gets resolved to the `#[panic_handler]` function.
|
|
|
|
|
extern "Rust" {
|
|
|
|
|
#[lang = "panic_impl"]
|
|
|
|
|
fn panic_impl(pi: &PanicInfo<'_>) -> !;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PanicInfo with the `can_unwind` flag set to false forces an abort.
|
|
|
|
|
let fmt = format_args!("panic in a function that cannot unwind");
|
|
|
|
|
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false);
|
|
|
|
|
|
|
|
|
|
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
|
|
|
|
|
unsafe { panic_impl(&pi) }
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 03:44:02 +01:00
|
|
|
/// The entry point for panicking with a formatted message.
|
|
|
|
|
///
|
|
|
|
|
/// This is designed to reduce the amount of code required at the call
|
|
|
|
|
/// site as much as possible (so that `panic!()` has as low an impact
|
|
|
|
|
/// on (e.g.) the inlining of other functions as possible), by moving
|
|
|
|
|
/// the actual formatting into this shared place.
|
2019-10-23 19:30:21 -07:00
|
|
|
#[cold]
|
2021-09-11 03:44:02 +01:00
|
|
|
// If panic_immediate_abort, inline the abort call,
|
|
|
|
|
// otherwise avoid inlining because of it is cold path.
|
2019-12-06 20:18:12 -08:00
|
|
|
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
|
|
|
|
|
#[cfg_attr(feature = "panic_immediate_abort", inline)]
|
2020-04-22 15:45:35 -04:00
|
|
|
#[track_caller]
|
2021-09-08 13:13:47 -04:00
|
|
|
#[lang = "panic_fmt"] // needed for const-evaluated panics
|
2021-10-25 17:07:16 +01:00
|
|
|
#[rustc_do_not_const_check] // hooked by const-eval
|
2021-12-17 21:52:22 +08:00
|
|
|
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
|
2021-10-25 17:07:16 +01:00
|
|
|
pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
2019-10-23 19:30:21 -07:00
|
|
|
if cfg!(feature = "panic_immediate_abort") {
|
2020-06-03 15:15:53 -04:00
|
|
|
super::intrinsics::abort()
|
2019-10-23 19:30:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
|
2019-11-25 12:24:39 +01:00
|
|
|
// that gets resolved to the `#[panic_handler]` function.
|
2019-10-23 19:30:21 -07:00
|
|
|
extern "Rust" {
|
|
|
|
|
#[lang = "panic_impl"]
|
|
|
|
|
fn panic_impl(pi: &PanicInfo<'_>) -> !;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 00:43:46 +01:00
|
|
|
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), true);
|
2020-03-10 10:31:03 +01:00
|
|
|
|
2020-04-23 23:21:53 +02:00
|
|
|
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
|
2018-04-30 10:55:24 +02:00
|
|
|
unsafe { panic_impl(&pi) }
|
|
|
|
|
}
|
2020-12-16 22:06:06 +01:00
|
|
|
|
2021-07-06 12:38:26 +00:00
|
|
|
/// This function is used instead of panic_fmt in const eval.
|
|
|
|
|
#[lang = "const_panic_fmt"]
|
2021-12-17 21:52:22 +08:00
|
|
|
#[rustc_const_unstable(feature = "core_panic", issue = "none")]
|
2021-07-06 12:38:26 +00:00
|
|
|
pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
|
|
|
|
|
if let Some(msg) = fmt.as_str() {
|
|
|
|
|
panic_str(msg);
|
|
|
|
|
} else {
|
2021-07-10 18:39:25 +02:00
|
|
|
// SAFETY: This is only evaluated at compile time, which reliably
|
|
|
|
|
// handles this UB (in case this branch turns out to be reachable
|
2021-07-09 17:29:41 +02:00
|
|
|
// somehow).
|
|
|
|
|
unsafe { crate::hint::unreachable_unchecked() };
|
2021-07-06 12:38:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 22:06:06 +01:00
|
|
|
#[derive(Debug)]
|
2021-01-16 18:30:22 +01:00
|
|
|
#[doc(hidden)]
|
2020-12-16 22:06:06 +01:00
|
|
|
pub enum AssertKind {
|
|
|
|
|
Eq,
|
|
|
|
|
Ne,
|
2021-03-04 17:55:23 +01:00
|
|
|
Match,
|
2020-12-16 22:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Internal function for `assert_eq!` and `assert_ne!` macros
|
|
|
|
|
#[cold]
|
|
|
|
|
#[track_caller]
|
2021-01-16 18:30:22 +01:00
|
|
|
#[doc(hidden)]
|
2020-12-16 22:06:06 +01:00
|
|
|
pub fn assert_failed<T, U>(
|
|
|
|
|
kind: AssertKind,
|
|
|
|
|
left: &T,
|
|
|
|
|
right: &U,
|
|
|
|
|
args: Option<fmt::Arguments<'_>>,
|
|
|
|
|
) -> !
|
|
|
|
|
where
|
|
|
|
|
T: fmt::Debug + ?Sized,
|
|
|
|
|
U: fmt::Debug + ?Sized,
|
|
|
|
|
{
|
2021-03-04 17:55:23 +01:00
|
|
|
assert_failed_inner(kind, &left, &right, args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Internal function for `assert_match!`
|
|
|
|
|
#[cold]
|
|
|
|
|
#[track_caller]
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
|
|
|
|
|
left: &T,
|
|
|
|
|
right: &str,
|
|
|
|
|
args: Option<fmt::Arguments<'_>>,
|
|
|
|
|
) -> ! {
|
|
|
|
|
// Use the Display implementation to display the pattern.
|
|
|
|
|
struct Pattern<'a>(&'a str);
|
|
|
|
|
impl fmt::Debug for Pattern<'_> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
fmt::Display::fmt(self.0, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Non-generic version of the above functions, to avoid code bloat.
|
|
|
|
|
#[track_caller]
|
|
|
|
|
fn assert_failed_inner(
|
|
|
|
|
kind: AssertKind,
|
|
|
|
|
left: &dyn fmt::Debug,
|
|
|
|
|
right: &dyn fmt::Debug,
|
|
|
|
|
args: Option<fmt::Arguments<'_>>,
|
|
|
|
|
) -> ! {
|
|
|
|
|
let op = match kind {
|
|
|
|
|
AssertKind::Eq => "==",
|
|
|
|
|
AssertKind::Ne => "!=",
|
|
|
|
|
AssertKind::Match => "matches",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match args {
|
|
|
|
|
Some(args) => panic!(
|
|
|
|
|
r#"assertion failed: `(left {} right)`
|
2020-12-16 22:06:06 +01:00
|
|
|
left: `{:?}`,
|
2021-03-13 18:50:43 +08:00
|
|
|
right: `{:?}`: {}"#,
|
2021-03-04 17:55:23 +01:00
|
|
|
op, left, right, args
|
|
|
|
|
),
|
|
|
|
|
None => panic!(
|
|
|
|
|
r#"assertion failed: `(left {} right)`
|
2020-12-16 22:06:06 +01:00
|
|
|
left: `{:?}`,
|
|
|
|
|
right: `{:?}`"#,
|
2021-03-04 17:55:23 +01:00
|
|
|
op, left, right,
|
|
|
|
|
),
|
2020-12-16 22:06:06 +01:00
|
|
|
}
|
|
|
|
|
}
|