Skip to content

Commit a98d81e

Browse files
committed
Add -Zerror-metrics=PATH to save diagnostic metadata to disk
1 parent 2b78d92 commit a98d81e

File tree

5 files changed

+139
-51
lines changed

5 files changed

+139
-51
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Session.vim
1919
*.iml
2020
.vscode
2121
.project
22+
.vim/
2223
.favorites.json
2324
.settings/
2425
.vs/

Diff for: compiler/rustc_driver_impl/src/lib.rs

+39-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ use rustc_metadata::creader::MetadataLoader;
5151
use rustc_metadata::locator;
5252
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
5353
use rustc_session::config::{
54-
nightly_options, ErrorOutputType, Input, OutFileName, OutputType, CG_OPTIONS, Z_OPTIONS,
54+
nightly_options, ErrorOutputType, Input, OutFileName, OutputType, UnstableOptions, CG_OPTIONS,
55+
Z_OPTIONS,
5556
};
5657
use rustc_session::getopts::{self, Matches};
5758
use rustc_session::lint::{Lint, LintId};
@@ -301,6 +302,8 @@ fn run_compiler(
301302
let Some(matches) = handle_options(&default_early_dcx, &args) else { return Ok(()) };
302303

303304
let sopts = config::build_session_options(&mut default_early_dcx, &matches);
305+
// fully initialize ice path static once unstable options are available as context
306+
let ice_file = ice_path_with_config(Some(&sopts.unstable_opts)).clone();
304307

305308
if let Some(ref code) = matches.opt_str("explain") {
306309
handle_explain(&default_early_dcx, diagnostics_registry(), code, sopts.color);
@@ -315,7 +318,7 @@ fn run_compiler(
315318
input: Input::File(PathBuf::new()),
316319
output_file: ofile,
317320
output_dir: odir,
318-
ice_file: ice_path().clone(),
321+
ice_file,
319322
file_loader,
320323
locale_resources: DEFAULT_LOCALE_RESOURCES,
321324
lint_caps: Default::default(),
@@ -357,7 +360,11 @@ fn run_compiler(
357360
// printing some information without compiling, or exiting immediately
358361
// after parsing, etc.
359362
let early_exit = || {
360-
if let Some(guar) = sess.dcx().has_errors() { Err(guar) } else { Ok(()) }
363+
if let Some(guar) = sess.dcx().has_errors() {
364+
Err(guar)
365+
} else {
366+
Ok(())
367+
}
361368
};
362369

363370
// This implements `-Whelp`. It should be handled very early, like
@@ -567,7 +574,11 @@ fn handle_explain(early_dcx: &EarlyDiagCtxt, registry: Registry, code: &str, col
567574
fn show_md_content_with_pager(content: &str, color: ColorConfig) {
568575
let mut fallback_to_println = false;
569576
let pager_name = env::var_os("PAGER").unwrap_or_else(|| {
570-
if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") }
577+
if cfg!(windows) {
578+
OsString::from("more.com")
579+
} else {
580+
OsString::from("less")
581+
}
571582
});
572583

573584
let mut cmd = Command::new(&pager_name);
@@ -1306,25 +1317,43 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
13061317

13071318
static ICE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
13081319

1320+
// This function should only be called from the ICE hook.
1321+
//
1322+
// The intended behavior is that `run_compiler` will invoke `ice_path_with_config` early in the
1323+
// initialization process to properly initialize the ICE_PATH static based on parsed CLI flags.
1324+
//
1325+
// Subsequent calls to either function will then return the proper ICE path as configured by
1326+
// the environment and cli flags
13091327
fn ice_path() -> &'static Option<PathBuf> {
1328+
ice_path_with_config(None)
1329+
}
1330+
1331+
fn ice_path_with_config(config: Option<&UnstableOptions>) -> &'static Option<PathBuf> {
1332+
if ICE_PATH.get().is_some() && config.is_some() && cfg!(debug_assertions) {
1333+
tracing::warn!(
1334+
"ICE_PATH has already been initialized -- files may be emitted at unintended paths"
1335+
)
1336+
}
1337+
13101338
ICE_PATH.get_or_init(|| {
13111339
if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() {
13121340
return None;
13131341
}
1314-
if let Some(s) = std::env::var_os("RUST_BACKTRACE")
1315-
&& s == "0"
1316-
{
1317-
return None;
1318-
}
13191342
let mut path = match std::env::var_os("RUSTC_ICE") {
13201343
Some(s) => {
13211344
if s == "0" {
13221345
// Explicitly opting out of writing ICEs to disk.
13231346
return None;
13241347
}
1348+
if let Some(unstable_opts) = config && unstable_opts.error_metrics.is_some() {
1349+
tracing::warn!("ignoring -Zerror-metrics in favor of RUSTC_ICE for destination of ICE report files");
1350+
}
13251351
PathBuf::from(s)
13261352
}
1327-
None => std::env::current_dir().unwrap_or_default(),
1353+
None => config
1354+
.and_then(|unstable_opts| unstable_opts.error_metrics.to_owned())
1355+
.or_else(|| std::env::current_dir().ok())
1356+
.unwrap_or_default(),
13281357
};
13291358
let now: OffsetDateTime = SystemTime::now().into();
13301359
let file_now = now

Diff for: compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,8 @@ options! {
17141714
"emit the bc module with thin LTO info (default: yes)"),
17151715
enforce_type_length_limit: bool = (false, parse_bool, [TRACKED],
17161716
"enforce the type length limit when monomorphizing instances in codegen"),
1717+
error_metrics: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
1718+
"stores metrics about the errors being emitted by rustc to disk"),
17171719
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
17181720
"export symbols from executables, as if they were dynamic libraries"),
17191721
external_clangrt: bool = (false, parse_bool, [UNTRACKED],

Diff for: tests/run-make/dump-ice-to-disk/rmake.rs

+71-15
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
// or full.
55
// - Check that disabling ICE logging results in zero files created.
66
// - Check that the ICE files contain some of the expected strings.
7+
// - exercise the -Zerror-metrics nightly flag
8+
// - verify what happens when both the nightly flag and env variable are set
9+
// - test the RUST_BACKTRACE=0 behavior against the file creation
10+
711
// See https://github.com/rust-lang/rust/pull/108714
812

913
use run_make_support::{cwd, has_extension, has_prefix, rfs, rustc, shallow_find_files};
1014

1115
fn main() {
1216
rustc().input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();
1317
let default = get_text_from_ice(".").lines().count();
14-
clear_ice_files();
1518

19+
clear_ice_files();
1620
rustc().env("RUSTC_ICE", cwd()).input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();
1721
let ice_text = get_text_from_ice(cwd());
1822
let default_set = ice_text.lines().count();
@@ -25,7 +29,28 @@ fn main() {
2529
ice_files.first().and_then(|f| f.file_name()).and_then(|n| n.to_str()).unwrap();
2630
// Ensure that the ICE dump path doesn't contain `:`, because they cause problems on Windows.
2731
assert!(!ice_file_name.contains(":"), "{ice_file_name}");
32+
assert_eq!(default, default_set);
33+
assert!(default > 0);
34+
// Some of the expected strings in an ICE file should appear.
35+
assert!(content.contains("thread 'rustc' panicked at"));
36+
assert!(content.contains("stack backtrace:"));
37+
38+
test_backtrace_short(default);
39+
test_backtrace_full(default);
40+
test_backtrace_disabled(default);
41+
42+
clear_ice_files();
43+
// The ICE dump is explicitly disabled. Therefore, this should produce no files.
44+
rustc().env("RUSTC_ICE", "0").input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();
45+
let ice_files = shallow_find_files(cwd(), |path| {
46+
has_prefix(path, "rustc-ice") && has_extension(path, "txt")
47+
});
48+
assert!(ice_files.is_empty()); // There should be 0 ICE files.
49+
50+
test_error_metrics_flag(default);
51+
}
2852

53+
fn test_backtrace_short(baseline: usize) {
2954
clear_ice_files();
3055
rustc()
3156
.env("RUSTC_ICE", cwd())
@@ -34,6 +59,11 @@ fn main() {
3459
.arg("-Ztreat-err-as-bug=1")
3560
.run_fail();
3661
let short = get_text_from_ice(cwd()).lines().count();
62+
// backtrace length in dump shouldn't be changed by RUST_BACKTRACE
63+
assert_eq!(short, baseline);
64+
}
65+
66+
fn test_backtrace_full(baseline: usize) {
3767
clear_ice_files();
3868
rustc()
3969
.env("RUSTC_ICE", cwd())
@@ -42,23 +72,49 @@ fn main() {
4272
.arg("-Ztreat-err-as-bug=1")
4373
.run_fail();
4474
let full = get_text_from_ice(cwd()).lines().count();
75+
// backtrace length in dump shouldn't be changed by RUST_BACKTRACE
76+
assert_eq!(full, baseline);
77+
}
78+
79+
fn test_backtrace_disabled(baseline: usize) {
4580
clear_ice_files();
81+
rustc()
82+
.env("RUSTC_ICE", cwd())
83+
.input("lib.rs")
84+
.env("RUST_BACKTRACE", "0")
85+
.arg("-Ztreat-err-as-bug=1")
86+
.run_fail();
87+
let disabled = get_text_from_ice(cwd()).lines().count();
88+
// backtrace length in dump shouldn't be changed by RUST_BACKTRACE
89+
assert_eq!(disabled, baseline);
90+
}
4691

47-
// The ICE dump is explicitly disabled. Therefore, this should produce no files.
48-
rustc().env("RUSTC_ICE", "0").input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();
49-
let ice_files = shallow_find_files(cwd(), |path| {
50-
has_prefix(path, "rustc-ice") && has_extension(path, "txt")
51-
});
52-
assert!(ice_files.is_empty()); // There should be 0 ICE files.
92+
fn test_error_metrics_flag(baseline: usize) {
93+
test_flag_only(baseline);
94+
test_flag_and_env(baseline);
95+
}
5396

54-
// The line count should not change.
55-
assert_eq!(short, default_set);
56-
assert_eq!(short, default);
57-
assert_eq!(full, default_set);
58-
assert!(default > 0);
59-
// Some of the expected strings in an ICE file should appear.
60-
assert!(content.contains("thread 'rustc' panicked at"));
61-
assert!(content.contains("stack backtrace:"));
97+
fn test_flag_only(baseline: usize) {
98+
clear_ice_files();
99+
let metrics_arg = format!("-Zerror-metrics={}", cwd().display());
100+
rustc().input("lib.rs").arg("-Ztreat-err-as-bug=1").arg(metrics_arg).run_fail();
101+
let output = get_text_from_ice(cwd()).lines().count();
102+
assert_eq!(output, baseline);
103+
}
104+
105+
fn test_flag_and_env(baseline: usize) {
106+
clear_ice_files();
107+
let metrics_arg = format!("-Zerror-metrics={}", cwd().display());
108+
let real_dir = cwd().join("actually_put_ice_here");
109+
std::fs::create_dir(real_dir.clone()).unwrap();
110+
rustc()
111+
.input("lib.rs")
112+
.env("RUSTC_ICE", real_dir.clone())
113+
.arg("-Ztreat-err-as-bug=1")
114+
.arg(metrics_arg)
115+
.run_fail();
116+
let output = get_text_from_ice(real_dir).lines().count();
117+
assert_eq!(output, baseline);
62118
}
63119

64120
fn clear_ice_files() {

Diff for: tests/rustdoc-ui/intra-doc/warning-crlf.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
// ignore-tidy-cr
2-
//@ check-pass
3-
4-
// This file checks the spans of intra-link warnings in a file with CRLF line endings. The
5-
// .gitattributes file in this directory should enforce it.
6-
7-
/// [error]
8-
pub struct A;
9-
//~^^ WARNING `error`
10-
11-
///
12-
/// docs [error1]
13-
//~^ WARNING `error1`
14-
15-
/// docs [error2]
16-
///
17-
pub struct B;
18-
//~^^^ WARNING `error2`
19-
20-
/**
21-
* This is a multi-line comment.
22-
*
23-
* It also has an [error].
24-
*/
25-
pub struct C;
26-
//~^^^ WARNING `error`
1+
// ignore-tidy-cr
2+
//@ check-pass
3+
4+
// This file checks the spans of intra-link warnings in a file with CRLF line endings. The
5+
// .gitattributes file in this directory should enforce it.
6+
7+
/// [error]
8+
pub struct A;
9+
//~^^ WARNING `error`
10+
11+
///
12+
/// docs [error1]
13+
//~^ WARNING `error1`
14+
15+
/// docs [error2]
16+
///
17+
pub struct B;
18+
//~^^^ WARNING `error2`
19+
20+
/**
21+
* This is a multi-line comment.
22+
*
23+
* It also has an [error].
24+
*/
25+
pub struct C;
26+
//~^^^ WARNING `error`

0 commit comments

Comments
 (0)