docs: rust: mention #[track_caller] for functions that panic
Subsystems (2)
PATCH 1/1 docs: rust: mention #[track_caller] for functions that panic
From: FUJITA Tomonori <[email protected]>
When a function panics, the panic message shows a line inside the function, not the caller. Adding #[track_caller] makes the message show the caller's location.
Add a sentence to the "# Panics" guideline: a function that may panic should also be marked with #[track_caller].
--- Documentation/rust/coding-guidelines.rst | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/rust/coding-guidelines.rst +4 -0
@@ -241,6 +241,10 @@ in the kernel: - While not shown here, if a function may panic, the conditions under which that happens must be described under a ``# Panics`` section. + Such a function should also be marked with ``#[track_caller]``, so that + the panic message shows the location of the caller instead of a line + inside the function. + Please note that panicking should be very rare and used only with a good reason. In almost all cases, a fallible approach should be used, typically returning a ``Result``.
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.43.0
Gary Guo
reviewerQuoted 8 lines · FUJITA Tomonori
From: FUJITA Tomonori <[email protected]>
When a function panics, the panic message shows a line inside the function, not the caller. Adding #[track_caller] makes the message show the caller's location.
Add a sentence to the "# Panics" guideline: a function that may panic should also be marked with #[track_caller].
I don't think this is a universal rule. #[track_caller] has a code and it shouldn't be used unless you really care about the caller (or have the panicking code being always inlined)
Best,
Gary
Quoted 23 lines
Signed-off-by FUJITA Tomonori <[email protected]>--- Documentation/rust/coding-guidelines.rst | 4 ++++ 1 file changed, 4 insertions(+)Documentation/rust/coding-guidelines.rst +4
-0@@ -241,6 +241,10 @@ in the kernel: - While not shown here, if a function may panic, the conditions under which that happens must be described under a ``# Panics`` section. + Such a function should also be marked with ``#[track_caller]``, so that + the panic message shows the location of the caller instead of a line + inside the function. + Please note that panicking should be very rare and used only with a good reason. In almost all cases, a fallible approach should be used, typically returning a ``Result``.base-commit:
aebb7274476403040d582c61478df8f4a7bf9cd3