Skip to contentSkip to conversation

docs: rust: mention #[track_caller] for functions that panic

PATCH In review FUJITA Tomonori 2 participants 2 messages Nov 18 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 3198be3a6d63..c7404ea7ba13 100644
@@ -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
reviewer
#1 lore raw
Quoted 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
---
 Documentation/rust/coding-guidelines.rst | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/rust/coding-guidelines.rst +4 -0
index 3198be3a6d63..c7404ea7ba13 100644
@@ -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