Skip to content

Commit d8b0069

Browse files
committed
Auto merge of #121415 - matthiaskrgr:rollup-o9zzet4, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #121206 (Top level error handling) - #121261 (coverage: Remove `pending_dups` from the span refiner) - #121336 (triagebot: add queue notifications) - #121373 (Consistently refer to a test's `revision` instead of `cfg`) - #121391 (never patterns: Fix liveness analysis in the presence of never patterns) - #121392 (Unify dylib loading between proc macros and codegen backends) - #121399 (Solaris linker does not support --strip-debug) - #121406 (Add a couple tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3406ada + 35650a4 commit d8b0069

File tree

50 files changed

+452
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+452
-449
lines changed

Diff for: Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4048,7 +4048,6 @@ dependencies = [
40484048
name = "rustc_interface"
40494049
version = "0.0.0"
40504050
dependencies = [
4051-
"libloading",
40524051
"rustc-rayon",
40534052
"rustc-rayon-core",
40544053
"rustc_ast",

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::CRATE_NODE_ID;
33
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
44
use rustc_data_structures::memmap::Mmap;
55
use rustc_data_structures::temp_dir::MaybeTempDir;
6-
use rustc_errors::{DiagCtxt, ErrorGuaranteed};
6+
use rustc_errors::{DiagCtxt, ErrorGuaranteed, FatalError};
77
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
88
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
99
use rustc_metadata::find_native_static_library;
@@ -487,7 +487,9 @@ fn collate_raw_dylibs<'a, 'b>(
487487
}
488488
}
489489
}
490-
sess.compile_status()?;
490+
if let Some(guar) = sess.dcx().has_errors() {
491+
return Err(guar);
492+
}
491493
Ok(dylib_table
492494
.into_iter()
493495
.map(|(name, imports)| {
@@ -720,10 +722,7 @@ fn link_dwarf_object<'a>(
720722
Ok(())
721723
}) {
722724
Ok(()) => {}
723-
Err(e) => {
724-
sess.dcx().emit_err(errors::ThorinErrorWrapper(e));
725-
sess.dcx().abort_if_errors();
726-
}
725+
Err(e) => sess.dcx().emit_fatal(errors::ThorinErrorWrapper(e)),
727726
}
728727
}
729728

@@ -999,7 +998,7 @@ fn link_natively<'a>(
999998
sess.dcx().emit_note(errors::CheckInstalledVisualStudio);
1000999
sess.dcx().emit_note(errors::InsufficientVSCodeProduct);
10011000
}
1002-
sess.dcx().abort_if_errors();
1001+
FatalError.raise();
10031002
}
10041003
}
10051004

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ impl<'a> Linker for GccLinker<'a> {
626626
// it does support --strip-all as a compatibility alias for -s.
627627
// The --strip-debug case is handled by running an external
628628
// `strip` utility as a separate step after linking.
629-
if self.sess.target.os != "illumos" {
629+
if !self.sess.target.is_like_solaris {
630630
self.linker_arg("--strip-debug");
631631
}
632632
}

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
449449
let Some(llfn) = cx.declare_c_main(llfty) else {
450450
// FIXME: We should be smart and show a better diagnostic here.
451451
let span = cx.tcx().def_span(rust_main_def_id);
452-
let dcx = cx.tcx().dcx();
453-
dcx.emit_err(errors::MultipleMainFunctions { span });
454-
dcx.abort_if_errors();
455-
bug!();
452+
cx.tcx().dcx().emit_fatal(errors::MultipleMainFunctions { span });
456453
};
457454

458455
// `main` should respect same config for frame pointer elimination as rest of code

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

+18-21
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,6 @@ pub const EXIT_FAILURE: i32 = 1;
144144
pub const DEFAULT_BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust/issues/new\
145145
?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md";
146146

147-
pub fn abort_on_err<T>(result: Result<T, ErrorGuaranteed>, sess: &Session) -> T {
148-
match result {
149-
Err(..) => {
150-
sess.dcx().abort_if_errors();
151-
panic!("error reported but abort_if_errors didn't abort???");
152-
}
153-
Ok(x) => x,
154-
}
155-
}
156-
157147
pub trait Callbacks {
158148
/// Called before creating the compiler instance
159149
fn config(&mut self, _config: &mut interface::Config) {}
@@ -349,27 +339,33 @@ fn run_compiler(
349339
},
350340
};
351341

352-
callbacks.config(&mut config);
353-
354-
default_early_dcx.abort_if_errors();
355342
drop(default_early_dcx);
356343

344+
callbacks.config(&mut config);
345+
357346
interface::run_compiler(config, |compiler| {
358347
let sess = &compiler.sess;
359348
let codegen_backend = &*compiler.codegen_backend;
360349

350+
// This is used for early exits unrelated to errors. E.g. when just
351+
// printing some information without compiling, or exiting immediately
352+
// after parsing, etc.
353+
let early_exit = || {
354+
if let Some(guar) = sess.dcx().has_errors() { Err(guar) } else { Ok(()) }
355+
};
356+
361357
// This implements `-Whelp`. It should be handled very early, like
362358
// `--help`/`-Zhelp`/`-Chelp`. This is the earliest it can run, because
363359
// it must happen after lints are registered, during session creation.
364360
if sess.opts.describe_lints {
365361
describe_lints(sess);
366-
return sess.compile_status();
362+
return early_exit();
367363
}
368364

369365
let early_dcx = EarlyDiagCtxt::new(sess.opts.error_format);
370366

371367
if print_crate_info(&early_dcx, codegen_backend, sess, has_input) == Compilation::Stop {
372-
return sess.compile_status();
368+
return early_exit();
373369
}
374370

375371
if !has_input {
@@ -378,16 +374,16 @@ fn run_compiler(
378374

379375
if !sess.opts.unstable_opts.ls.is_empty() {
380376
list_metadata(&early_dcx, sess, &*codegen_backend.metadata_loader());
381-
return sess.compile_status();
377+
return early_exit();
382378
}
383379

384380
if sess.opts.unstable_opts.link_only {
385381
process_rlink(sess, compiler);
386-
return sess.compile_status();
382+
return early_exit();
387383
}
388384

389385
let linker = compiler.enter(|queries| {
390-
let early_exit = || sess.compile_status().map(|_| None);
386+
let early_exit = || early_exit().map(|_| None);
391387
queries.parse()?;
392388

393389
if let Some(ppm) = &sess.opts.pretty {
@@ -659,10 +655,11 @@ fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
659655
};
660656
}
661657
};
662-
let result = compiler.codegen_backend.link(sess, codegen_results, &outputs);
663-
abort_on_err(result, sess);
658+
if compiler.codegen_backend.link(sess, codegen_results, &outputs).is_err() {
659+
FatalError.raise();
660+
}
664661
} else {
665-
dcx.emit_fatal(RlinkNotAFile {})
662+
dcx.emit_fatal(RlinkNotAFile {});
666663
}
667664
}
668665

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use rustc_ast as ast;
44
use rustc_ast_pretty::pprust as pprust_ast;
5+
use rustc_errors::FatalError;
56
use rustc_hir as hir;
67
use rustc_hir_pretty as pprust_hir;
78
use rustc_middle::bug;
@@ -18,7 +19,6 @@ use std::fmt::Write;
1819

1920
pub use self::PpMode::*;
2021
pub use self::PpSourceMode::*;
21-
use crate::abort_on_err;
2222

2323
struct AstNoAnn;
2424

@@ -243,7 +243,9 @@ impl<'tcx> PrintExtra<'tcx> {
243243

244244
pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
245245
if ppm.needs_analysis() {
246-
abort_on_err(ex.tcx().analysis(()), sess);
246+
if ex.tcx().analysis(()).is_err() {
247+
FatalError.raise();
248+
}
247249
}
248250

249251
let (src, src_name) = get_source(sess);
@@ -334,7 +336,9 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
334336
ThirTree => {
335337
let tcx = ex.tcx();
336338
let mut out = String::new();
337-
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
339+
if rustc_hir_analysis::check_crate(tcx).is_err() {
340+
FatalError.raise();
341+
}
338342
debug!("pretty printing THIR tree");
339343
for did in tcx.hir().body_owners() {
340344
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_tree(did));
@@ -344,7 +348,9 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
344348
ThirFlat => {
345349
let tcx = ex.tcx();
346350
let mut out = String::new();
347-
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
351+
if rustc_hir_analysis::check_crate(tcx).is_err() {
352+
FatalError.raise();
353+
}
348354
debug!("pretty printing THIR flat");
349355
for did in tcx.hir().body_owners() {
350356
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did));

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

+51-32
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,10 @@ struct DiagCtxtInner {
471471
emitted_diagnostics: FxHashSet<Hash128>,
472472

473473
/// Stashed diagnostics emitted in one stage of the compiler that may be
474-
/// stolen by other stages (e.g. to improve them and add more information).
475-
/// The stashed diagnostics count towards the total error count.
476-
/// When `.abort_if_errors()` is called, these are also emitted.
474+
/// stolen and emitted/cancelled by other stages (e.g. to improve them and
475+
/// add more information). All stashed diagnostics must be emitted with
476+
/// `emit_stashed_diagnostics` by the time the `DiagCtxtInner` is dropped,
477+
/// otherwise an assertion failure will occur.
477478
stashed_diagnostics: FxIndexMap<(Span, StashKey), Diagnostic>,
478479

479480
future_breakage_diagnostics: Vec<Diagnostic>,
@@ -558,7 +559,9 @@ pub struct DiagCtxtFlags {
558559

559560
impl Drop for DiagCtxtInner {
560561
fn drop(&mut self) {
561-
self.emit_stashed_diagnostics();
562+
// Any stashed diagnostics should have been handled by
563+
// `emit_stashed_diagnostics` by now.
564+
assert!(self.stashed_diagnostics.is_empty());
562565

563566
if self.err_guars.is_empty() {
564567
self.flush_delayed()
@@ -750,17 +753,24 @@ impl DiagCtxt {
750753
}
751754

752755
/// Emit all stashed diagnostics.
753-
pub fn emit_stashed_diagnostics(&self) {
756+
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
754757
self.inner.borrow_mut().emit_stashed_diagnostics()
755758
}
756759

757-
/// This excludes lint errors, delayed bugs, and stashed errors.
760+
/// This excludes lint errors, delayed bugs and stashed errors.
758761
#[inline]
759-
pub fn err_count(&self) -> usize {
762+
pub fn err_count_excluding_lint_errs(&self) -> usize {
760763
self.inner.borrow().err_guars.len()
761764
}
762765

763-
/// This excludes normal errors, lint errors and delayed bugs. Unless
766+
/// This excludes delayed bugs and stashed errors.
767+
#[inline]
768+
pub fn err_count(&self) -> usize {
769+
let inner = self.inner.borrow();
770+
inner.err_guars.len() + inner.lint_err_guars.len()
771+
}
772+
773+
/// This excludes normal errors, lint errors, and delayed bugs. Unless
764774
/// absolutely necessary, avoid using this. It's dubious because stashed
765775
/// errors can later be cancelled, so the presence of a stashed error at
766776
/// some point of time doesn't guarantee anything -- there are no
@@ -769,27 +779,29 @@ impl DiagCtxt {
769779
self.inner.borrow().stashed_err_count
770780
}
771781

772-
/// This excludes lint errors, delayed bugs, and stashed errors.
773-
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
774-
self.inner.borrow().has_errors()
782+
/// This excludes lint errors, delayed bugs, and stashed errors. Unless
783+
/// absolutely necessary, prefer `has_errors` to this method.
784+
pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
785+
self.inner.borrow().has_errors_excluding_lint_errors()
775786
}
776787

777-
/// This excludes delayed bugs and stashed errors. Unless absolutely
778-
/// necessary, prefer `has_errors` to this method.
779-
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
780-
self.inner.borrow().has_errors_or_lint_errors()
788+
/// This excludes delayed bugs and stashed errors.
789+
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
790+
self.inner.borrow().has_errors()
781791
}
782792

783793
/// This excludes stashed errors. Unless absolutely necessary, prefer
784-
/// `has_errors` or `has_errors_or_lint_errors` to this method.
785-
pub fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
786-
self.inner.borrow().has_errors_or_lint_errors_or_delayed_bugs()
794+
/// `has_errors` to this method.
795+
pub fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
796+
self.inner.borrow().has_errors_or_delayed_bugs()
787797
}
788798

789799
pub fn print_error_count(&self, registry: &Registry) {
790800
let mut inner = self.inner.borrow_mut();
791801

792-
inner.emit_stashed_diagnostics();
802+
// Any stashed diagnostics should have been handled by
803+
// `emit_stashed_diagnostics` by now.
804+
assert!(inner.stashed_diagnostics.is_empty());
793805

794806
if inner.treat_err_as_bug() {
795807
return;
@@ -864,10 +876,12 @@ impl DiagCtxt {
864876
}
865877
}
866878

879+
/// This excludes delayed bugs and stashed errors. Used for early aborts
880+
/// after errors occurred -- e.g. because continuing in the face of errors is
881+
/// likely to lead to bad results, such as spurious/uninteresting
882+
/// additional errors -- when returning an error `Result` is difficult.
867883
pub fn abort_if_errors(&self) {
868-
let mut inner = self.inner.borrow_mut();
869-
inner.emit_stashed_diagnostics();
870-
if !inner.err_guars.is_empty() {
884+
if self.has_errors().is_some() {
871885
FatalError.raise();
872886
}
873887
}
@@ -1268,10 +1282,10 @@ impl DiagCtxt {
12681282
// `DiagCtxtInner::foo`.
12691283
impl DiagCtxtInner {
12701284
/// Emit all stashed diagnostics.
1271-
fn emit_stashed_diagnostics(&mut self) {
1285+
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
1286+
let mut guar = None;
12721287
let has_errors = !self.err_guars.is_empty();
12731288
for (_, diag) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
1274-
// Decrement the count tracking the stash; emitting will increment it.
12751289
if diag.is_error() {
12761290
if diag.is_lint.is_none() {
12771291
self.stashed_err_count -= 1;
@@ -1284,8 +1298,9 @@ impl DiagCtxtInner {
12841298
continue;
12851299
}
12861300
}
1287-
self.emit_diagnostic(diag);
1301+
guar = guar.or(self.emit_diagnostic(diag));
12881302
}
1303+
guar
12891304
}
12901305

12911306
// Return value is only `Some` if the level is `Error` or `DelayedBug`.
@@ -1329,7 +1344,7 @@ impl DiagCtxtInner {
13291344
DelayedBug => {
13301345
// If we have already emitted at least one error, we don't need
13311346
// to record the delayed bug, because it'll never be used.
1332-
return if let Some(guar) = self.has_errors_or_lint_errors() {
1347+
return if let Some(guar) = self.has_errors() {
13331348
Some(guar)
13341349
} else {
13351350
let backtrace = std::backtrace::Backtrace::capture();
@@ -1445,17 +1460,16 @@ impl DiagCtxtInner {
14451460
.is_some_and(|c| self.err_guars.len() + self.lint_err_guars.len() + 1 >= c.get())
14461461
}
14471462

1448-
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1463+
fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
14491464
self.err_guars.get(0).copied()
14501465
}
14511466

1452-
fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
1453-
self.has_errors().or_else(|| self.lint_err_guars.get(0).copied())
1467+
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1468+
self.has_errors_excluding_lint_errors().or_else(|| self.lint_err_guars.get(0).copied())
14541469
}
14551470

1456-
fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1457-
self.has_errors_or_lint_errors()
1458-
.or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
1471+
fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1472+
self.has_errors().or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14591473
}
14601474

14611475
/// Translate `message` eagerly with `args` to `SubdiagnosticMessage::Eager`.
@@ -1488,6 +1502,11 @@ impl DiagCtxtInner {
14881502
}
14891503

14901504
fn flush_delayed(&mut self) {
1505+
// Stashed diagnostics must be emitted before delayed bugs are flushed.
1506+
// Otherwise, we might ICE prematurely when errors would have
1507+
// eventually happened.
1508+
assert!(self.stashed_diagnostics.is_empty());
1509+
14911510
if self.delayed_bugs.is_empty() {
14921511
return;
14931512
}

0 commit comments

Comments
 (0)