]> git.lizzy.rs Git - rust.git/blob - src/libcore/failure.rs
return &mut T from the arenas, not &T
[rust.git] / src / libcore / failure.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 //! Failure support for libcore
12 //!
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:
17 //!
18 //! ```ignore
19 //! fn fail_impl(fmt: &fmt::Arguments, &(&'static str, uint)) -> !;
20 //! ```
21 //!
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.
25 //!
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.
30
31 #![allow(dead_code, missing_doc)]
32
33 use fmt;
34 use intrinsics;
35
36 #[cold] #[inline(never)] // this is the slow path, always
37 #[lang="fail"]
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);
43     }, "{}", expr);
44
45     unsafe { intrinsics::abort() }
46 }
47
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() }
56 }
57
58 #[cold] #[inline(never)]
59 pub fn fail_fmt(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! {
60     #[allow(ctypes)]
61     extern {
62         #[lang = "fail_fmt"]
63         fn fail_impl(fmt: &fmt::Arguments, file: &'static str,
64                         line: uint) -> !;
65
66     }
67     let (file, line) = *file_line;
68     unsafe { fail_impl(fmt, file, line) }
69 }