5 fn track_caller_trait_method(&self, line: u32, col: u32) {
6 let location = std::panic::Location::caller();
7 assert_eq!(location.file(), file!());
8 // The trait method definition is annotated with `#[track_caller]`,
9 // so caller location information will work through a method
10 // call on a trait object
11 assert_eq!(location.line(), line, "Bad line");
12 assert_eq!(location.column(), col, "Bad col");
15 fn track_caller_not_on_trait_method(&self);
18 fn track_caller_through_self(self: Box<Self>, line: u32, col: u32);
22 // We have `#[track_caller]` on the implementation of the method,
23 // but not on the definition of the method in the trait. Therefore,
24 // caller location information will *not* work through a method call
25 // on a trait object. Instead, we will get the location of this method
27 fn track_caller_not_on_trait_method(&self) {
28 let location = std::panic::Location::caller();
29 assert_eq!(location.file(), file!());
30 assert_eq!(location.line(), line!() - 3);
31 assert_eq!(location.column(), 5);
34 // We don't have a `#[track_caller]` attribute, but
35 // `#[track_caller]` is present on the trait definition,
36 // so we'll still get location information
37 fn track_caller_through_self(self: Box<Self>, line: u32, col: u32) {
38 let location = std::panic::Location::caller();
39 assert_eq!(location.file(), file!());
40 // The trait method definition is annotated with `#[track_caller]`,
41 // so caller location information will work through a method
42 // call on a trait object
43 assert_eq!(location.line(), line, "Bad line");
44 assert_eq!(location.column(), col, "Bad col");
49 let tracked: &dyn Tracked = &();
50 // The column is the start of 'track_caller_trait_method'
51 tracked.track_caller_trait_method(line!(), 13);
53 const TRACKED: &dyn Tracked = &();
54 // The column is the start of 'track_caller_trait_method'
55 TRACKED.track_caller_trait_method(line!(), 13);
56 TRACKED.track_caller_not_on_trait_method();
58 // The column is the start of `track_caller_through_self`
59 let boxed: Box<dyn Tracked> = Box::new(());
60 boxed.track_caller_through_self(line!(), 11);