]> git.proxmox.com Git - rustc.git/blame - src/test/ui/rfc-2091-track-caller/tracked-trait-obj.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / ui / rfc-2091-track-caller / tracked-trait-obj.rs
CommitLineData
f9652781
XL
1// run-pass
2
3trait Tracked {
4 #[track_caller]
136023e0 5 fn track_caller_trait_method(&self, line: u32, col: u32) {
f9652781
XL
6 let location = std::panic::Location::caller();
7 assert_eq!(location.file(), file!());
136023e0
XL
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");
f9652781 13 }
136023e0
XL
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);
f9652781
XL
19}
20
136023e0
XL
21impl 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}
f9652781
XL
47
48fn main() {
136023e0
XL
49 let tracked: &dyn Tracked = &();
50 // The column is the start of 'track_caller_trait_method'
51 tracked.track_caller_trait_method(line!(), 13);
f9652781
XL
52
53 const TRACKED: &dyn Tracked = &();
136023e0
XL
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);
f9652781 61}