Skip to content

Commit e244040

Browse files
authored
Merge pull request #362 from japaric/panic
document #[panic_handler]
2 parents 2f9a1f2 + d17ef56 commit e244040

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113

114114
- [Application Binary Interface](abi.md)
115115

116+
- [The Rust runtime](runtime.md)
117+
116118
[Appendix: Influences](influences.md)
117119

118120
[Appendix: As-yet-undocumented Features](undocumented.md)

src/attributes.md

+2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ which can be used to control type layout.
206206
symbol for this item to its identifier.
207207
- [`used`] - on statics, this forces the compiler to keep the variable in the
208208
output object file.
209+
- [`panic_handler`] — sets the function to handle panics.
209210

210211
### Deprecation
211212

@@ -632,3 +633,4 @@ pub fn f() {}
632633
[Expression Attributes]: expressions.html#expression-attributes
633634
[`meta` macro fragment specifier]: macros-by-example.html
634635
[`used`]: abi.html#the-used-attribute
636+
[`panic_handler`]: runtime.html#the-panic_handler-attribute

src/runtime.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# The Rust runtime
2+
3+
This section documents features that define some aspects of the Rust runtime.
4+
5+
## The `panic_handler` attribute
6+
7+
The *`panic_handler` attribute* can only be applied to a function with signature
8+
`fn(&PanicInfo) -> !`. The function marked with this [attribute] defines the behavior of panics. The
9+
[`PanicInfo`] struct contains information about the location of the panic. There must be a single
10+
`panic_handler` function in the dependency graph of a binary, dylib or cdylib crate.
11+
12+
Below is shown a `panic_handler` function that logs the panic message and then halts the
13+
thread.
14+
15+
<!-- NOTE(ignore) `mdbook test` doesn't support `no_std` code -->
16+
17+
``` rust, ignore
18+
#![no_std]
19+
20+
use core::fmt::{self, Write};
21+
use core::panic::PanicInfo;
22+
23+
struct Sink {
24+
// ..
25+
# _0: (),
26+
}
27+
#
28+
# impl Sink {
29+
# fn new() -> Sink { Sink { _0: () }}
30+
# }
31+
#
32+
# impl fmt::Write for Sink {
33+
# fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
34+
# }
35+
36+
#[panic_handler]
37+
fn panic(info: &PanicInfo) -> ! {
38+
let mut sink = Sink::new();
39+
40+
// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
41+
let _ = writeln!(sink, "{}", info);
42+
43+
loop {}
44+
}
45+
```
46+
47+
### Standard behavior
48+
49+
The standard library provides an implementation of `panic_handler` that
50+
defaults to unwinding the stack but that can be [changed to abort the
51+
process][abort]. The standard library's panic behavior can be modified at
52+
runtime with the [set_hook] function.
53+
54+
[`PanicInfo`]: ../core/panic/struct.PanicInfo.html
55+
[abort]: ../book/ch09-01-unrecoverable-errors-with-panic.html
56+
[attribute]: attributes.html
57+
[set_hook]: ../std/panic/fn.set_hook.html

0 commit comments

Comments
 (0)