Skip to content

Commit 3cc0b65

Browse files
committed
Add documentation for the cold and track_caller attributes
Document the built-in `cold` and `track_caller` attributes in the standard library using the `#[doc(attribute = "...")]` mechanism, following the existing `must_use` and `inline` attribute documentation.
1 parent 5503df8 commit 3cc0b65

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

library/core/src/attribute_docs.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,64 @@ mod warn_attribute {}
405405
/// [`Result`]: result::Result
406406
/// [the `no_std` attribute]: ../reference/names/preludes.html#the-no_std-attribute
407407
mod no_std_attribute {}
408+
409+
#[doc(attribute = "cold")]
410+
//
411+
/// Hint to the compiler that a function is unlikely to be called.
412+
///
413+
/// Marking a function `#[cold]` tells the compiler that calls to it are rare, so it can
414+
/// optimize for the common case where the function is not called. Like `inline`, it is only a
415+
/// hint and does not change the function's behavior.
416+
///
417+
/// It is typically used on functions that handle uncommon cases, such as error or panic paths:
418+
///
419+
/// ```rust
420+
/// # #![allow(dead_code)]
421+
/// fn check(value: i32) {
422+
/// if value < 0 {
423+
/// report_error("value must be non-negative");
424+
/// }
425+
/// // ... the common case continues here ...
426+
/// }
427+
///
428+
/// #[cold]
429+
/// fn report_error(message: &str) {
430+
/// eprintln!("error: {message}");
431+
/// }
432+
/// ```
433+
///
434+
/// For more information, see the Reference on [the `cold` attribute].
435+
///
436+
/// [the `cold` attribute]: ../reference/attributes/codegen.html#the-cold-attribute
437+
mod cold_attribute {}
438+
439+
#[doc(attribute = "track_caller")]
440+
//
441+
/// Make a function report the location of its caller instead of its own.
442+
///
443+
/// When a function panics, the panic message normally points at the line inside that function
444+
/// where the panic happened. `#[track_caller]` changes that: it lets the function see the
445+
/// [`Location`] it was called from, so the panic (and any direct use of [`Location::caller`])
446+
/// points at the call site instead. The standard library uses this on methods like
447+
/// [`Option::unwrap`], so a failed `unwrap` blames the line that called it rather than a line
448+
/// inside the standard library.
449+
///
450+
/// ```rust,should_panic
451+
/// #[track_caller]
452+
/// fn assert_even(n: i32) {
453+
/// assert!(n % 2 == 0, "{n} is not even");
454+
/// }
455+
///
456+
/// // The panic blames this line, not the `assert!` inside `assert_even`.
457+
/// assert_even(3);
458+
/// ```
459+
///
460+
/// The attribute applies to functions with the default `"Rust"` ABI, other than `fn main`.
461+
///
462+
/// For more information, see the Reference on [the `track_caller` attribute].
463+
///
464+
/// [`Location`]: panic::Location
465+
/// [`Location::caller`]: panic::Location::caller
466+
/// [`Option::unwrap`]: Option::unwrap
467+
/// [the `track_caller` attribute]: ../reference/attributes/codegen.html#the-track_caller-attribute
468+
mod track_caller_attribute {}

0 commit comments

Comments
 (0)