From 7aeb71d3a9220a8f4709bce550f652976703fe09 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 23 Jun 2025 15:36:23 -0700 Subject: [PATCH 1/3] Move the panic_handler example to the intro and an example block --- src/panic.md | 70 ++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/panic.md b/src/panic.md index 2be7e42fb2..2d167af23d 100644 --- a/src/panic.md +++ b/src/panic.md @@ -22,48 +22,48 @@ r[panic.panic_handler] r[panic.panic_handler.intro] The *`panic_handler` attribute* 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. +> +> ```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.allowed-positions] +The `panic_handler` attribute can only be applied to a function with signature `fn(&PanicInfo) -> !`. + 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. - - -```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 From 731d62a0f468807d5d3965626de986203707c6ef Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 23 Jun 2025 15:44:19 -0700 Subject: [PATCH 2/3] Update panic_handler to use the attribute template --- src/panic.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/panic.md b/src/panic.md index 2d167af23d..49b7c77512 100644 --- a/src/panic.md +++ b/src/panic.md @@ -20,7 +20,7 @@ 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. > [!EXAMPLE] > Below is shown a `panic_handler` function that logs the panic message and then halts the thread. @@ -58,8 +58,17 @@ The *`panic_handler` attribute* can be applied to a function to define the behav > [!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 can only be applied to a function with signature `fn(&PanicInfo) -> !`. +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. From d8e8071526d44b7ebf474473c1422b9dbb41d9db Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 22 Sep 2025 13:31:09 -0700 Subject: [PATCH 3/3] Minor update of `panic_hanlder` More closely align with the template, and some minor word tweaks. --- src/panic.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/panic.md b/src/panic.md index 49b7c77512..3240627bf0 100644 --- a/src/panic.md +++ b/src/panic.md @@ -16,6 +16,7 @@ There are also language features that provide a level of control over panic beha > [!NOTE] > The standard library provides the capability to explicitly panic via the [`panic!` macro][panic!]. + r[panic.panic_handler] ## The `panic_handler` attribute @@ -59,16 +60,16 @@ The *`panic_handler` [attribute][attributes]* can be applied to a function to de > 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. +The `panic_handler` attribute uses the [MetaWord] syntax. 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. +The `panic_handler` attribute may be used any number of times on a function. > [!NOTE] -> `rustc` currently warns about unused duplicate `panic_handler` attributes. +> `rustc` lints against any use following the first. r[panic.panic_handler.unique] There must be a single `panic_handler` function in the dependency graph.