Skip to content

Commit d237378

Browse files
committed
Some cleanups around EarlyDiagCtxt
All callers of EarlyDiagCtxt::early_error now emit a fatal error.
1 parent 6dd75f0 commit d237378

File tree

8 files changed

+26
-40
lines changed

8 files changed

+26
-40
lines changed

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{env, error, fmt, fs, io};
22

33
use rustc_session::EarlyDiagCtxt;
4-
use rustc_span::ErrorGuaranteed;
54

65
/// Expands argfiles in command line arguments.
76
#[derive(Default)]
@@ -118,22 +117,22 @@ pub fn arg_expand_all(early_dcx: &EarlyDiagCtxt, at_args: &[String]) -> Vec<Stri
118117
///
119118
/// This function is identical to [`env::args()`] except that it emits an error when it encounters
120119
/// non-Unicode arguments instead of panicking.
121-
pub fn raw_args(early_dcx: &EarlyDiagCtxt) -> Result<Vec<String>, ErrorGuaranteed> {
122-
let mut res = Ok(Vec::new());
120+
pub fn raw_args(early_dcx: &EarlyDiagCtxt) -> Vec<String> {
121+
let mut args = Vec::new();
122+
let mut guar = Ok(());
123123
for (i, arg) in env::args_os().enumerate() {
124124
match arg.into_string() {
125-
Ok(arg) => {
126-
if let Ok(args) = &mut res {
127-
args.push(arg);
128-
}
129-
}
125+
Ok(arg) => args.push(arg),
130126
Err(arg) => {
131-
res =
127+
guar =
132128
Err(early_dcx.early_err(format!("argument {i} is not valid Unicode: {arg:?}")))
133129
}
134130
}
135131
}
136-
res
132+
if let Err(guar) = guar {
133+
guar.raise_fatal();
134+
}
135+
args
137136
}
138137

139138
#[derive(Debug)]

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,9 @@ pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, FatalError> {
12121212

12131213
/// Variant of `catch_fatal_errors` for the `interface::Result` return type
12141214
/// that also computes the exit code.
1215-
pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
1215+
pub fn catch_with_exit_code(f: impl FnOnce()) -> i32 {
12161216
match catch_fatal_errors(f) {
1217-
Ok(Ok(())) => EXIT_SUCCESS,
1217+
Ok(()) => EXIT_SUCCESS,
12181218
_ => EXIT_FAILURE,
12191219
}
12201220
}
@@ -1499,10 +1499,8 @@ pub fn main() -> ! {
14991499
install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
15001500
install_ctrlc_handler();
15011501

1502-
let exit_code = catch_with_exit_code(|| {
1503-
run_compiler(&args::raw_args(&early_dcx)?, &mut callbacks);
1504-
Ok(())
1505-
});
1502+
let exit_code =
1503+
catch_with_exit_code(|| run_compiler(&args::raw_args(&early_dcx), &mut callbacks));
15061504

15071505
if let Some(format) = callbacks.time_passes {
15081506
let end_rss = get_resident_set_size();

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ pub fn parse_error_format(
18191819
ErrorOutputType::HumanReadable(HumanReadableErrorType::Unicode, color)
18201820
}
18211821
Some(arg) => {
1822-
early_dcx.abort_if_error_and_set_error_format(ErrorOutputType::HumanReadable(
1822+
early_dcx.set_error_format(ErrorOutputType::HumanReadable(
18231823
HumanReadableErrorType::Default,
18241824
color,
18251825
));
@@ -2360,7 +2360,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
23602360

23612361
let error_format = parse_error_format(early_dcx, matches, color, json_color, json_rendered);
23622362

2363-
early_dcx.abort_if_error_and_set_error_format(error_format);
2363+
early_dcx.set_error_format(error_format);
23642364

23652365
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_else(|_| {
23662366
early_dcx.early_fatal("`--diagnostic-width` must be an positive integer");
@@ -2770,6 +2770,7 @@ pub mod nightly_options {
27702770
"the option `{}` is only accepted on the nightly compiler",
27712771
opt.name
27722772
);
2773+
// The non-zero nightly_options_on_stable will force an early_fatal eventually.
27732774
let _ = early_dcx.early_err(msg);
27742775
}
27752776
OptionStability::Stable => {}

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

+4-11
Original file line numberDiff line numberDiff line change
@@ -1362,23 +1362,16 @@ pub struct EarlyDiagCtxt {
13621362
dcx: DiagCtxt,
13631363
}
13641364

1365-
impl Default for EarlyDiagCtxt {
1366-
fn default() -> Self {
1367-
Self::new(ErrorOutputType::default())
1368-
}
1369-
}
1370-
13711365
impl EarlyDiagCtxt {
13721366
pub fn new(output: ErrorOutputType) -> Self {
13731367
let emitter = mk_emitter(output);
13741368
Self { dcx: DiagCtxt::new(emitter) }
13751369
}
13761370

13771371
/// Swap out the underlying dcx once we acquire the user's preference on error emission
1378-
/// format. Any errors prior to that will cause an abort and all stashed diagnostics of the
1379-
/// previous dcx will be emitted.
1380-
pub fn abort_if_error_and_set_error_format(&mut self, output: ErrorOutputType) {
1381-
self.dcx.handle().abort_if_errors();
1372+
/// format. If `early_err` was previously called this will panic.
1373+
pub fn set_error_format(&mut self, output: ErrorOutputType) {
1374+
assert!(self.dcx.handle().has_errors().is_none());
13821375

13831376
let emitter = mk_emitter(output);
13841377
self.dcx = DiagCtxt::new(emitter);
@@ -1398,7 +1391,7 @@ impl EarlyDiagCtxt {
13981391

13991392
#[allow(rustc::untranslatable_diagnostic)]
14001393
#[allow(rustc::diagnostic_outside_of_impl)]
1401-
#[must_use = "ErrorGuaranteed must be returned from `run_compiler` in order to exit with a non-zero status code"]
1394+
#[must_use = "raise_fatal must be called on the returned ErrorGuaranteed in order to exit with a non-zero status code"]
14021395
pub fn early_err(&self, msg: impl Into<DiagMessage>) -> ErrorGuaranteed {
14031396
self.dcx.handle().err(msg)
14041397
}

Diff for: src/librustdoc/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ pub fn main() {
177177
rustc_driver::init_logger(&early_dcx, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
178178

179179
let exit_code = rustc_driver::catch_with_exit_code(|| {
180-
let at_args = rustc_driver::args::raw_args(&early_dcx)?;
180+
let at_args = rustc_driver::args::raw_args(&early_dcx);
181181
main_args(&mut early_dcx, &at_args);
182-
Ok(())
183182
});
184183
process::exit(exit_code);
185184
}

Diff for: src/tools/clippy/src/driver.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn main() {
197197
});
198198

199199
exit(rustc_driver::catch_with_exit_code(move || {
200-
let mut orig_args = rustc_driver::args::raw_args(&early_dcx)?;
200+
let mut orig_args = rustc_driver::args::raw_args(&early_dcx);
201201

202202
let has_sysroot_arg = |args: &mut [String]| -> bool {
203203
if has_arg(args, "--sysroot") {
@@ -239,7 +239,7 @@ pub fn main() {
239239
pass_sysroot_env_if_given(&mut args, sys_root_env);
240240

241241
rustc_driver::run_compiler(&args, &mut DefaultCallbacks);
242-
return Ok(());
242+
return;
243243
}
244244

245245
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
@@ -301,7 +301,6 @@ pub fn main() {
301301
} else {
302302
rustc_driver::run_compiler(&args, &mut RustcCallbacks { clippy_args_var });
303303
}
304-
Ok(())
305304
}))
306305
}
307306

Diff for: src/tools/miri/src/bin/miri.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,8 @@ fn run_compiler_and_exit(
379379
callbacks: &mut (dyn rustc_driver::Callbacks + Send),
380380
) -> ! {
381381
// Invoke compiler, and handle return code.
382-
let exit_code = rustc_driver::catch_with_exit_code(move || {
383-
rustc_driver::run_compiler(args, callbacks);
384-
Ok(())
385-
});
382+
let exit_code =
383+
rustc_driver::catch_with_exit_code(move || rustc_driver::run_compiler(args, callbacks));
386384
std::process::exit(exit_code)
387385
}
388386

@@ -461,7 +459,7 @@ fn main() {
461459
// (`install_ice_hook` might change `RUST_BACKTRACE`.)
462460
let env_snapshot = env::vars_os().collect::<Vec<_>>();
463461

464-
let args = rustc_driver::args::raw_args(&early_dcx)
462+
let args = rustc_driver::catch_fatal_errors(|| rustc_driver::args::raw_args(&early_dcx))
465463
.unwrap_or_else(|_| std::process::exit(rustc_driver::EXIT_FAILURE));
466464

467465
// Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`, even if

Diff for: tests/ui-fulldeps/obtain-borrowck.rs

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ fn main() {
4848
let mut callbacks = CompilerCalls::default();
4949
// Call the Rust compiler with our callbacks.
5050
rustc_driver::run_compiler(&rustc_args, &mut callbacks);
51-
Ok(())
5251
});
5352
std::process::exit(exit_code);
5453
}

0 commit comments

Comments
 (0)