]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Document why as_mut_ptr is safe
[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 // ignore-tidy-undocumented-unsafe
24
25 #![allow(dead_code, missing_docs)]
26 #![unstable(feature = "core_panic",
27             reason = "internal details of the implementation of the `panic!` \
28                       and related macros",
29             issue = "0")]
30
31 use crate::fmt;
32 use crate::panic::{Location, PanicInfo};
33
34 #[cfg(bootstrap)]
35 #[cold]
36 // never inline unless panic_immediate_abort to avoid code
37 // bloat at the call sites as much as possible
38 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
39 #[lang = "panic"]
40 pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
41     if cfg!(feature = "panic_immediate_abort") {
42         unsafe { super::intrinsics::abort() }
43     }
44
45     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
46     // reduce size overhead. The format_args! macro uses str's Display trait to
47     // write expr, which calls Formatter::pad, which must accommodate string
48     // truncation and padding (even though none is used here). Using
49     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
50     // output binary, saving up to a few kilobytes.
51     let (expr, file, line, col) = *expr_file_line_col;
52     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line, col))
53 }
54
55 #[cfg(not(bootstrap))]
56 #[cold]
57 // never inline unless panic_immediate_abort to avoid code
58 // bloat at the call sites as much as possible
59 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
60 #[lang = "panic"]
61 pub fn panic(expr: &str, location: &Location<'_>) -> ! {
62     if cfg!(feature = "panic_immediate_abort") {
63         unsafe { super::intrinsics::abort() }
64     }
65
66     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
67     // reduce size overhead. The format_args! macro uses str's Display trait to
68     // write expr, which calls Formatter::pad, which must accommodate string
69     // truncation and padding (even though none is used here). Using
70     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
71     // output binary, saving up to a few kilobytes.
72     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), location)
73 }
74
75 #[cfg(bootstrap)]
76 #[cold]
77 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
78 #[lang = "panic_bounds_check"]
79 fn panic_bounds_check(file_line_col: &(&'static str, u32, u32),
80                      index: usize, len: usize) -> ! {
81     if cfg!(feature = "panic_immediate_abort") {
82         unsafe { super::intrinsics::abort() }
83     }
84
85     panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
86                            len, index), file_line_col)
87 }
88
89 #[cfg(not(bootstrap))]
90 #[cold]
91 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
92 #[lang = "panic_bounds_check"]
93 fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
94     if cfg!(feature = "panic_immediate_abort") {
95         unsafe { super::intrinsics::abort() }
96     }
97
98     panic_fmt(
99         format_args!("index out of bounds: the len is {} but the index is {}", len, index),
100         location
101     )
102 }
103
104 #[cfg(bootstrap)]
105 #[cold]
106 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
107 #[cfg_attr(    feature="panic_immediate_abort" ,inline)]
108 pub fn panic_fmt(fmt: fmt::Arguments<'_>, file_line_col: &(&'static str, u32, u32)) -> ! {
109     if cfg!(feature = "panic_immediate_abort") {
110         unsafe { super::intrinsics::abort() }
111     }
112
113     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
114     extern "Rust" {
115         #[lang = "panic_impl"]
116         fn panic_impl(pi: &PanicInfo<'_>) -> !;
117     }
118
119     let (file, line, col) = *file_line_col;
120     let location = Location::internal_constructor(file, line, col);
121     let pi = PanicInfo::internal_constructor(Some(&fmt), &location);
122     unsafe { panic_impl(&pi) }
123 }
124
125 #[cfg(not(bootstrap))]
126 #[cold]
127 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
128 #[cfg_attr(    feature="panic_immediate_abort" ,inline)]
129 pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
130     if cfg!(feature = "panic_immediate_abort") {
131         unsafe { super::intrinsics::abort() }
132     }
133
134     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
135     extern "Rust" {
136         #[lang = "panic_impl"]
137         fn panic_impl(pi: &PanicInfo<'_>) -> !;
138     }
139
140     let pi = PanicInfo::internal_constructor(Some(&fmt), location);
141     unsafe { panic_impl(&pi) }
142 }