|
| 1 | +#![feature(track_caller, core_intrinsics)] |
| 2 | + |
| 3 | +use std::panic::Location; |
| 4 | + |
| 5 | +#[track_caller] |
| 6 | +fn tracked() -> &'static Location<'static> { |
| 7 | + Location::caller() // most importantly, we never get line 7 |
| 8 | +} |
| 9 | + |
| 10 | +fn nested_intrinsic() -> &'static Location<'static> { |
| 11 | + Location::caller() |
| 12 | +} |
| 13 | + |
| 14 | +fn nested_tracked() -> &'static Location<'static> { |
| 15 | + tracked() |
| 16 | +} |
| 17 | + |
| 18 | +macro_rules! caller_location_from_macro { |
| 19 | + () => (core::panic::Location::caller()); |
| 20 | +} |
| 21 | + |
| 22 | +fn main() { |
| 23 | + let location = Location::caller(); |
| 24 | + assert_eq!(location.file(), file!()); |
| 25 | + assert_eq!(location.line(), 23); |
| 26 | + assert_eq!(location.column(), 20); |
| 27 | + |
| 28 | + let tracked = tracked(); |
| 29 | + assert_eq!(tracked.file(), file!()); |
| 30 | + assert_eq!(tracked.line(), 28); |
| 31 | + assert_eq!(tracked.column(), 19); |
| 32 | + |
| 33 | + let nested = nested_intrinsic(); |
| 34 | + assert_eq!(nested.file(), file!()); |
| 35 | + assert_eq!(nested.line(), 11); |
| 36 | + assert_eq!(nested.column(), 5); |
| 37 | + |
| 38 | + let contained = nested_tracked(); |
| 39 | + assert_eq!(contained.file(), file!()); |
| 40 | + assert_eq!(contained.line(), 15); |
| 41 | + assert_eq!(contained.column(), 5); |
| 42 | + |
| 43 | + // `Location::caller()` in a macro should behave similarly to `file!` and `line!`, |
| 44 | + // i.e. point to where the macro was invoked, instead of the macro itself. |
| 45 | + let inmacro = caller_location_from_macro!(); |
| 46 | + assert_eq!(inmacro.file(), file!()); |
| 47 | + assert_eq!(inmacro.line(), 45); |
| 48 | + assert_eq!(inmacro.column(), 19); |
| 49 | + |
| 50 | + let intrinsic = core::intrinsics::caller_location(); |
| 51 | + assert_eq!(intrinsic.file(), file!()); |
| 52 | + assert_eq!(intrinsic.line(), 50); |
| 53 | + assert_eq!(intrinsic.column(), 21); |
| 54 | +} |
0 commit comments