rust/tests/run-pass/track-caller-attribute.rs

100 lines
2.8 KiB
Rust
Raw Normal View History

2020-07-02 03:37:29 -05:00
#![feature(core_intrinsics)]
2019-12-08 04:06:35 -06:00
use std::panic::Location;
#[track_caller]
fn tracked() -> &'static Location<'static> {
Location::caller() // most importantly, we never get line 7
}
fn nested_intrinsic() -> &'static Location<'static> {
Location::caller()
}
fn nested_tracked() -> &'static Location<'static> {
tracked()
}
macro_rules! caller_location_from_macro {
() => (core::panic::Location::caller());
}
2020-01-28 15:55:07 -06:00
fn test_fn_ptr() {
fn pass_to_ptr_call<T>(f: fn(T), x: T) {
f(x);
}
#[track_caller]
fn tracked_unit(_: ()) {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
pass_to_ptr_call(tracked_unit, ());
}
2020-07-31 11:30:07 -05:00
fn test_trait_obj() {
trait Tracked {
#[track_caller]
fn handle(&self) { // `fn` here is what the `location` should point at.
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
// we only call this via trait object, so the def site should *always* be returned
assert_eq!(location.line(), line!() - 4);
assert_eq!(location.column(), 9);
}
}
impl Tracked for () {}
impl Tracked for u8 {}
let tracked: &dyn Tracked = &5u8;
tracked.handle();
const TRACKED: &dyn Tracked = &();
TRACKED.handle();
}
2019-12-08 04:06:35 -06:00
fn main() {
let location = Location::caller();
2020-01-28 15:55:07 -06:00
let expected_line = line!() - 1;
2019-12-08 04:06:35 -06:00
assert_eq!(location.file(), file!());
2020-01-28 15:55:07 -06:00
assert_eq!(location.line(), expected_line);
2019-12-08 04:06:35 -06:00
assert_eq!(location.column(), 20);
let tracked = tracked();
2020-01-28 15:55:07 -06:00
let expected_line = line!() - 1;
2019-12-08 04:06:35 -06:00
assert_eq!(tracked.file(), file!());
2020-01-28 15:55:07 -06:00
assert_eq!(tracked.line(), expected_line);
2019-12-08 04:06:35 -06:00
assert_eq!(tracked.column(), 19);
let nested = nested_intrinsic();
assert_eq!(nested.file(), file!());
assert_eq!(nested.line(), 11);
assert_eq!(nested.column(), 5);
let contained = nested_tracked();
assert_eq!(contained.file(), file!());
assert_eq!(contained.line(), 15);
assert_eq!(contained.column(), 5);
// `Location::caller()` in a macro should behave similarly to `file!` and `line!`,
// i.e. point to where the macro was invoked, instead of the macro itself.
let inmacro = caller_location_from_macro!();
2020-01-28 15:55:07 -06:00
let expected_line = line!() - 1;
2019-12-08 04:06:35 -06:00
assert_eq!(inmacro.file(), file!());
2020-01-28 15:55:07 -06:00
assert_eq!(inmacro.line(), expected_line);
2019-12-08 04:06:35 -06:00
assert_eq!(inmacro.column(), 19);
let intrinsic = core::intrinsics::caller_location();
2020-01-28 15:55:07 -06:00
let expected_line = line!() - 1;
2019-12-08 04:06:35 -06:00
assert_eq!(intrinsic.file(), file!());
2020-01-28 15:55:07 -06:00
assert_eq!(intrinsic.line(), expected_line);
2019-12-08 04:06:35 -06:00
assert_eq!(intrinsic.column(), 21);
2020-01-28 15:55:07 -06:00
test_fn_ptr();
2020-07-31 11:30:07 -05:00
test_trait_obj();
2019-12-08 04:06:35 -06:00
}