Skip to content

Commit a8b905c

Browse files
committed
Auto merge of rust-lang#115158 - Enselic:break-rust-args, r=compiler-errors
Include compiler flags when you `break rust;` Closes rust-lang#70661 r? `@RalfJung` who requested this feature :)
2 parents 25ed43d + d5e79f2 commit a8b905c

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

compiler/rustc_driver_impl/src/lib.rs

+1-48
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ pub const EXIT_FAILURE: i32 = 1;
140140
pub const DEFAULT_BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust/issues/new\
141141
?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md";
142142

143-
const ICE_REPORT_COMPILER_FLAGS: &[&str] = &["-Z", "-C", "--crate-type"];
144-
145-
const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &[&str] = &["metadata", "extra-filename"];
146-
147-
const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &[&str] = &["incremental"];
148-
149143
pub fn abort_on_err<T>(result: Result<T, ErrorGuaranteed>, sess: &Session) -> T {
150144
match result {
151145
Err(..) => {
@@ -1250,47 +1244,6 @@ fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
12501244
}
12511245
}
12521246

1253-
/// Gets a list of extra command-line flags provided by the user, as strings.
1254-
///
1255-
/// This function is used during ICEs to show more information useful for
1256-
/// debugging, since some ICEs only happens with non-default compiler flags
1257-
/// (and the users don't always report them).
1258-
fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
1259-
let mut args = env::args_os().map(|arg| arg.to_string_lossy().to_string()).peekable();
1260-
1261-
let mut result = Vec::new();
1262-
let mut excluded_cargo_defaults = false;
1263-
while let Some(arg) = args.next() {
1264-
if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) {
1265-
let content = if arg.len() == a.len() {
1266-
// A space-separated option, like `-C incremental=foo` or `--crate-type rlib`
1267-
match args.next() {
1268-
Some(arg) => arg.to_string(),
1269-
None => continue,
1270-
}
1271-
} else if arg.get(a.len()..a.len() + 1) == Some("=") {
1272-
// An equals option, like `--crate-type=rlib`
1273-
arg[a.len() + 1..].to_string()
1274-
} else {
1275-
// A non-space option, like `-Cincremental=foo`
1276-
arg[a.len()..].to_string()
1277-
};
1278-
let option = content.split_once('=').map(|s| s.0).unwrap_or(&content);
1279-
if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.iter().any(|exc| option == *exc) {
1280-
excluded_cargo_defaults = true;
1281-
} else {
1282-
result.push(a.to_string());
1283-
match ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.iter().find(|s| option == **s) {
1284-
Some(s) => result.push(format!("{s}=[REDACTED]")),
1285-
None => result.push(content),
1286-
}
1287-
}
1288-
}
1289-
}
1290-
1291-
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
1292-
}
1293-
12941247
/// Runs a closure and catches unwinds triggered by fatal errors.
12951248
///
12961249
/// The compiler currently unwinds with a special sentinel value to abort
@@ -1449,7 +1402,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14491402
None
14501403
};
14511404

1452-
if let Some((flags, excluded_cargo_defaults)) = extra_compiler_flags() {
1405+
if let Some((flags, excluded_cargo_defaults)) = rustc_session::utils::extra_compiler_flags() {
14531406
handler.emit_note(session_diagnostics::IceFlags { flags: flags.join(" ") });
14541407
if excluded_cargo_defaults {
14551408
handler.emit_note(session_diagnostics::IceExcludeCargoDefaults);

compiler/rustc_hir_typeck/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ fn fatally_break_rust(tcx: TyCtxt<'_>) {
436436
tcx.sess.cfg_version,
437437
config::host_triple(),
438438
));
439+
if let Some((flags, excluded_cargo_defaults)) = rustc_session::utils::extra_compiler_flags() {
440+
handler.note_without_error(format!("compiler flags: {}", flags.join(" ")));
441+
if excluded_cargo_defaults {
442+
handler.note_without_error("some of the compiler flags provided by cargo are hidden");
443+
}
444+
}
439445
}
440446

441447
fn has_expected_num_generic_args(tcx: TyCtxt<'_>, trait_did: DefId, expected: usize) -> bool {

compiler/rustc_session/src/utils.rs

+47
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,50 @@ impl CanonicalizedPath {
111111
&self.original
112112
}
113113
}
114+
115+
/// Gets a list of extra command-line flags provided by the user, as strings.
116+
///
117+
/// This function is used during ICEs to show more information useful for
118+
/// debugging, since some ICEs only happens with non-default compiler flags
119+
/// (and the users don't always report them).
120+
pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
121+
const ICE_REPORT_COMPILER_FLAGS: &[&str] = &["-Z", "-C", "--crate-type"];
122+
123+
const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &[&str] = &["metadata", "extra-filename"];
124+
125+
const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &[&str] = &["incremental"];
126+
127+
let mut args = std::env::args_os().map(|arg| arg.to_string_lossy().to_string()).peekable();
128+
129+
let mut result = Vec::new();
130+
let mut excluded_cargo_defaults = false;
131+
while let Some(arg) = args.next() {
132+
if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) {
133+
let content = if arg.len() == a.len() {
134+
// A space-separated option, like `-C incremental=foo` or `--crate-type rlib`
135+
match args.next() {
136+
Some(arg) => arg.to_string(),
137+
None => continue,
138+
}
139+
} else if arg.get(a.len()..a.len() + 1) == Some("=") {
140+
// An equals option, like `--crate-type=rlib`
141+
arg[a.len() + 1..].to_string()
142+
} else {
143+
// A non-space option, like `-Cincremental=foo`
144+
arg[a.len()..].to_string()
145+
};
146+
let option = content.split_once('=').map(|s| s.0).unwrap_or(&content);
147+
if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.iter().any(|exc| option == *exc) {
148+
excluded_cargo_defaults = true;
149+
} else {
150+
result.push(a.to_string());
151+
match ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.iter().find(|s| option == **s) {
152+
Some(s) => result.push(format!("{s}=[REDACTED]")),
153+
None => result.push(content),
154+
}
155+
}
156+
}
157+
}
158+
159+
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
160+
}

tests/ui/track-diagnostics/track.rs

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
// normalize-stderr-test ".rs:\d+:\d+" -> ".rs:LL:CC"
77
// normalize-stderr-test "note: rustc .+ running on .+" -> "note: rustc $$VERSION running on $$TARGET"
88

9+
// The test becomes too flaky if we care about exact args. If `-Z ui-testing`
10+
// from compiletest and `-Z track-diagnostics` from `// compile-flags` at the
11+
// top of this file are present, then assume all args are present.
12+
// normalize-stderr-test "note: compiler flags: .*-Z ui-testing.*-Z track-diagnostics" -> "note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics"
13+
914
fn main() {
1015
break rust
1116
}

tests/ui/track-diagnostics/track.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ note: we would appreciate a joke overview: https://github.com/rust-lang/rust/iss
2020

2121
note: rustc $VERSION running on $TARGET
2222

23+
note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics
24+
2325
error: aborting due to 3 previous errors
2426

2527
Some errors have detailed explanations: E0268, E0425.

0 commit comments

Comments
 (0)