Rollup merge of #134561 - bjorn3:less_fatal_error_raise, r=compiler-errors

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.
This commit is contained in:
Matthias Krüger 2024-12-20 21:32:30 +01:00 committed by GitHub
commit 4a792fdce1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 22 additions and 41 deletions

View File

@ -15,7 +15,7 @@ use rustc_ast::CRATE_NODE_ID;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{DiagCtxtHandle, FatalError};
use rustc_errors::DiagCtxtHandle;
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
@ -1038,22 +1038,22 @@ fn link_natively(
Err(e) => {
let linker_not_found = e.kind() == io::ErrorKind::NotFound;
if linker_not_found {
sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e });
let err = if linker_not_found {
sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e })
} else {
sess.dcx().emit_err(errors::UnableToExeLinker {
linker_path,
error: e,
command_formatted: format!("{cmd:?}"),
});
}
})
};
if sess.target.is_like_msvc && linker_not_found {
sess.dcx().emit_note(errors::MsvcMissingLinker);
sess.dcx().emit_note(errors::CheckInstalledVisualStudio);
sess.dcx().emit_note(errors::InsufficientVSCodeProduct);
}
FatalError.raise();
err.raise_fatal();
}
}

View File

@ -4,7 +4,6 @@ use std::cell::Cell;
use std::fmt::Write;
use rustc_ast_pretty::pprust as pprust_ast;
use rustc_errors::FatalError;
use rustc_middle::bug;
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt};
@ -311,9 +310,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
let tcx = ex.tcx();
let mut out = String::new();
rustc_hir_analysis::check_crate(tcx);
if tcx.dcx().has_errors().is_some() {
FatalError.raise();
}
tcx.dcx().abort_if_errors();
debug!("pretty printing THIR tree");
for did in tcx.hir().body_owners() {
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>) {
let tcx = ex.tcx();
let mut out = String::new();
rustc_hir_analysis::check_crate(tcx);
if tcx.dcx().has_errors().is_some() {
FatalError.raise();
}
tcx.dcx().abort_if_errors();
debug!("pretty printing THIR flat");
for did in tcx.hir().body_owners() {
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did));

View File

@ -16,8 +16,8 @@ use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, PResult, Subdiagnostic,
Suggestions, pluralize,
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions,
pluralize,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::edit_distance::find_best_match_for_name;
@ -3023,17 +3023,10 @@ impl<'a> Parser<'a> {
}
pub(super) fn recover_vcs_conflict_marker(&mut self) {
if let Err(err) = self.err_vcs_conflict_marker() {
err.emit();
FatalError.raise();
}
}
pub(crate) fn err_vcs_conflict_marker(&mut self) -> PResult<'a, ()> {
// <<<<<<<
let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else {
return Ok(());
return;
};
let mut spans = Vec::with_capacity(3);
spans.push(start);
@ -3063,7 +3056,7 @@ impl<'a> Parser<'a> {
self.bump();
}
let mut err = self.dcx().struct_span_err(spans, "encountered diff marker");
let mut err = self.dcx().struct_span_fatal(spans, "encountered diff marker");
match middlediff3 {
// We're using diff3
Some(middlediff3) => {
@ -3106,7 +3099,7 @@ impl<'a> Parser<'a> {
visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
);
Err(err)
err.emit();
}
/// Parse and throw away a parenthesized comma separated

View File

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

View File

@ -3,7 +3,6 @@
use std::path::Path;
use rustc_ast::{self as ast, attr};
use rustc_errors::FatalError;
use rustc_span::{Span, Symbol, sym};
use crate::Session;
@ -90,11 +89,10 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute]) -> Symbol {
}
pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
let mut err_count = 0;
let mut guar = None;
{
if s.is_empty() {
err_count += 1;
sess.dcx().emit_err(CrateNameEmpty { span: sp });
guar = Some(sess.dcx().emit_err(CrateNameEmpty { span: sp }));
}
for c in s.as_str().chars() {
if c.is_alphanumeric() {
@ -103,8 +101,7 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
if c == '_' {
continue;
}
err_count += 1;
sess.dcx().emit_err(InvalidCharacterInCrateName {
guar = Some(sess.dcx().emit_err(InvalidCharacterInCrateName {
span: sp,
character: c,
crate_name: s,
@ -113,12 +110,12 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
} else {
None
},
});
}));
}
}
if err_count > 0 {
FatalError.raise();
if let Some(guar) = guar {
guar.raise_fatal();
}
}

View File

@ -1,8 +1,6 @@
use std::fmt;
use rustc_errors::{
Diag, E0275, EmissionGuarantee, ErrorGuaranteed, FatalError, struct_span_code_err,
};
use rustc_errors::{Diag, E0275, EmissionGuarantee, ErrorGuaranteed, struct_span_code_err};
use rustc_hir::def::Namespace;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_infer::traits::{Obligation, PredicateObligation};
@ -52,8 +50,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
) -> ! {
let mut err = self.build_overflow_error(cause, span, suggest_increasing_limit);
mutate(&mut err);
err.emit();
FatalError.raise();
err.emit().raise_fatal();
}
pub fn build_overflow_error(