1
Fork 0

Give DiagnosticBuilder a default type.

`IntoDiagnostic` defaults to `ErrorGuaranteed`, because errors are the
most common diagnostic level. It makes sense to do likewise for the
closely-related (and much more widely used) `DiagnosticBuilder` type,
letting us write `DiagnosticBuilder<'a, ErrorGuaranteed>` as just
`DiagnosticBuilder<'a>`. This cuts over 200 lines of code due to many
multi-line things becoming single line things.
This commit is contained in:
Nicholas Nethercote 2023-12-19 15:26:24 +11:00
parent 6257f3bf1f
commit 757d6f6ef8
59 changed files with 250 additions and 454 deletions

View file

@ -3,8 +3,8 @@ use std::borrow::Cow;
use rustc_ast::token::Token;
use rustc_ast::{Path, Visibility};
use rustc_errors::{
AddToDiagnostic, Applicability, DiagCtxt, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
Level, SubdiagnosticMessage,
AddToDiagnostic, Applicability, DiagCtxt, DiagnosticBuilder, IntoDiagnostic, Level,
SubdiagnosticMessage,
};
use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::ExprParenthesesNeeded;
@ -1043,11 +1043,7 @@ pub(crate) struct ExpectedIdentifier {
impl<'a> IntoDiagnostic<'a> for ExpectedIdentifier {
#[track_caller]
fn into_diagnostic(
self,
dcx: &'a DiagCtxt,
level: Level,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a> {
let token_descr = TokenDescription::from_token(&self.token);
let mut diag = DiagnosticBuilder::new(
@ -1107,11 +1103,7 @@ pub(crate) struct ExpectedSemi {
impl<'a> IntoDiagnostic<'a> for ExpectedSemi {
#[track_caller]
fn into_diagnostic(
self,
dcx: &'a DiagCtxt,
level: Level,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a> {
let token_descr = TokenDescription::from_token(&self.token);
let mut diag = DiagnosticBuilder::new(

View file

@ -35,7 +35,7 @@ use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{
pluralize, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
DiagnosticMessage, ErrorGuaranteed, FatalError, MultiSpan, PResult,
DiagnosticMessage, FatalError, MultiSpan, PResult,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
@ -245,7 +245,7 @@ impl<'a> Parser<'a> {
&self,
sp: S,
m: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
) -> DiagnosticBuilder<'a> {
self.dcx().struct_span_err(sp, m)
}
@ -413,7 +413,7 @@ impl<'a> Parser<'a> {
}
}
pub(super) fn expected_ident_found_err(&mut self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
pub(super) fn expected_ident_found_err(&mut self) -> DiagnosticBuilder<'a> {
self.expected_ident_found(false).unwrap_err()
}
@ -958,7 +958,7 @@ impl<'a> Parser<'a> {
pub(super) fn recover_closure_body(
&mut self,
mut err: DiagnosticBuilder<'a, ErrorGuaranteed>,
mut err: DiagnosticBuilder<'a>,
before: token::Token,
prev: token::Token,
token: token::Token,
@ -1189,7 +1189,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, ErrorGuaranteed>,
mut e: DiagnosticBuilder<'a>,
expr: &mut P<Expr>,
) -> PResult<'a, ()> {
if let ExprKind::Binary(binop, _, _) = &expr.kind
@ -1234,10 +1234,7 @@ impl<'a> Parser<'a> {
/// Suggest add the missing `let` before the identifier in stmt
/// `a: Ty = 1` -> `let a: Ty = 1`
pub(super) fn suggest_add_missing_let_for_stmt(
&mut self,
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
) {
pub(super) fn suggest_add_missing_let_for_stmt(&mut self, err: &mut DiagnosticBuilder<'a>) {
if self.token == token::Colon {
let prev_span = self.prev_token.span.shrink_to_lo();
let snapshot = self.create_snapshot_for_diagnostic();
@ -2320,7 +2317,7 @@ impl<'a> Parser<'a> {
}
}
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a> {
let (span, msg) = match (&self.token.kind, self.subparser_name) {
(&token::Eof, Some(origin)) => {
let sp = self.prev_token.span.shrink_to_hi();
@ -2572,7 +2569,7 @@ impl<'a> Parser<'a> {
pub fn recover_const_arg(
&mut self,
start: Span,
mut err: DiagnosticBuilder<'a, ErrorGuaranteed>,
mut err: DiagnosticBuilder<'a>,
) -> PResult<'a, GenericArg> {
let is_op_or_dot = AssocOp::from_token(&self.token)
.and_then(|op| {
@ -2674,7 +2671,7 @@ impl<'a> Parser<'a> {
/// Creates a dummy const argument, and reports that the expression must be enclosed in braces
pub fn dummy_const_arg_needs_braces(
&self,
mut err: DiagnosticBuilder<'a, ErrorGuaranteed>,
mut err: DiagnosticBuilder<'a>,
span: Span,
) -> GenericArg {
err.multipart_suggestion(

View file

@ -26,8 +26,7 @@ use rustc_ast::{ClosureBinder, MetaItemLit, StmtKind};
use rustc_ast_pretty::pprust;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, PResult,
StashKey,
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, PResult, StashKey,
};
use rustc_macros::Subdiagnostic;
use rustc_session::errors::{report_lit_error, ExprParenthesesNeeded};
@ -1691,7 +1690,7 @@ impl<'a> Parser<'a> {
&self,
lifetime: Ident,
mk_lit_char: impl FnOnce(Symbol, Span) -> L,
err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>,
err: impl FnOnce(&Self) -> DiagnosticBuilder<'a>,
) -> L {
if let Some(mut diag) = self.dcx().steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar)
{

View file

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

View file

@ -18,7 +18,7 @@ use rustc_ast::{
PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
};
use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident};
@ -687,7 +687,7 @@ impl<'a> Parser<'a> {
fn fatal_unexpected_non_pat(
&mut self,
err: DiagnosticBuilder<'a, ErrorGuaranteed>,
err: DiagnosticBuilder<'a>,
expected: Option<Expected>,
) -> PResult<'a, P<Pat>> {
err.cancel();
@ -969,7 +969,7 @@ impl<'a> Parser<'a> {
let mut fields = ThinVec::new();
let mut etc = false;
let mut ate_comma = true;
let mut delayed_err: Option<DiagnosticBuilder<'a, ErrorGuaranteed>> = None;
let mut delayed_err: Option<DiagnosticBuilder<'a>> = None;
let mut first_etc_and_maybe_comma_span = None;
let mut last_non_comma_dotdot_span = None;
@ -1135,7 +1135,7 @@ impl<'a> Parser<'a> {
fn recover_misplaced_pattern_modifiers(
&self,
fields: &ThinVec<PatField>,
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
err: &mut DiagnosticBuilder<'a>,
) {
if let Some(last) = fields.iter().last()
&& last.is_shorthand

View file

@ -19,7 +19,7 @@ use rustc_ast::util::classify;
use rustc_ast::{AttrStyle, AttrVec, LocalKind, MacCall, MacCallStmt, MacStmtStyle};
use rustc_ast::{Block, BlockCheckMode, Expr, ExprKind, HasAttrs, Local, Stmt};
use rustc_ast::{StmtKind, DUMMY_NODE_ID};
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{BytePos, Span};
@ -442,7 +442,7 @@ impl<'a> Parser<'a> {
fn error_block_no_opening_brace_msg(
&mut self,
msg: Cow<'static, str>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
) -> DiagnosticBuilder<'a> {
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;