1 #![feature(core_intrinsics)]
2 
3 use std::panic::Location;
4 
5 #[track_caller]
tracked() -> &'static Location<'static>6 fn tracked() -> &'static Location<'static> {
7     Location::caller() // most importantly, we never get line 7
8 }
9 
nested_intrinsic() -> &'static Location<'static>10 fn nested_intrinsic() -> &'static Location<'static> {
11     Location::caller()
12 }
13 
nested_tracked() -> &'static Location<'static>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 
test_fn_ptr()22 fn test_fn_ptr() {
23     fn pass_to_ptr_call<T>(f: fn(T), x: T) {
24         f(x);
25     }
26 
27     #[track_caller]
28     fn tracked_unit(_: ()) {
29         let expected_line = line!() - 1;
30         let location = std::panic::Location::caller();
31         assert_eq!(location.file(), file!());
32         assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
33     }
34 
35     pass_to_ptr_call(tracked_unit, ());
36 }
37 
test_trait_obj()38 fn test_trait_obj() {
39     trait Tracked {
40         #[track_caller]
41         fn handle(&self) -> &'static Location<'static> {
42             std::panic::Location::caller()
43         }
44     }
45 
46     impl Tracked for () {}
47     impl Tracked for u8 {}
48 
49     // Test that we get the correct location
50     // even with a call through a trait object
51 
52     let tracked: &dyn Tracked = &5u8;
53     let location = tracked.handle();
54     let expected_line = line!() - 1;
55     assert_eq!(location.file(), file!());
56     assert_eq!(location.line(), expected_line);
57     assert_eq!(location.column(), 28);
58 
59     const TRACKED: &dyn Tracked = &();
60     let location = TRACKED.handle();
61     let expected_line = line!() - 1;
62     assert_eq!(location.file(), file!());
63     assert_eq!(location.line(), expected_line);
64     assert_eq!(location.column(), 28);
65 
66 }
67 
main()68 fn main() {
69     let location = Location::caller();
70     let expected_line = line!() - 1;
71     assert_eq!(location.file(), file!());
72     assert_eq!(location.line(), expected_line);
73     assert_eq!(location.column(), 20);
74 
75     let tracked = tracked();
76     let expected_line = line!() - 1;
77     assert_eq!(tracked.file(), file!());
78     assert_eq!(tracked.line(), expected_line);
79     assert_eq!(tracked.column(), 19);
80 
81     let nested = nested_intrinsic();
82     assert_eq!(nested.file(), file!());
83     assert_eq!(nested.line(), 11);
84     assert_eq!(nested.column(), 5);
85 
86     let contained = nested_tracked();
87     assert_eq!(contained.file(), file!());
88     assert_eq!(contained.line(), 15);
89     assert_eq!(contained.column(), 5);
90 
91     // `Location::caller()` in a macro should behave similarly to `file!` and `line!`,
92     // i.e. point to where the macro was invoked, instead of the macro itself.
93     let inmacro = caller_location_from_macro!();
94     let expected_line = line!() - 1;
95     assert_eq!(inmacro.file(), file!());
96     assert_eq!(inmacro.line(), expected_line);
97     assert_eq!(inmacro.column(), 19);
98 
99     let intrinsic = core::intrinsics::caller_location();
100     let expected_line = line!() - 1;
101     assert_eq!(intrinsic.file(), file!());
102     assert_eq!(intrinsic.line(), expected_line);
103     assert_eq!(intrinsic.column(), 21);
104 
105     test_fn_ptr();
106     test_trait_obj();
107 }
108