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: &(&'static str, 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!` \
41 #[cold] #[inline(never)]
42 pub fn panic_new(expr_file_line_col: &(&'static str, &'static str, u32, u32)) -> ! {
43 let (expr, file, line, _) = *expr_file_line_col;
44 let expr_file_line = (expr, file, line);
45 panic(&expr_file_line)
48 #[cold] #[inline(never)] // this is the slow path, always
50 pub fn panic(expr_file_line: &(&'static str, &'static str, u32)) -> ! {
51 // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
52 // reduce size overhead. The format_args! macro uses str's Display trait to
53 // write expr, which calls Formatter::pad, which must accommodate string
54 // truncation and padding (even though none is used here). Using
55 // Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
56 // output binary, saving up to a few kilobytes.
57 let (expr, file, line) = *expr_file_line;
58 panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), &(file, line))
61 #[cold] #[inline(never)]
62 #[lang = "panic_bounds_check"]
63 fn panic_bounds_check(file_line: &(&'static str, u32),
64 index: usize, len: usize) -> ! {
65 panic_fmt(format_args!("index out of bounds: the len is {} but the index is {}",
66 len, index), file_line)
69 #[cold] #[inline(never)]
70 pub fn panic_fmt_new(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! {
71 let (file, line, _) = *file_line_col;
72 let file_line = (file, line);
73 panic_fmt(fmt, &file_line)
76 #[cold] #[inline(never)]
77 pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
78 #[allow(improper_ctypes)]
82 fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32, col: u32) -> !;
84 let (file, line) = *file_line;
85 unsafe { panic_impl(fmt, file, line, 0) }