@@ -140,6 +140,28 @@ feature detection on the x86 platforms.
140
140
> may be enabled or disabled for an entire crate with the
141
141
> [ ` -C target-feature ` ] flag.
142
142
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
+
143
165
[ _MetaListNameValueStr_ ] : ../attributes.md#meta-item-attribute-syntax
144
166
[ `-C target-cpu` ] : ../../rustc/codegen-options/index.html#target-cpu
145
167
[ `-C target-feature` ] : ../../rustc/codegen-options/index.html#target-feature
0 commit comments