]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2091-track-caller/tracked-trait-obj.rs
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
[rust.git] / src / test / ui / rfc-2091-track-caller / tracked-trait-obj.rs
1 // run-pass
2
3 trait Tracked {
4     #[track_caller]
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");
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);
19 }
20
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 }
47
48 fn main() {
49     let tracked: &dyn Tracked = &();
50     // The column is the start of 'track_caller_trait_method'
51     tracked.track_caller_trait_method(line!(), 13);
52
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();
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);
61 }