|
| 1 | +// run-pass |
| 2 | + |
| 3 | +#![feature(stmt_expr_attributes)] |
| 4 | + |
| 5 | +use std::panic::Location; |
| 6 | + |
| 7 | +#[track_caller] |
| 8 | +fn tracked() -> &'static Location<'static> { |
| 9 | + let get_location = #[track_caller] || Location::caller(); |
| 10 | + get_location() |
| 11 | +} |
| 12 | + |
| 13 | +fn untracked_wrapper() -> (&'static Location<'static>, u32) { |
| 14 | + let get_location = #[track_caller] || Location::caller(); |
| 15 | + (get_location(), line!()) |
| 16 | +} |
| 17 | + |
| 18 | +fn nested_tracked() -> (&'static Location<'static>, u32) { |
| 19 | + (tracked(), line!()) |
| 20 | +} |
| 21 | + |
| 22 | +fn main() { |
| 23 | + let get_location = #[track_caller] || Location::caller(); |
| 24 | + let (location, line) = (get_location(), line!()); |
| 25 | + assert_eq!(location.file(), file!()); |
| 26 | + assert_eq!(location.line(), line); |
| 27 | + |
| 28 | + let (tracked, tracked_line) = (tracked(), line!()); |
| 29 | + assert_eq!(tracked.file(), file!()); |
| 30 | + assert_eq!(tracked.line(), tracked_line); |
| 31 | + |
| 32 | + let (nested, nested_line) = untracked_wrapper(); |
| 33 | + assert_eq!(nested.file(), file!()); |
| 34 | + assert_eq!(nested.line(), nested_line); |
| 35 | + |
| 36 | + let (contained, contained_line) = nested_tracked(); |
| 37 | + assert_eq!(contained.file(), file!()); |
| 38 | + assert_eq!(contained.line(), contained_line); |
| 39 | + |
| 40 | + fn pass_to_ptr_call<T, R>(f: fn(T) -> R, x: T) -> R { |
| 41 | + f(x) |
| 42 | + } |
| 43 | + |
| 44 | + let (get_location_w_n, line_from_shim) = (#[track_caller] |_| Location::caller(), line!()); |
| 45 | + |
| 46 | + let (location_with_arg, line_with_arg) = (get_location_w_n(3), line!()); |
| 47 | + assert_eq!(location_with_arg.file(), file!()); |
| 48 | + assert_eq!(location_with_arg.line(), line_with_arg); |
| 49 | + |
| 50 | + let location_with_shim = pass_to_ptr_call(get_location_w_n, 5); |
| 51 | + // FIXME make the closure's "def site" point to this file |
| 52 | + assert_eq!(location_with_shim.file(), file!()); |
| 53 | + assert_eq!(location_with_shim.line(), line_from_shim); |
| 54 | +} |
0 commit comments