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 //! Failure support for libcore
13 //! The core library cannot define failure, but it does *declare* failure. This
14 //! means that the functions inside of libcore are allowed to fail, but to be
15 //! useful an upstream crate must define failure for libcore to use. The current
16 //! interface for failure is:
19 //! fn fail_impl(fmt: &fmt::Arguments, &(&'static str, uint)) -> !;
22 //! This definition allows for failing with any general message, but it does not
23 //! allow for failing with a `~Any` value. The reason for this is that libcore
24 //! is not allowed to allocate.
26 //! This module contains a few other failure functions, but these are just the
27 //! necessary lang items for the compiler. All failure is 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_doc)]
36 #[cold] #[inline(never)] // this is the slow path, always
38 pub fn fail(expr_file_line: &(&'static str, &'static str, uint)) -> ! {
39 let (expr, file, line) = *expr_file_line;
40 let ref file_line = (file, line);
41 format_args!(|args| -> () {
42 fail_fmt(args, file_line);
45 unsafe { intrinsics::abort() }
48 #[cold] #[inline(never)]
49 #[lang="fail_bounds_check"]
50 fn fail_bounds_check(file_line: &(&'static str, uint),
51 index: uint, len: uint) -> ! {
52 format_args!(|args| -> () {
53 fail_fmt(args, file_line);
54 }, "index out of bounds: the len is {} but the index is {}", len, index);
55 unsafe { intrinsics::abort() }
58 #[cold] #[inline(never)]
59 pub fn fail_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
63 fn fail_impl(fmt: &fmt::Arguments, file: &'static str,
67 let (file, line) = *file_line;
68 unsafe { fail_impl(fmt, file, line) }