implement PanicTracker to track t panics · rust-lang/rust@de44231 (original) (raw)

`@@ -7,8 +7,9 @@ use std::ffi::OsStr;

`

7

7

`use std::path::{Path, PathBuf};

`

8

8

`use std::process::{Command, Stdio};

`

9

9

`use std::sync::OnceLock;

`

``

10

`+

use std::thread::panicking;

`

10

11

`use std::time::{Instant, SystemTime, UNIX_EPOCH};

`

11

``

`-

use std::{env, fs, io, str};

`

``

12

`+

use std::{env, fs, io, panic, str};

`

12

13

``

13

14

`use build_helper::util::fail;

`

14

15

`use object::read::archive::ArchiveFile;

`

`@@ -22,6 +23,23 @@ pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};

`

22

23

`#[cfg(test)]

`

23

24

`mod tests;

`

24

25

``

``

26

`` +

/// A wrapper around std::panic::Location used to track the location of panics

``

``

27

`` +

/// triggered by t macro usage.

``

``

28

`+

pub struct PanicTracker<'a>(pub &'a panic::Location<'a>);

`

``

29

+

``

30

`+

impl Drop for PanicTracker<'_> {

`

``

31

`+

fn drop(&mut self) {

`

``

32

`+

if panicking() {

`

``

33

`+

eprintln!(

`

``

34

`+

"Panic was initiated from {}:{}:{}",

`

``

35

`+

self.0.file(),

`

``

36

`+

self.0.line(),

`

``

37

`+

self.0.column()

`

``

38

`+

);

`

``

39

`+

}

`

``

40

`+

}

`

``

41

`+

}

`

``

42

+

25

43

`` /// A helper macro to unwrap a result except also print out details like:

``

26

44

`///

`

27

45

`/// * The file/line of the panic

`

`@@ -32,19 +50,21 @@ mod tests;

`

32

50

`` /// using a Result with try!, but this may change one day...

``

33

51

`#[macro_export]

`

34

52

`macro_rules! t {

`

35

``

`-

($e:expr) => {

`

``

53

`+

($e:expr) => {{

`

``

54

`+

let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());

`

36

55

`match $e {

`

37

56

`Ok(e) => e,

`

38

57

`Err(e) => panic!("{} failed with {}", stringify!($e), e),

`

39

58

`}

`

40

``

`-

};

`

``

59

`+

}};

`

41

60

`// it can show extra info in the second parameter

`

42

``

`-

($e:expr, $extra:expr) => {

`

``

61

`+

($e:expr, $extra:expr) => {{

`

``

62

`+

let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());

`

43

63

`match $e {

`

44

64

`Ok(e) => e,

`

45

65

`Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),

`

46

66

`}

`

47

``

`-

};

`

``

67

`+

}};

`

48

68

`}

`

49

69

``

50

70

`pub use t;

`