]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Rollup merge of #56476 - GuillaumeGomez:invalid-line-number-match, r=QuietMisdreavus
[rust.git] / src / libcore / panicking.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Panic support for libcore
12 //!
13 //! The core library cannot define panicking, but it does *declare* panicking. This
14 //! means that the functions inside of libcore are allowed to panic, but to be
15 //! useful an upstream crate must define panicking for libcore to use. The current
16 //! interface for panicking is:
17 //!
18 //! ```
19 //! # use std::fmt;
20 //! fn panic_impl(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> !
21 //! # { loop {} }
22 //! ```
23 //!
24 //! This definition allows for panicking with any general message, but it does not
25 //! allow for failing with a `Box<Any>` value. The reason for this is that libcore
26 //! is not allowed to allocate.
27 //!
28 //! This module contains a few other panicking functions, but these are just the
29 //! necessary lang items for the compiler. All panics are funneled through this
30 //! one function. Currently, the actual symbol is declared in the standard
31 //! library, but the location of this may change over time.
32
33 #![allow(dead_code, missing_docs)]
34 #![unstable(feature = "core_panic",
35             reason = "internal details of the implementation of the `panic!` \
36                       and related macros",
37             issue = "0")]
38
39 use fmt;
40 use panic::{Location, PanicInfo};
41
42 #[cold]
43 // never inline unless panic_immediate_abort to avoid code
44 // bloat at the call sites as much as possible
45 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
46 #[lang = "panic"]
47 pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
48     if cfg!(feature = "panic_immediate_abort") {
49         unsafe { super::intrinsics::abort() }
50     }
51
52     // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
53     // reduce size overhead. The format_args! macro uses str's Display trait to
54     // write expr, which calls Formatter::pad, which must accommodate string
55     // truncation and padding (even though none is used here). Using
56     // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
57     // output binary, saving up to a few kilobytes.
58     let (expr, file, line, col) = *expr_file_line_col;
59     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line, col))
60 }
61
62 #[cold]
63 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
64 #[lang = "panic_bounds_check"]
65 fn panic_bounds_check(file_line_col: &(&'static str, u32, u32),
66                      index: usize, len: usize) -> ! {
67     if cfg!(feature = "panic_immediate_abort") {
68         unsafe { super::intrinsics::abort() }
69     }
70
71     panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
72                            len, index), file_line_col)
73 }
74
75 #[cold]
76 #[cfg_attr(not(feature="panic_immediate_abort"),inline(never))]
77 #[cfg_attr(    feature="panic_immediate_abort" ,inline)]
78 pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! {
79     if cfg!(feature = "panic_immediate_abort") {
80         unsafe { super::intrinsics::abort() }
81     }
82
83     // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
84     #[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe
85     extern "Rust" {
86         #[lang = "panic_impl"]
87         fn panic_impl(pi: &PanicInfo) -> !;
88     }
89
90     let (file, line, col) = *file_line_col;
91     let pi = PanicInfo::internal_constructor(
92         Some(&fmt),
93         Location::internal_constructor(file, line, col),
94     );
95     unsafe { panic_impl(&pi) }
96 }