Skip to content

Update panic_handler to use the attribute template #1918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions src/panic.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,59 @@ r[panic.panic_handler]
## The `panic_handler` attribute

r[panic.panic_handler.intro]
The *`panic_handler` attribute* can be applied to a function to define the behavior of panics.
The *`panic_handler` [attribute][attributes]* can be applied to a function to define the behavior of panics.

r[panic.panic_handler.allowed-positions]
The `panic_handler` attribute can only be applied to a function with signature `fn(&PanicInfo) -> !`.
> [!EXAMPLE]
> Below is shown a `panic_handler` function that logs the panic message and then halts the thread.
> <!-- ignore: test infrastructure can't handle no_std -->
> ```rust,ignore
> #![no_std]
>
> use core::fmt::{self, Write};
> use core::panic::PanicInfo;
>
> struct Sink {
> // ..
> # _0: (),
> }
> #
> # impl Sink {
> # fn new() -> Sink { Sink { _0: () }}
> # }
> #
> # impl fmt::Write for Sink {
> # fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
> # }
>
> #[panic_handler]
> fn panic(info: &PanicInfo) -> ! {
> let mut sink = Sink::new();
>
> // logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
> let _ = writeln!(sink, "{}", info);
>
> loop {}
> }
> ```

> [!NOTE]
> The [`PanicInfo`] struct contains information about the location of the panic.

r[panic.panic_handler.syntax]
The `panic_handler` attribute uses the [MetaWord] syntax and thus does not take any inputs.

r[panic.panic_handler.allowed-positions]
The `panic_handler` attribute may only be applied to a function with signature `fn(&PanicInfo) -> !`.

r[panic.panic_handler.duplicates]
Duplicate instances of the `panic_handler` attribute on a function are ignored.

> [!NOTE]
> `rustc` currently warns about unused duplicate `panic_handler` attributes.

r[panic.panic_handler.unique]
There must be a single `panic_handler` function in the dependency graph.

Below is shown a `panic_handler` function that logs the panic message and then halts the thread.

<!-- ignore: test infrastructure can't handle no_std -->
```rust,ignore
#![no_std]

use core::fmt::{self, Write};
use core::panic::PanicInfo;

struct Sink {
// ..
# _0: (),
}
#
# impl Sink {
# fn new() -> Sink { Sink { _0: () }}
# }
#
# impl fmt::Write for Sink {
# fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
# }

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
let mut sink = Sink::new();

// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
let _ = writeln!(sink, "{}", info);

loop {}
}
```

r[panic.panic_handler.std]
### Standard behavior

Expand Down