Skip to content

Commit 5e3c2c7

Browse files
committed
Document #[track_caller].
1 parent 11e893f commit 5e3c2c7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/attributes.md

+2
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

+22
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ 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, allowing code within the function to
146+
observe the callsite of the topmost `track_caller`-attributed function in the attributed function's
147+
callers. If the attributed function itself is called by an unattributed function, then the
148+
attributed function observes its own callsite. If the attributed function is called by another
149+
attributed function, then code within the callee observes the callsite of the caller, and so on.
150+
151+
> Note: `rustc` implements the `core::intrinsics::caller_location` intrinsic for observing the
152+
> callsite of a function with `#[track_caller]`, wrapped by `core::panic::Location::caller` for
153+
> general use.
154+
155+
Coercing a function with `#[track_caller]` to a function pointer creates a shim which appears to
156+
observers to have been called at the attributed function's definition site.
157+
158+
> Note: The aforementioned shim for function pointers is necessary because `rustc` implements
159+
> `track_caller` in a codegen context by appending an implicit parameter to the function ABI, but
160+
> this would be unsound for an indirect call because the parameter is not a part of the function's
161+
> type and a given function pointer type may or may not refer to a function with the attribute. The
162+
> creation of a shim hides the implicit parameter from callers of the function pointer, preserving
163+
> soundness.
164+
143165
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
144166
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu
145167
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature

0 commit comments

Comments
 (0)