]> git.lizzy.rs Git - rust.git/blob - src/libcore/panicking.rs
Auto merge of #31319 - alexcrichton:msvc-backtraces, r=michaelwoerister
[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 //! ```ignore
19 //! fn panic_impl(fmt: fmt::Arguments, &(&'static str, u32)) -> !;
20 //! ```
21 //!
22 //! This definition allows for panicking with any general message, but it does not
23 //! allow for failing with a `Box<Any>` value. The reason for this is that libcore
24 //! is not allowed to allocate.
25 //!
26 //! This module contains a few other panicking functions, but these are just the
27 //! necessary lang items for the compiler. All panics are funneled through this
28 //! one function. Currently, the actual symbol is declared in the standard
29 //! library, but the location of this may change over time.
30
31 #![allow(dead_code, missing_docs)]
32 #![unstable(feature = "core_panic",
33             reason = "internal details of the implementation of the `panic!` \
34                       and related macros",
35             issue = "0")]
36
37 use fmt;
38
39 #[cold] #[inline(never)] // this is the slow path, always
40 #[lang = "panic"]
41 pub fn panic(expr_file_line: &(&'static str, &'static str, u32)) -> ! {
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) = *expr_file_line;
49     panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line))
50 }
51
52 #[cold] #[inline(never)]
53 #[lang = "panic_bounds_check"]
54 fn panic_bounds_check(file_line: &(&'static str, u32),
55                      index: usize, len: usize) -> ! {
56     panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
57                            len, index), file_line)
58 }
59
60 #[cold] #[inline(never)]
61 pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
62     #[allow(improper_ctypes)]
63     extern {
64         #[lang = "panic_fmt"]
65         #[unwind]
66         fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32) -> !;
67     }
68     let (file, line) = *file_line;
69     unsafe { panic_impl(fmt, file, line) }
70 }