1
Fork 0

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:
bjorn3 2024-12-20 14:08:24 +00:00
parent 8a1f8039a7
commit 701e2f708b
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::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::memmap::Mmap; use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::temp_dir::MaybeTempDir; 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_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file}; use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
@ -1039,22 +1039,22 @@ fn link_natively(
Err(e) => { Err(e) => {
let linker_not_found = e.kind() == io::ErrorKind::NotFound; let linker_not_found = e.kind() == io::ErrorKind::NotFound;
if linker_not_found { let err = if linker_not_found {
sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e }); sess.dcx().emit_err(errors::LinkerNotFound { linker_path, error: e })
} else { } else {
sess.dcx().emit_err(errors::UnableToExeLinker { sess.dcx().emit_err(errors::UnableToExeLinker {
linker_path, linker_path,
error: e, error: e,
command_formatted: format!("{cmd:?}"), command_formatted: format!("{cmd:?}"),
}); })
} };
if sess.target.is_like_msvc && linker_not_found { if sess.target.is_like_msvc && linker_not_found {
sess.dcx().emit_note(errors::MsvcMissingLinker); sess.dcx().emit_note(errors::MsvcMissingLinker);
sess.dcx().emit_note(errors::CheckInstalledVisualStudio); sess.dcx().emit_note(errors::CheckInstalledVisualStudio);
sess.dcx().emit_note(errors::InsufficientVSCodeProduct); 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 std::fmt::Write;
use rustc_ast_pretty::pprust as pprust_ast; use rustc_ast_pretty::pprust as pprust_ast;
use rustc_errors::FatalError;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty}; use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt}; 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 tcx = ex.tcx();
let mut out = String::new(); let mut out = String::new();
rustc_hir_analysis::check_crate(tcx); rustc_hir_analysis::check_crate(tcx);
if tcx.dcx().has_errors().is_some() { tcx.dcx().abort_if_errors();
FatalError.raise();
}
debug!("pretty printing THIR tree"); debug!("pretty printing THIR tree");
for did in tcx.hir().body_owners() { for did in tcx.hir().body_owners() {
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_tree(did)); 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 tcx = ex.tcx();
let mut out = String::new(); let mut out = String::new();
rustc_hir_analysis::check_crate(tcx); rustc_hir_analysis::check_crate(tcx);
if tcx.dcx().has_errors().is_some() { tcx.dcx().abort_if_errors();
FatalError.raise();
}
debug!("pretty printing THIR flat"); debug!("pretty printing THIR flat");
for did in tcx.hir().body_owners() { for did in tcx.hir().body_owners() {
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did)); 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::fx::FxHashSet;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::{ use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, PResult, Subdiagnostic, Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions,
Suggestions, pluralize, pluralize,
}; };
use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::edit_distance::find_best_match_for_name; 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) { 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) let Some(start) = self.conflict_marker(&TokenKind::BinOp(token::Shl), &TokenKind::Lt)
else { else {
return Ok(()); return;
}; };
let mut spans = Vec::with_capacity(3); let mut spans = Vec::with_capacity(3);
spans.push(start); spans.push(start);
@ -3063,7 +3056,7 @@ impl<'a> Parser<'a> {
self.bump(); 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 { match middlediff3 {
// We're using diff3 // We're using diff3
Some(middlediff3) => { 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>", 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 /// 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 // 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) // nested unclosed angle args (`f::<f::<f::<f::<...`), we avoid severe O(n^2)
// behavior by bailing out earlier (#117080). // behavior by bailing out earlier (#117080).
e.emit(); e.emit().raise_fatal();
rustc_errors::FatalError.raise();
} }
Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => { Err(e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
self.angle_bracket_nesting -= 1; self.angle_bracket_nesting -= 1;

View file

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

View file

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