]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Rollup merge of #66761 - yuyoyuppe:rust_llvm_minor_fix, r=alexcrichton
[rust.git] / src / libcore / panicking.rs
1 //! Panic support for libcore
2 //!
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:
7 //!
8 //! ```
9 //! fn panic_impl(pi: &core::panic::PanicInfo<'_>) -> !
10 //! # { loop {} }
11 //! ```
12 //!
13 //! 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.
16 //!
17 //! This module contains a few other panicking functions, but these are just the
18 //! 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.
21
22 // ignore-tidy-undocumented-unsafe
23
24 #![allow(dead_code, missing_docs)]
25 #![unstable(feature = "core_panic",
26             reason = "internal details of the implementation of the `panic!` \
27                       and related macros",
28             issue = "0")]
29
30 use crate::fmt;
31 use crate::panic::{Location, PanicInfo};
32
33 #[cold]
34 // never inline unless panic_immediate_abort to avoid code
35 // bloat at the call sites as much as possible
36 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
37 #[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
38 pub fn panic(expr: &str, location: &Location<'_>) -> ! {
39     if cfg!(feature = "panic_immediate_abort") {
40         unsafe { super::intrinsics::abort() }
41     }
42
43     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
44     // reduce size overhead. The format_args! macro uses str's Display trait to
45     // write expr, which calls Formatter::pad, which must accommodate string
46     // truncation and padding (even though none is used here). Using
47     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
48     // output binary, saving up to a few kilobytes.
49     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), location)
50 }
51
52 #[cold]
53 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
54 #[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
55 fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
56     if cfg!(feature = "panic_immediate_abort") {
57         unsafe { super::intrinsics::abort() }
58     }
59
60     panic_fmt(
61         format_args!("index out of bounds: the len is {} but the index is {}", len, index),
62         location
63     )
64 }
65
66 #[cold]
67 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
68 #[cfg_attr(    feature="panic_immediate_abort" ,inline)]
69 pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
70     if cfg!(feature = "panic_immediate_abort") {
71         unsafe { super::intrinsics::abort() }
72     }
73
74     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
75     extern "Rust" {
76         #[lang = "panic_impl"]
77         fn panic_impl(pi: &PanicInfo<'_>) -> !;
78     }
79
80     let pi = PanicInfo::internal_constructor(Some(&fmt), location);
81     unsafe { panic_impl(&pi) }
82 }