]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Snap cfgs to new beta
[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 //! # use std::fmt;
10 //! fn panic_impl(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> !
11 //! # { loop {} }
12 //! ```
13 //!
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.
17 //!
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.
22
23 #![allow(dead_code, missing_docs)]
24 #![unstable(feature = "core_panic",
25             reason = "internal details of the implementation of the `panic!` \
26                       and related macros",
27             issue = "0")]
28
29 use crate::fmt;
30 use crate::panic::{Location, PanicInfo};
31
32 #[cold]
33 // never inline unless panic_immediate_abort to avoid code
34 // bloat at the call sites as much as possible
35 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
36 #[lang = "panic"]
37 pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
38     if cfg!(feature = "panic_immediate_abort") {
39         unsafe { super::intrinsics::abort() }
40     }
41
42     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
43     // reduce size overhead. The format_args! macro uses str's Display trait to
44     // write expr, which calls Formatter::pad, which must accommodate string
45     // truncation and padding (even though none is used here). Using
46     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
47     // output binary, saving up to a few kilobytes.
48     let (expr, file, line, col) = *expr_file_line_col;
49     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line, col))
50 }
51
52 #[cold]
53 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
54 #[lang = "panic_bounds_check"]
55 fn panic_bounds_check(file_line_col: &(&'static str, u32, u32),
56                      index: usize, len: usize) -> ! {
57     if cfg!(feature = "panic_immediate_abort") {
58         unsafe { super::intrinsics::abort() }
59     }
60
61     panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
62                            len, index), file_line_col)
63 }
64
65 #[cold]
66 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
67 #[cfg_attr(    feature="panic_immediate_abort" ,inline)]
68 pub fn panic_fmt(fmt: fmt::Arguments<'_>, file_line_col: &(&'static str, u32, u32)) -> ! {
69     if cfg!(feature = "panic_immediate_abort") {
70         unsafe { super::intrinsics::abort() }
71     }
72
73     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
74     extern "Rust" {
75         #[lang = "panic_impl"]
76         fn panic_impl(pi: &PanicInfo<'_>) -> !;
77     }
78
79     let (file, line, col) = *file_line_col;
80     let pi = PanicInfo::internal_constructor(
81         Some(&fmt),
82         Location::internal_constructor(file, line, col),
83     );
84     unsafe { panic_impl(&pi) }
85 }