Skip to content

Commit 4820c70

Browse files
committed
added regression test for core::panic::Location::file's lifetime
1 parent c9890ec commit 4820c70

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

core/tests/panic/location.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use core::panic::Location;
2+
3+
// Note: Some of the following tests depend on the source location,
4+
// so please be careful when editing this file.
5+
6+
#[test]
7+
fn location_const_caller() {
8+
const _CALLER_REFERENCE: &Location<'static> = Location::caller();
9+
const _CALLER: Location<'static> = *Location::caller();
10+
}
11+
12+
#[test]
13+
fn location_const_file() {
14+
const CALLER: &Location<'static> = Location::caller();
15+
const FILE: &str = CALLER.file();
16+
assert_eq!(FILE, file!());
17+
}
18+
19+
#[test]
20+
fn location_const_line() {
21+
const CALLER: &Location<'static> = Location::caller();
22+
const LINE: u32 = CALLER.line();
23+
assert_eq!(LINE, 21);
24+
}
25+
26+
#[test]
27+
fn location_const_column() {
28+
const CALLER: &Location<'static> = Location::caller();
29+
const COLUMN: u32 = CALLER.column();
30+
assert_eq!(COLUMN, 40);
31+
}
32+
33+
#[test]
34+
fn location_file_lifetime<'x>() {
35+
// Verify that the returned `&str`s lifetime is derived from the generic
36+
// lifetime 'a, not the lifetime of `&self`, when calling `Location::file`.
37+
// Test failure is indicated by a compile failure, not a runtime panic.
38+
let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file;
39+
}

0 commit comments

Comments
 (0)