Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c93941

Browse files
committedMay 21, 2020
Document #[track_caller].
1 parent a6efccd commit 2c93941

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎src/attributes.md

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ The following is an index of all built-in attributes.
221221
- [`cold`] — Hint that a function is unlikely to be called.
222222
- [`no_builtins`] — Disables use of certain built-in functions.
223223
- [`target_feature`] — Configure platform-specific code generation.
224+
- [`track_caller`] - Pass the parent call location to `std::panic::Location::caller()`.
224225
- Documentation
225226
- `doc` — Specifies documentation. See [The Rustdoc Book] for more
226227
information. [Doc comments] are transformed into `doc` attributes.
@@ -294,6 +295,7 @@ The following is an index of all built-in attributes.
294295
[`should_panic`]: attributes/testing.md#the-should_panic-attribute
295296
[`target_feature`]: attributes/codegen.md#the-target_feature-attribute
296297
[`test`]: attributes/testing.md#the-test-attribute
298+
[`track_caller`]: attributes/codegen.md#the-track_caller-attribute
297299
[`type_length_limit`]: attributes/limits.md#the-type_length_limit-attribute
298300
[`used`]: abi.md#the-used-attribute
299301
[`warn`]: attributes/diagnostics.md#lint-check-attributes

‎src/attributes/codegen.md

+52
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,54 @@ feature detection on the x86 platforms.
143143
> may be enabled or disabled for an entire crate with the
144144
> [`-C target-feature`] flag.
145145
146+
## The `track_caller` attribute
147+
148+
The `track_caller` attribute may be applied to any function with [`"Rust"` ABI][rust-abi]. When
149+
applied to trait functions and methods, the attribute applies to all implementations. If the trait
150+
provides a default implementation with the attribute, then the attribute also applies to override
151+
implementations.
152+
153+
When applied to a function in an `extern` block the attribute must also be applied to any linked
154+
implementations, otherwise undefined behavior results. When applied to a function which is made
155+
available to an `extern` block, the declaration in the `extern` block must also have the attribute,
156+
otherwise undefined behavior results.
157+
158+
### Behavior
159+
160+
Applying the attribute to a function `f` allows code within `f` to observe the [`Location`] of the
161+
"topmost" tracked call that led to `f`'s invocation. At the point of observation, an implementation
162+
behaves as if it walks up the stack from `f`'s frame to find the nearest frame of an *unattributed*
163+
function `outer`, and it returns the [`Location`] of the tracked call in `outer`.
164+
165+
> Note: `core` provides [`core::panic::Location::caller`] for observing caller locations. It wraps
166+
> the [`core::intrinsics::caller_location`] intrinsic implemented by `rustc`.
167+
168+
#### Examples
169+
170+
If `f` is called directly by `outer`, code in `f` observes its callsite within `outer`.
171+
172+
If `f` is called by another attributed function `g` which is in turn called by `outer`, code in both
173+
`f` and `g` observes `g`'s callsite within `outer`.
174+
175+
If `g` is called by another attributed function `h` which is in turn called by `outer`, all code in
176+
`f`, `g`, and `h` observes `h`'s callsite within `outer`, and so on.
177+
178+
### Limitations
179+
180+
This information is a hint and implementations are not required to preserve it.
181+
182+
In particular, coercing a function with `#[track_caller]` to a function pointer creates a shim which
183+
appears to observers to have been called at the attributed function's definition site, losing actual
184+
caller information across virtual calls. A common example of this coercion is the creation of a
185+
trait object whose methods are attributed.
186+
187+
> Note: The aforementioned shim for function pointers is necessary because `rustc` implements
188+
> `track_caller` in a codegen context by appending an implicit parameter to the function ABI, but
189+
> this would be unsound for an indirect call because the parameter is not a part of the function's
190+
> type and a given function pointer type may or may not refer to a function with the attribute. The
191+
> creation of a shim hides the implicit parameter from callers of the function pointer, preserving
192+
> soundness.
193+
146194
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
147195
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu
148196
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature
@@ -155,3 +203,7 @@ feature detection on the x86 platforms.
155203
[trait]: ../items/traits.md
156204
[undefined behavior]: ../behavior-considered-undefined.md
157205
[unsafe function]: ../unsafe-functions.md
206+
[rust-abi]: ../items/external-blocks.md#abi
207+
[`core::intrinsics::caller_location`]: ../../core/intrinsics/fn.caller_location.html
208+
[`core::panic::Location::caller`]: ../../core/panic/struct.Location.html#method.caller
209+
[`Location`]: ../../core/panic/struct.Location.html

0 commit comments

Comments
 (0)
Please sign in to comment.