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.
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.
11 //! Panic support for libcore
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:
20 //! fn panic_impl(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> !
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.
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.
33 #![allow(dead_code, missing_docs)]
34 #![unstable(feature = "core_panic",
35 reason = "internal details of the implementation of the `panic!` \
40 use panic::{Location, PanicInfo};
42 #[cold] #[inline(never)] // this is the slow path, always
44 pub fn panic(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
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))
55 #[cold] #[inline(never)]
56 #[lang = "panic_bounds_check"]
57 fn panic_bounds_check(file_line_col: &(&'static str, u32, u32),
58 index: usize, len: usize) -> ! {
59 panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
60 len, index), file_line_col)
63 #[cold] #[inline(never)]
64 pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! {
65 // NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
66 #[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe
68 #[lang = "panic_impl"]
69 fn panic_impl(pi: &PanicInfo) -> !;
72 let (file, line, col) = *file_line_col;
73 let pi = PanicInfo::internal_constructor(
75 Location::internal_constructor(file, line, col),
77 unsafe { panic_impl(&pi) }