1 //! Panic support for libcore
3 //! The core library cannot define panicking, but it does *declare* panicking. This
4 //! means that the functions inside of libcore are allowed to panic, but to be
5 //! useful an upstream crate must define panicking for libcore to use. The current
6 //! interface for panicking is:
10 //! fn panic_impl(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> !
14 //! This definition allows for panicking with any general message, but it does not
15 //! allow for failing with a `Box<Any>` value. The reason for this is that libcore
16 //! is not allowed to allocate.
18 //! This module contains a few other panicking functions, but these are just the
19 //! necessary lang items for the compiler. All panics are funneled through this
20 //! one function. Currently, the actual symbol is declared in the standard
21 //! library, but the location of this may change over time.
23 // ignore-tidy-undocumented-unsafe
25 #![allow(dead_code, missing_docs)]
26 #![unstable(feature = "core_panic",
27 reason = "internal details of the implementation of the `panic!` \
32 use crate::panic::{Location, PanicInfo};
35 // never inline unless panic_immediate_abort to avoid code
36 // bloat at the call sites as much as possible
37 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
39 pub fn panic(expr: &str, location: &Location<'_>) -> ! {
40 if cfg!(feature = "panic_immediate_abort") {
41 unsafe { super::intrinsics::abort() }
44 // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
45 // reduce size overhead. The format_args! macro uses str's Display trait to
46 // write expr, which calls Formatter::pad, which must accommodate string
47 // truncation and padding (even though none is used here). Using
48 // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
49 // output binary, saving up to a few kilobytes.
50 panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), location)
54 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
55 #[lang = "panic_bounds_check"]
56 fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
57 if cfg!(feature = "panic_immediate_abort") {
58 unsafe { super::intrinsics::abort() }
62 format_args!("index out of bounds: the len is {} but the index is {}", len, index),
68 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
69 #[cfg_attr( feature="panic_immediate_abort" ,inline)]
70 pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
71 if cfg!(feature = "panic_immediate_abort") {
72 unsafe { super::intrinsics::abort() }
75 // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
77 #[lang = "panic_impl"]
78 fn panic_impl(pi: &PanicInfo<'_>) -> !;
81 let pi = PanicInfo::internal_constructor(Some(&fmt), location);
82 unsafe { panic_impl(&pi) }