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:
19 //! fn panic_impl(fmt: fmt::Arguments, &(&'static str, u32)) -> !;
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.
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.
31 #![allow(dead_code, missing_docs)]
32 #![unstable(feature = "core_panic",
33 reason = "internal details of the implementation of the `panic!` \
38 #[cold] #[inline(never)] // this is the slow path, always
40 pub fn panic(expr_file_line: &(&'static str, &'static str, u32)) -> ! {
41 // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
42 // reduce size overhead. The format_args! macro uses str's Display trait to
43 // write expr, which calls Formatter::pad, which must accommodate string
44 // truncation and padding (even though none is used here). Using
45 // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
46 // output binary, saving up to a few kilobytes.
47 let (expr, file, line) = *expr_file_line;
48 panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line))
51 #[cold] #[inline(never)]
52 #[lang = "panic_bounds_check"]
53 fn panic_bounds_check(file_line: &(&'static str, u32),
54 index: usize, len: usize) -> ! {
55 panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
56 len, index), file_line)
59 #[cold] #[inline(never)]
60 pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
61 #[allow(improper_ctypes)]
64 fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32) -> !;
66 let (file, line) = *file_line;
67 unsafe { panic_impl(fmt, file, line) }