Skip to content

Commit 6cc6561

Browse files
committed
Document #[track_caller].
1 parent 11e893f commit 6cc6561

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ The following is an index of all built-in attributes.
219219
- [`cold`] — Hint that a function is unlikely to be called.
220220
- [`no_builtins`] — Disables use of certain built-in functions.
221221
- [`target_feature`] — Configure platform-specific code generation.
222+
- [`track_caller`] - Pass the parent call location to `std::panic::Location::caller()`.
222223
- Documentation
223224
- `doc` — Specifies documentation. See [The Rustdoc Book] for more
224225
information. [Doc comments] are transformed into `doc` attributes.
@@ -291,6 +292,7 @@ The following is an index of all built-in attributes.
291292
[`should_panic`]: attributes/testing.md#the-should_panic-attribute
292293
[`target_feature`]: attributes/codegen.md#the-target_feature-attribute
293294
[`test`]: attributes/testing.md#the-test-attribute
295+
[`track_caller`]: attributes/codegen.md#the-track_caller-attribute
294296
[`type_length_limit`]: attributes/limits.md#the-type_length_limit-attribute
295297
[`used`]: abi.md#the-used-attribute
296298
[`warn`]: attributes/diagnostics.md#lint-check-attributes

src/attributes/codegen.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ feature detection on the x86 platforms.
140140
> may be enabled or disabled for an entire crate with the
141141
> [`-C target-feature`] flag.
142142
143+
## The `track_caller` attribute
144+
145+
The `track_caller` attribute applies to function declarations and indicates to the compiler that the
146+
attributed function's generated code should have an implicit `&'static core::panic::Location`
147+
argument which is populated by its caller. If the caller of the attributed function is also
148+
annotated with `#[track_caller]`, then the attributed function should receive the location
149+
its caller received, and so on.
150+
151+
An implementation of `core::intrinsics::caller_location` returns this implicit argument if called
152+
from a function with the attribute. Otherwise it returns the location at which the intrinsic itself
153+
was called.
154+
155+
Creating a function pointer to a function with `#[track_caller]` creates a shim which passes the
156+
location at which the attributed function is defined. The pointer then refers to this shim,
157+
preventing unsoundness or the implicit ABI from leaking into the function pointer's type.
158+
159+
In a const context, the attribute does not affect function ABI and instead informs the const
160+
evaluator to continue walking up the stack when probing for the topmost caller location.
161+
143162
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
144163
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu
145164
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature

0 commit comments

Comments
 (0)