|
2 | 2 |
|
3 | 3 | trait Tracked {
|
4 | 4 | #[track_caller]
|
5 |
| - fn handle(&self) { |
| 5 | + fn track_caller_trait_method(&self, line: u32, col: u32) { |
6 | 6 | let location = std::panic::Location::caller();
|
7 | 7 | assert_eq!(location.file(), file!());
|
8 |
| - // we only call this via trait object, so the def site should *always* be returned |
9 |
| - assert_eq!(location.line(), line!() - 4); |
10 |
| - assert_eq!(location.column(), 5); |
| 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"); |
11 | 13 | }
|
| 14 | + |
| 15 | + fn track_caller_not_on_trait_method(&self); |
| 16 | + |
| 17 | + #[track_caller] |
| 18 | + fn track_caller_through_self(self: Box<Self>, line: u32, col: u32); |
12 | 19 | }
|
13 | 20 |
|
14 |
| -impl Tracked for () {} |
15 |
| -impl Tracked for u8 {} |
| 21 | +impl Tracked for () { |
| 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 |
| 26 | + #[track_caller] |
| 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); |
| 32 | + } |
| 33 | + |
| 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"); |
| 45 | + } |
| 46 | +} |
16 | 47 |
|
17 | 48 | fn main() {
|
18 |
| - let tracked: &dyn Tracked = &5u8; |
19 |
| - tracked.handle(); |
| 49 | + let tracked: &dyn Tracked = &(); |
| 50 | + // The column is the start of 'track_caller_trait_method' |
| 51 | + tracked.track_caller_trait_method(line!(), 13); |
20 | 52 |
|
21 | 53 | const TRACKED: &dyn Tracked = &();
|
22 |
| - TRACKED.handle(); |
| 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(); |
| 57 | + |
| 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); |
23 | 61 | }
|
0 commit comments