From 955fdaea4a187ab629266ff8fe88a1e3432fb592 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 22:46:50 -0400 Subject: [PATCH 1/5] Rename `Parser::span_fatal_err` -> `Parser::span_err` The name was misleading, it wasn't actually a fatal error. --- compiler/rustc_parse/src/parser/diagnostics.rs | 6 +----- compiler/rustc_parse/src/parser/item.rs | 2 +- compiler/rustc_parse/src/parser/mod.rs | 2 +- compiler/rustc_parse/src/parser/stmt.rs | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 70a5ac6f15ec2..72fdc78c30cbc 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -144,11 +144,7 @@ impl AttemptLocalParseRecovery { } impl<'a> Parser<'a> { - pub(super) fn span_fatal_err>( - &self, - sp: S, - err: Error, - ) -> DiagnosticBuilder<'a> { + pub(super) fn span_err>(&self, sp: S, err: Error) -> DiagnosticBuilder<'a> { err.span_err(sp, self.diagnostic()) } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 20e8b0f6425ce..553ffda814fe9 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1326,7 +1326,7 @@ impl<'a> Parser<'a> { token::CloseDelim(token::Brace) => {} token::DocComment(..) => { let previous_span = self.prev_token.span; - let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment); + let mut err = self.span_err(self.token.span, Error::UselessDocComment); self.bump(); // consume the doc comment let comma_after_doc_seen = self.eat(&token::Comma); // `seen_comma` is always false, because we are inside doc block diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 74481e236f31c..ec11f2d3add92 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -525,7 +525,7 @@ impl<'a> Parser<'a> { fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> { self.token.ident().ok_or_else(|| match self.prev_token.kind { TokenKind::DocComment(..) => { - self.span_fatal_err(self.prev_token.span, Error::UselessDocComment) + self.span_err(self.prev_token.span, Error::UselessDocComment) } _ => self.expected_ident_found(), }) diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 592f64f4a399f..b40eed8c5d118 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -168,7 +168,7 @@ impl<'a> Parser<'a> { fn error_outer_attrs(&self, attrs: &[Attribute]) { if let [.., last] = attrs { if last.is_doc_comment() { - self.span_fatal_err(last.span, Error::UselessDocComment).emit(); + self.span_err(last.span, Error::UselessDocComment).emit(); } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { self.struct_span_err(last.span, "expected statement after outer attribute").emit(); } From e49f4471aaead4317359a21d4913965f176782a6 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 22:48:21 -0400 Subject: [PATCH 2/5] Remove some unnecessary uses of `struct_span_fatal` All of them immediately called `emit()` then `raise()`, so they could just call `span_fatal` directly. --- compiler/rustc_parse/src/lexer/mod.rs | 56 ++++++++++----------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index bd8dfd678a97b..d3ff29a5b1a7b 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -315,57 +315,41 @@ impl<'a> StringReader<'a> { let (lit_kind, mode, prefix_len, postfix_len) = match kind { rustc_lexer::LiteralKind::Char { terminated } => { if !terminated { - self.sess - .span_diagnostic - .struct_span_fatal_with_code( - self.mk_sp(start, suffix_start), - "unterminated character literal", - error_code!(E0762), - ) - .emit(); - FatalError.raise(); + self.sess.span_diagnostic.span_fatal_with_code( + self.mk_sp(start, suffix_start), + "unterminated character literal", + error_code!(E0762), + ) } (token::Char, Mode::Char, 1, 1) // ' ' } rustc_lexer::LiteralKind::Byte { terminated } => { if !terminated { - self.sess - .span_diagnostic - .struct_span_fatal_with_code( - self.mk_sp(start + BytePos(1), suffix_start), - "unterminated byte constant", - error_code!(E0763), - ) - .emit(); - FatalError.raise(); + self.sess.span_diagnostic.span_fatal_with_code( + self.mk_sp(start + BytePos(1), suffix_start), + "unterminated byte constant", + error_code!(E0763), + ) } (token::Byte, Mode::Byte, 2, 1) // b' ' } rustc_lexer::LiteralKind::Str { terminated } => { if !terminated { - self.sess - .span_diagnostic - .struct_span_fatal_with_code( - self.mk_sp(start, suffix_start), - "unterminated double quote string", - error_code!(E0765), - ) - .emit(); - FatalError.raise(); + self.sess.span_diagnostic.span_fatal_with_code( + self.mk_sp(start, suffix_start), + "unterminated double quote string", + error_code!(E0765), + ) } (token::Str, Mode::Str, 1, 1) // " " } rustc_lexer::LiteralKind::ByteStr { terminated } => { if !terminated { - self.sess - .span_diagnostic - .struct_span_fatal_with_code( - self.mk_sp(start + BytePos(1), suffix_start), - "unterminated double quote byte string", - error_code!(E0766), - ) - .emit(); - FatalError.raise(); + self.sess.span_diagnostic.span_fatal_with_code( + self.mk_sp(start + BytePos(1), suffix_start), + "unterminated double quote byte string", + error_code!(E0766), + ) } (token::ByteStr, Mode::ByteStr, 2, 1) // b" " } From 96509b483589e8d9ae98d1c075dccef184c8be30 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 22:45:01 -0400 Subject: [PATCH 3/5] Make `Diagnostic::span_fatal` unconditionally raise an error It had no callers which didn't immediately call `raise()`, and this unifies the behavior with `Session`. --- compiler/rustc_ast_lowering/src/expr.rs | 4 +--- compiler/rustc_errors/src/lib.rs | 8 ++++---- compiler/rustc_parse/src/lexer/mod.rs | 14 +++++--------- compiler/rustc_session/src/cgu_reuse_tracker.rs | 2 +- compiler/rustc_session/src/session.rs | 4 ++-- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index bf70a41fd79e0..ea0770daf0eed 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1236,9 +1236,7 @@ impl<'hir> LoweringContext<'_, 'hir> { (Some(..), Some(..), HalfOpen) => hir::LangItem::Range, (None, Some(..), Closed) => hir::LangItem::RangeToInclusive, (Some(..), Some(..), Closed) => unreachable!(), - (_, None, Closed) => { - self.diagnostic().span_fatal(span, "inclusive range with no end").raise() - } + (_, None, Closed) => self.diagnostic().span_fatal(span, "inclusive range with no end"), }; let fields = self.arena.alloc_from_iter( diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 6aee769298bd8..cbfb5f951b29f 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -634,9 +634,9 @@ impl Handler { DiagnosticBuilder::new(self, Level::Note, msg) } - pub fn span_fatal(&self, span: impl Into, msg: &str) -> FatalError { + pub fn span_fatal(&self, span: impl Into, msg: &str) -> ! { self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span); - FatalError + FatalError.raise() } pub fn span_fatal_with_code( @@ -644,9 +644,9 @@ impl Handler { span: impl Into, msg: &str, code: DiagnosticId, - ) -> FatalError { + ) -> ! { self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span); - FatalError + FatalError.raise() } pub fn span_err(&self, span: impl Into, msg: &str) { diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index d3ff29a5b1a7b..1c2f9a9645fe0 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -148,15 +148,11 @@ impl<'a> StringReader<'a> { None => "unterminated block comment", }; let last_bpos = self.pos; - self.sess - .span_diagnostic - .struct_span_fatal_with_code( - self.mk_sp(start, last_bpos), - msg, - error_code!(E0758), - ) - .emit(); - FatalError.raise(); + self.sess.span_diagnostic.span_fatal_with_code( + self.mk_sp(start, last_bpos), + msg, + error_code!(E0758), + ); } // Skip non-doc comments diff --git a/compiler/rustc_session/src/cgu_reuse_tracker.rs b/compiler/rustc_session/src/cgu_reuse_tracker.rs index 0eec12aa03f2d..a9e14754334c1 100644 --- a/compiler/rustc_session/src/cgu_reuse_tracker.rs +++ b/compiler/rustc_session/src/cgu_reuse_tracker.rs @@ -112,7 +112,7 @@ impl CguReuseTracker { not recorded", cgu_user_name, cgu_name ); - diag.span_fatal(error_span.0, &msg).raise(); + diag.span_fatal(error_span.0, &msg) } } } diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index e7dfc4b8c4128..fff4ee7739f46 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -421,7 +421,7 @@ impl Session { } pub fn span_fatal>(&self, sp: S, msg: &str) -> ! { - self.diagnostic().span_fatal(sp, msg).raise() + self.diagnostic().span_fatal(sp, msg) } pub fn span_fatal_with_code>( &self, @@ -429,7 +429,7 @@ impl Session { msg: &str, code: DiagnosticId, ) -> ! { - self.diagnostic().span_fatal_with_code(sp, msg, code).raise() + self.diagnostic().span_fatal_with_code(sp, msg, code) } pub fn fatal(&self, msg: &str) -> ! { self.diagnostic().fatal(msg).raise() From ebbc94957555350cdc71c4c853de430119f59f9f Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 22:58:25 -0400 Subject: [PATCH 4/5] Note why `Handler::fatal` is different from `Sesssion::fatal` --- compiler/rustc_errors/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index cbfb5f951b29f..dc1664bb2baa8 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -692,6 +692,7 @@ impl Handler { db } + // NOTE: intentionally doesn't raise an error so rustc_codegen_ssa only reports fatal errors in the main thread pub fn fatal(&self, msg: &str) -> FatalError { self.inner.borrow_mut().fatal(msg) } From f25aa5767f0bb1409c7c7b6c202f22d8bf48bbee Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 23:00:34 -0400 Subject: [PATCH 5/5] Remove unused `opt_span_warn` function --- compiler/rustc_session/src/session.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index fff4ee7739f46..acb2a1ae7efa9 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -492,12 +492,6 @@ impl Session { pub fn warn(&self, msg: &str) { self.diagnostic().warn(msg) } - pub fn opt_span_warn>(&self, opt_sp: Option, msg: &str) { - match opt_sp { - Some(sp) => self.span_warn(sp, msg), - None => self.warn(msg), - } - } /// Delay a span_bug() call until abort_if_errors() #[track_caller] pub fn delay_span_bug>(&self, sp: S, msg: &str) {