Skip to content

Commit 03ef0e8

Browse files
committed
Reduce the amount of explicit FatalError.raise()
Instead use dcx.abort_if_error() or guar.raise_fatal() instead. These guarantee that an error actually happened previously and thus we don't silently abort.
1 parent 3bf62cc commit 03ef0e8

File tree

6 files changed

+22
-41
lines changed

6 files changed

+22
-41
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_ast::CRATE_NODE_ID;
1515
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1616
use rustc_data_structures::memmap::Mmap;
1717
use rustc_data_structures::temp_dir::MaybeTempDir;
18-
use rustc_errors::{DiagCtxtHandle, FatalError};
18+
use rustc_errors::DiagCtxtHandle;
1919
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
2020
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2121
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
@@ -1039,22 +1039,22 @@ fn link_natively(
10391039
Err(e) => {
10401040
let linker_not_found = e.kind() == io::ErrorKind::NotFound;
10411041

1042-
if linker_not_found {
1043-
sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e });
1042+
let err = if linker_not_found {
1043+
sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e })
10441044
} else {
10451045
sess.dcx().emit_err(errors::UnableToExeLinker {
10461046
linker_path,
10471047
error: e,
10481048
command_formatted: format!("{cmd:?}"),
1049-
});
1050-
}
1049+
})
1050+
};
10511051

10521052
if sess.target.is_like_msvc && linker_not_found {
10531053
sess.dcx().emit_note(errors::MsvcMissingLinker);
10541054
sess.dcx().emit_note(errors::CheckInstalledVisualStudio);
10551055
sess.dcx().emit_note(errors::InsufficientVSCodeProduct);
10561056
}
1057-
FatalError.raise();
1057+
err.raise_fatal();
10581058
}
10591059
}
10601060

compiler/rustc_driver_impl/src/pretty.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::cell::Cell;
44
use std::fmt::Write;
55

66
use rustc_ast_pretty::pprust as pprust_ast;
7-
use rustc_errors::FatalError;
87
use rustc_middle::bug;
98
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
109
use rustc_middle::ty::{self, TyCtxt};
@@ -311,9 +310,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
311310
let tcx = ex.tcx();
312311
let mut out = String::new();
313312
rustc_hir_analysis::check_crate(tcx);
314-
if tcx.dcx().has_errors().is_some() {
315-
FatalError.raise();
316-
}
313+
tcx.dcx().abort_if_errors();
317314
debug!("pretty printing THIR tree");
318315
for did in tcx.hir().body_owners() {
319316
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_tree(did));
@@ -324,9 +321,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
324321
let tcx = ex.tcx();
325322
let mut out = String::new();
326323
rustc_hir_analysis::check_crate(tcx);
327-
if tcx.dcx().has_errors().is_some() {
328-
FatalError.raise();
329-
}
324+
tcx.dcx().abort_if_errors();
330325
debug!("pretty printing THIR flat");
331326
for did in tcx.hir().body_owners() {
332327
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did));

compiler/rustc_parse/src/parser/diagnostics.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_ast_pretty::pprust;
1616
use rustc_data_structures::fx::FxHashSet;
1717
use rustc_data_structures::sync::Lrc;
1818
use rustc_errors::{
19-
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, PResult, Subdiagnostic,
20-
Suggestions, pluralize,
19+
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions,
20+
pluralize,
2121
};
2222
use rustc_session::errors::ExprParenthesesNeeded;
2323
use rustc_span::edit_distance::find_best_match_for_name;
@@ -3057,17 +3057,10 @@ impl<'a> Parser<'a> {
30573057
}
30583058

30593059
pub(super) fn recover_vcs_conflict_marker(&mut self) {
3060-
if let Err(err) = self.err_vcs_conflict_marker() {
3061-
err.emit();
3062-
FatalError.raise();
3063-
}
3064-
}
3065-
3066-
pub(crate) fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
30673060
// <<<<<<<
30683061
let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
30693062
else {
3070-
return Ok(());
3063+
return;
30713064
};
30723065
let mut spans = Vec::with_capacity(3);
30733066
spans.push(start);
@@ -3097,7 +3090,7 @@ impl<'a> Parser<'a> {
30973090
self.bump();
30983091
}
30993092

3100-
let mut err = self.dcx().struct_span_err(spans, "encountered diff marker");
3093+
let mut err = self.dcx().struct_span_fatal(spans, "encountered diff marker");
31013094
match middlediff3 {
31023095
// We're using diff3
31033096
Some(middlediff3) => {
@@ -3140,7 +3133,7 @@ impl<'a> Parser<'a> {
31403133
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
31413134
);
31423135

3143-
Err(err)
3136+
err.emit();
31443137
}
31453138

31463139
/// Parse and throw away a parenthesized comma separated

compiler/rustc_parse/src/parser/path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ impl<'a> Parser<'a> {
599599
// When encountering severely malformed code where there are several levels of
600600
// nested unclosed angle args (`f::<f::<f::<f::<...`), we avoid severe O(n^2)
601601
// behavior by bailing out earlier (#117080).
602-
e.emit();
603-
rustc_errors::FatalError.raise();
602+
e.emit().raise_fatal();
604603
}
605604
Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
606605
self.angle_bracket_nesting -= 1;

compiler/rustc_session/src/output.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::path::Path;
44

55
use rustc_ast::{self as ast, attr};
6-
use rustc_errors::FatalError;
76
use rustc_span::{Span, Symbol, sym};
87

98
use crate::Session;
@@ -90,11 +89,10 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute]) -> Symbol {
9089
}
9190

9291
pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
93-
let mut err_count = 0;
92+
let mut guar = None;
9493
{
9594
if s.is_empty() {
96-
err_count += 1;
97-
sess.dcx().emit_err(CrateNameEmpty { span: sp });
95+
guar = Some(sess.dcx().emit_err(CrateNameEmpty { span: sp }));
9896
}
9997
for c in s.as_str().chars() {
10098
if c.is_alphanumeric() {
@@ -103,8 +101,7 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
103101
if c == '_' {
104102
continue;
105103
}
106-
err_count += 1;
107-
sess.dcx().emit_err(InvalidCharacterInCrateName {
104+
guar = Some(sess.dcx().emit_err(InvalidCharacterInCrateName {
108105
span: sp,
109106
character: c,
110107
crate_name: s,
@@ -113,12 +110,12 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
113110
} else {
114111
None
115112
},
116-
});
113+
}));
117114
}
118115
}
119116

120-
if err_count > 0 {
121-
FatalError.raise();
117+
if let Some(guar) = guar {
118+
guar.raise_fatal();
122119
}
123120
}
124121

compiler/rustc_trait_selection/src/error_reporting/traits/overflow.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::fmt;
22

3-
use rustc_errors::{
4-
Diag, E0275, EmissionGuarantee, ErrorGuaranteed, FatalError, struct_span_code_err,
5-
};
3+
use rustc_errors::{Diag, E0275, EmissionGuarantee, ErrorGuaranteed, struct_span_code_err};
64
use rustc_hir::def::Namespace;
75
use rustc_hir::def_id::LOCAL_CRATE;
86
use rustc_infer::traits::{Obligation, PredicateObligation};
@@ -52,8 +50,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
5250
) -> ! {
5351
let mut err = self.build_overflow_error(cause, span, suggest_increasing_limit);
5452
mutate(&mut err);
55-
err.emit();
56-
FatalError.raise();
53+
err.emit().raise_fatal();
5754
}
5855

5956
pub fn build_overflow_error(

0 commit comments

Comments
 (0)