Skip to content

Commit 0b42c74

Browse files
authored
Rollup merge of rust-lang#48266 - pietroalbini:report-compiler-flags-on-ice, r=michaelwoerister
Report non-standard compile flags on ICE Some ICEs (such as the recent rust-lang#48248) only happens when a non-standard compiler flag is provided to rustc, but users don't always report the used flags. This can slow down reproducing the issue, so this PR shows all the non-standard compiler flags in the ICE error message. For example, the output of rust-lang#48248 with this PR is: ``` error: internal compiler error: [...] thread 'rustc' panicked at [...] note: Run with `RUST_BACKTRACE=1` for a backtrace. note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: [...] note: rustc 1.25.0-dev running on x86_64-unknown-linux-gnu note: compiler flags: -C link-dead-code ``` ### Open questions * At the moment, only `-C` and `-Z` flags are shown by default, and all the ones provided by cargo in a standard build are ignored: I did this to only show the flags that probably caused the ICE, and to remove some noise from the message. This removed flags like `opt-level` and `debuginfo` though, could those be useful for reproducing ICEs?
2 parents 29f5c69 + 2985a1a commit 0b42c74

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

src/librustc_driver/lib.rs

+80-5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ pub mod target_features {
139139
const BUG_REPORT_URL: &'static str = "https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.\
140140
md#bug-reports";
141141

142+
const ICE_REPORT_COMPILER_FLAGS: &'static [&'static str] = &[
143+
"Z",
144+
"C",
145+
"crate-type",
146+
];
147+
const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &'static [&'static str] = &[
148+
"metadata",
149+
"extra-filename",
150+
];
151+
const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &'static [&'static str] = &[
152+
"incremental",
153+
];
154+
142155
pub fn abort_on_err<T>(result: Result<T, CompileIncomplete>, sess: &Session) -> T {
143156
match result {
144157
Err(CompileIncomplete::Errored(ErrorReported)) => {
@@ -1431,6 +1444,57 @@ pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
14311444
thread.unwrap().join()
14321445
}
14331446

1447+
/// Get a list of extra command-line flags provided by the user, as strings.
1448+
///
1449+
/// This function is used during ICEs to show more information useful for
1450+
/// debugging, since some ICEs only happens with non-default compiler flags
1451+
/// (and the users don't always report them).
1452+
fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
1453+
let mut args = Vec::new();
1454+
for arg in env::args_os() {
1455+
args.push(arg.to_string_lossy().to_string());
1456+
}
1457+
1458+
let matches = if let Some(matches) = handle_options(&args) {
1459+
matches
1460+
} else {
1461+
return None;
1462+
};
1463+
1464+
let mut result = Vec::new();
1465+
let mut excluded_cargo_defaults = false;
1466+
for flag in ICE_REPORT_COMPILER_FLAGS {
1467+
let prefix = if flag.len() == 1 { "-" } else { "--" };
1468+
1469+
for content in &matches.opt_strs(flag) {
1470+
// Split always returns the first element
1471+
let name = if let Some(first) = content.split('=').next() {
1472+
first
1473+
} else {
1474+
&content
1475+
};
1476+
1477+
let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
1478+
name
1479+
} else {
1480+
content
1481+
};
1482+
1483+
if !ICE_REPORT_COMPILER_FLAGS_EXCLUDE.contains(&name) {
1484+
result.push(format!("{}{} {}", prefix, flag, content));
1485+
} else {
1486+
excluded_cargo_defaults = true;
1487+
}
1488+
}
1489+
}
1490+
1491+
if result.len() > 0 {
1492+
Some((result, excluded_cargo_defaults))
1493+
} else {
1494+
None
1495+
}
1496+
}
1497+
14341498
/// Run a procedure which will detect panics in the compiler and print nicer
14351499
/// error messages rather than just failing the test.
14361500
///
@@ -1462,11 +1526,22 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
14621526
errors::Level::Bug);
14631527
}
14641528

1465-
let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
1466-
format!("we would appreciate a bug report: {}", BUG_REPORT_URL),
1467-
format!("rustc {} running on {}",
1468-
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
1469-
config::host_triple())];
1529+
let mut xs = vec![
1530+
"the compiler unexpectedly panicked. this is a bug.".to_string(),
1531+
format!("we would appreciate a bug report: {}", BUG_REPORT_URL),
1532+
format!("rustc {} running on {}",
1533+
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
1534+
config::host_triple()),
1535+
];
1536+
1537+
if let Some((flags, excluded_cargo_defaults)) = extra_compiler_flags() {
1538+
xs.push(format!("compiler flags: {}", flags.join(" ")));
1539+
1540+
if excluded_cargo_defaults {
1541+
xs.push("some of the compiler flags provided by cargo are hidden".to_string());
1542+
}
1543+
}
1544+
14701545
for note in &xs {
14711546
handler.emit(&MultiSpan::new(),
14721547
&note,

0 commit comments

Comments
 (0)