Skip to content

Commit 6cee5bf

Browse files
Rollup merge of #140636 - onur-ozkan:panic-tracker-for-t-macro, r=Kobzol
implement `PanicTracker` to track `t` panics Trying to understand panics triggered by `t` macro is very exhausting (especially on CI failures) because it doesn't provide any information about where the macro was originally invoked. This change adds that missing information when an inner call inside the `t` macro panics. Resolves #137557
2 parents 1c801a3 + de44231 commit 6cee5bf

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/bootstrap/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use tracing::{instrument, span};
5353
pub use utils::change_tracker::{
5454
CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
5555
};
56+
pub use utils::helpers::PanicTracker;
5657

5758
use crate::core::build_steps::vendor::VENDOR_DIR;
5859

src/bootstrap/src/utils/helpers.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use std::ffi::OsStr;
77
use std::path::{Path, PathBuf};
88
use std::process::{Command, Stdio};
99
use std::sync::OnceLock;
10+
use std::thread::panicking;
1011
use std::time::{Instant, SystemTime, UNIX_EPOCH};
11-
use std::{env, fs, io, str};
12+
use std::{env, fs, io, panic, str};
1213

1314
use build_helper::util::fail;
1415
use object::read::archive::ArchiveFile;
@@ -22,6 +23,23 @@ pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};
2223
#[cfg(test)]
2324
mod tests;
2425

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+
2543
/// A helper macro to `unwrap` a result except also print out details like:
2644
///
2745
/// * The file/line of the panic
@@ -32,19 +50,21 @@ mod tests;
3250
/// using a `Result` with `try!`, but this may change one day...
3351
#[macro_export]
3452
macro_rules! t {
35-
($e:expr) => {
53+
($e:expr) => {{
54+
let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());
3655
match $e {
3756
Ok(e) => e,
3857
Err(e) => panic!("{} failed with {}", stringify!($e), e),
3958
}
40-
};
59+
}};
4160
// 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());
4363
match $e {
4464
Ok(e) => e,
4565
Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
4666
}
47-
};
67+
}};
4868
}
4969

5070
pub use t;

0 commit comments

Comments
 (0)