rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission".

This commit is contained in:
Eduard-Mihai Burtescu 2022-01-27 09:44:25 +00:00
parent 0b9d70cf6d
commit b7e95dee65
83 changed files with 842 additions and 471 deletions

View file

@ -3,7 +3,9 @@ use rustc_ast::ast::{self, AttrStyle};
use rustc_ast::token::{self, CommentKind, Token, TokenKind};
use rustc_ast::tokenstream::{Spacing, TokenStream};
use rustc_ast::util::unicode::contains_text_flow_control_chars;
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, FatalError, PResult};
use rustc_errors::{
error_code, Applicability, DiagnosticBuilder, ErrorReported, FatalError, PResult,
};
use rustc_lexer::unescape::{self, Mode};
use rustc_lexer::{Base, DocStyle, RawStrError};
use rustc_session::lint::builtin::{
@ -127,7 +129,7 @@ impl<'a> StringReader<'a> {
to_pos: BytePos,
m: &str,
c: char,
) -> DiagnosticBuilder<'a> {
) -> DiagnosticBuilder<'a, ErrorReported> {
self.sess
.span_diagnostic
.struct_span_fatal(self.mk_sp(from_pos, to_pos), &format!("{}: {}", m, escaped_char(c)))

View file

@ -144,7 +144,7 @@ pub(crate) fn emit_unescape_error(
c.escape_default().to_string(),
Applicability::MachineApplicable,
)
.emit()
.emit();
}
EscapeError::BareCarriageReturn => {
let msg = if mode.in_double_quotes() {
@ -292,16 +292,18 @@ pub(crate) fn emit_unescape_error(
.span_label(span, "must have at most 6 hex digits")
.emit();
}
EscapeError::UnclosedUnicodeEscape => handler
.struct_span_err(span, "unterminated unicode escape")
.span_label(span, "missing a closing `}`")
.span_suggestion_verbose(
span.shrink_to_hi(),
"terminate the unicode escape",
"}".to_string(),
Applicability::MaybeIncorrect,
)
.emit(),
EscapeError::UnclosedUnicodeEscape => {
handler
.struct_span_err(span, "unterminated unicode escape")
.span_label(span, "missing a closing `}`")
.span_suggestion_verbose(
span.shrink_to_hi(),
"terminate the unicode escape",
"}".to_string(),
Applicability::MaybeIncorrect,
)
.emit();
}
EscapeError::NoBraceInUnicodeEscape => {
let msg = "incorrect unicode escape sequence";
let mut diag = handler.struct_span_err(span, msg);
@ -347,7 +349,7 @@ pub(crate) fn emit_unescape_error(
}
EscapeError::ZeroChars => {
let msg = "empty character literal";
handler.struct_span_err(span, msg).span_label(span, msg).emit()
handler.struct_span_err(span, msg).span_label(span, msg).emit();
}
EscapeError::LoneSlash => {
let msg = "invalid trailing slash in literal";

View file

@ -16,7 +16,7 @@ use rustc_ast::{
};
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{pluralize, struct_span_err, Diagnostic};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorReported};
use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, Ident};
@ -53,7 +53,11 @@ pub enum Error {
}
impl Error {
fn span_err(self, sp: impl Into<MultiSpan>, handler: &Handler) -> DiagnosticBuilder<'_> {
fn span_err(
self,
sp: impl Into<MultiSpan>,
handler: &Handler,
) -> DiagnosticBuilder<'_, ErrorReported> {
match self {
Error::UselessDocComment => {
let mut err = struct_span_err!(
@ -151,11 +155,19 @@ impl AttemptLocalParseRecovery {
}
impl<'a> Parser<'a> {
pub(super) fn span_err<S: Into<MultiSpan>>(&self, sp: S, err: Error) -> DiagnosticBuilder<'a> {
pub(super) fn span_err<S: Into<MultiSpan>>(
&self,
sp: S,
err: Error,
) -> DiagnosticBuilder<'a, ErrorReported> {
err.span_err(sp, self.diagnostic())
}
pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> {
pub fn struct_span_err<S: Into<MultiSpan>>(
&self,
sp: S,
m: &str,
) -> DiagnosticBuilder<'a, ErrorReported> {
self.sess.span_diagnostic.struct_span_err(sp, m)
}
@ -171,7 +183,7 @@ impl<'a> Parser<'a> {
self.sess.source_map().span_to_snippet(span)
}
pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a> {
pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a, ErrorReported> {
let mut err = self.struct_span_err(
self.token.span,
&format!("expected identifier, found {}", super::token_descr(&self.token)),
@ -717,7 +729,7 @@ impl<'a> Parser<'a> {
/// encounter a parse error when encountering the first `,`.
pub(super) fn check_mistyped_turbofish_with_multiple_type_params(
&mut self,
mut e: DiagnosticBuilder<'a>,
mut e: DiagnosticBuilder<'a, ErrorReported>,
expr: &mut P<Expr>,
) -> PResult<'a, ()> {
if let ExprKind::Binary(binop, _, _) = &expr.kind {
@ -1439,7 +1451,7 @@ impl<'a> Parser<'a> {
pub(super) fn recover_closing_delimiter(
&mut self,
tokens: &[TokenKind],
mut err: DiagnosticBuilder<'a>,
mut err: DiagnosticBuilder<'a, ErrorReported>,
) -> PResult<'a, bool> {
let mut pos = None;
// We want to use the last closing delim that would apply.
@ -1810,7 +1822,7 @@ impl<'a> Parser<'a> {
}
}
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a> {
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a, ErrorReported> {
let (span, msg) = match (&self.token.kind, self.subparser_name) {
(&token::Eof, Some(origin)) => {
let sp = self.sess.source_map().next_point(self.prev_token.span);
@ -2016,7 +2028,7 @@ impl<'a> Parser<'a> {
pub fn recover_const_arg(
&mut self,
start: Span,
mut err: DiagnosticBuilder<'a>,
mut err: DiagnosticBuilder<'a, ErrorReported>,
) -> PResult<'a, GenericArg> {
let is_op = AssocOp::from_token(&self.token)
.and_then(|op| {
@ -2096,7 +2108,7 @@ impl<'a> Parser<'a> {
pub(super) fn incorrect_move_async_order_found(
&self,
move_async_span: Span,
) -> DiagnosticBuilder<'a> {
) -> DiagnosticBuilder<'a, ErrorReported> {
let mut err =
self.struct_span_err(move_async_span, "the order of `move` and `async` is incorrect");
err.span_suggestion_verbose(

View file

@ -17,7 +17,7 @@ use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp
use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, PResult};
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported, PResult};
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::edition::LATEST_STABLE_EDITION;
@ -1167,7 +1167,9 @@ impl<'a> Parser<'a> {
return Some(self.mk_expr_err(span));
}
Ok(_) => {}
Err(mut err) => err.emit(),
Err(mut err) => {
err.emit();
}
}
}
_ => {}
@ -1819,6 +1821,7 @@ impl<'a> Parser<'a> {
err
} else {
self.struct_span_err(sp, &format!("suffixes on {} are invalid", kind))
.forget_guarantee()
};
err.span_label(sp, format!("invalid suffix `{}`", suf));
err.emit();
@ -2100,9 +2103,9 @@ impl<'a> Parser<'a> {
fn error_missing_if_then_block(
&self,
if_span: Span,
err: Option<DiagnosticBuilder<'a>>,
err: Option<DiagnosticBuilder<'a, ErrorReported>>,
binop_span: Option<Span>,
) -> DiagnosticBuilder<'a> {
) -> DiagnosticBuilder<'a, ErrorReported> {
let msg = "this `if` expression has a condition, but no block";
let mut err = if let Some(mut err) = err {

View file

@ -13,7 +13,7 @@ use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, Vari
use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
use rustc_ast::{MacArgs, MacCall, MacDelimiter};
use rustc_ast_pretty::pprust;
use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
use rustc_errors::{struct_span_err, Applicability, ErrorReported, PResult, StashKey};
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
use rustc_span::lev_distance::lev_distance;
use rustc_span::source_map::{self, Span};
@ -801,7 +801,7 @@ impl<'a> Parser<'a> {
before_where_clause_span: Span,
after_predicates: &[WherePredicate],
after_where_clause_span: Span,
) {
) -> ErrorReported {
let mut err =
self.struct_span_err(after_where_clause_span, "where clause not allowed here");
if !after_predicates.is_empty() {

View file

@ -32,7 +32,7 @@ use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;
use rustc_errors::PResult;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, FatalError};
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported, FatalError};
use rustc_session::parse::ParseSess;
use rustc_span::source_map::{MultiSpan, Span, DUMMY_SP};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -877,7 +877,7 @@ impl<'a> Parser<'a> {
fn recover_missing_braces_around_closure_body(
&mut self,
closure_spans: ClosureSpans,
mut expect_err: DiagnosticBuilder<'_>,
mut expect_err: DiagnosticBuilder<'_, ErrorReported>,
) -> PResult<'a, ()> {
let initial_semicolon = self.token.span;
@ -1429,7 +1429,7 @@ impl<'a> Parser<'a> {
crate fn make_unclosed_delims_error(
unmatched: UnmatchedBrace,
sess: &ParseSess,
) -> Option<DiagnosticBuilder<'_>> {
) -> Option<DiagnosticBuilder<'_, ErrorReported>> {
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
// `unmatched_braces` only for error recovery in the `Parser`.
let found_delim = unmatched.found_delim?;

View file

@ -8,7 +8,7 @@ use rustc_ast::{
PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
};
use rustc_ast_pretty::pprust;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult};
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported, PResult};
use rustc_span::source_map::{respan, Span, Spanned};
use rustc_span::symbol::{kw, sym, Ident};
@ -655,7 +655,7 @@ impl<'a> Parser<'a> {
fn fatal_unexpected_non_pat(
&mut self,
err: DiagnosticBuilder<'a>,
err: DiagnosticBuilder<'a, ErrorReported>,
expected: Expected,
) -> PResult<'a, P<Pat>> {
err.cancel();
@ -886,7 +886,7 @@ impl<'a> Parser<'a> {
let mut fields = Vec::new();
let mut etc = false;
let mut ate_comma = true;
let mut delayed_err: Option<DiagnosticBuilder<'a>> = None;
let mut delayed_err: Option<DiagnosticBuilder<'a, ErrorReported>> = None;
let mut etc_span = None;
while self.token != token::CloseDelim(token::Brace) {

View file

@ -18,7 +18,7 @@ use rustc_ast::{
};
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt};
use rustc_ast::{StmtKind, DUMMY_NODE_ID};
use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorReported, PResult};
use rustc_span::source_map::{BytePos, Span};
use rustc_span::symbol::{kw, sym};
@ -414,7 +414,10 @@ impl<'a> Parser<'a> {
Ok(block)
}
fn error_block_no_opening_brace_msg(&mut self, msg: &str) -> DiagnosticBuilder<'a> {
fn error_block_no_opening_brace_msg(
&mut self,
msg: &str,
) -> DiagnosticBuilder<'a, ErrorReported> {
let sp = self.token.span;
let mut e = self.struct_span_err(sp, msg);
let do_not_suggest_help = self.token.is_keyword(kw::In) || self.token == token::Colon;

View file

@ -345,7 +345,8 @@ impl<'a> Parser<'a> {
let lt_no_plus = self.check_lifetime() && !self.look_ahead(1, |t| t.is_like_plus());
let bounds = self.parse_generic_bounds_common(allow_plus, None)?;
if lt_no_plus {
self.struct_span_err(lo, "lifetime in trait object type must be followed by `+`").emit()
self.struct_span_err(lo, "lifetime in trait object type must be followed by `+`")
.emit();
}
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
}