Prefer DiagnosticBuilder over Diagnostic in diagnostic modifiers.

There are lots of functions that modify a diagnostic. This can be via a
`&mut Diagnostic` or a `&mut DiagnosticBuilder`, because the latter type
wraps the former and impls `DerefMut`.

This commit converts all the `&mut Diagnostic` occurrences to `&mut
DiagnosticBuilder`. This is a step towards greatly simplifying
`Diagnostic`. Some of the relevant function are made generic, because
they deal with both errors and warnings. No function bodies are changed,
because all the modifier methods are available on both `Diagnostic` and
`DiagnosticBuilder`.
This commit is contained in:
Nicholas Nethercote 2024-02-01 10:13:24 +11:00
parent 8b21296b5d
commit b18f3e11fa
66 changed files with 536 additions and 454 deletions

View file

@ -8,7 +8,7 @@ use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle
use rustc_ast as ast;
use rustc_ast::attr;
use rustc_ast::token::{self, Delimiter, Nonterminal};
use rustc_errors::{codes::*, Diagnostic, PResult};
use rustc_errors::{codes::*, DiagnosticBuilder, PResult};
use rustc_span::{sym, BytePos, Span};
use thin_vec::ThinVec;
use tracing::debug;
@ -141,7 +141,7 @@ impl<'a> Parser<'a> {
fn annotate_following_item_if_applicable(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
span: Span,
attr_type: OuterAttributeType,
) -> Option<Span> {

View file

@ -34,8 +34,8 @@ use rustc_ast::{
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{
pluralize, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
ErrorGuaranteed, FatalError, PErr, PResult,
pluralize, AddToDiagnostic, Applicability, DiagCtxt, DiagnosticBuilder, ErrorGuaranteed,
FatalError, PErr, PResult,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
@ -208,11 +208,11 @@ struct MultiSugg {
}
impl MultiSugg {
fn emit(self, err: &mut Diagnostic) {
fn emit(self, err: &mut DiagnosticBuilder<'_>) {
err.multipart_suggestion(self.msg, self.patches, self.applicability);
}
fn emit_verbose(self, err: &mut Diagnostic) {
fn emit_verbose(self, err: &mut DiagnosticBuilder<'_>) {
err.multipart_suggestion_verbose(self.msg, self.patches, self.applicability);
}
}
@ -846,7 +846,7 @@ impl<'a> Parser<'a> {
err.emit();
}
fn check_too_many_raw_str_terminators(&mut self, err: &mut Diagnostic) -> bool {
fn check_too_many_raw_str_terminators(&mut self, err: &mut DiagnosticBuilder<'_>) -> bool {
let sm = self.sess.source_map();
match (&self.prev_token.kind, &self.token.kind) {
(
@ -2179,7 +2179,7 @@ impl<'a> Parser<'a> {
pub(super) fn parameter_without_type(
&mut self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
pat: P<ast::Pat>,
require_name: bool,
first_param: bool,

View file

@ -25,9 +25,7 @@ use rustc_ast::{Arm, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLim
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, PResult, StashKey,
};
use rustc_errors::{AddToDiagnostic, Applicability, DiagnosticBuilder, PResult, StashKey};
use rustc_lexer::unescape::unescape_char;
use rustc_macros::Subdiagnostic;
use rustc_session::errors::{report_lit_error, ExprParenthesesNeeded};
@ -865,7 +863,7 @@ impl<'a> Parser<'a> {
);
let mut err = self.dcx().struct_span_err(span, msg);
let suggest_parens = |err: &mut Diagnostic| {
let suggest_parens = |err: &mut DiagnosticBuilder<'_>| {
let suggestions = vec![
(span.shrink_to_lo(), "(".to_string()),
(span.shrink_to_hi(), ")".to_string()),
@ -3437,7 +3435,7 @@ impl<'a> Parser<'a> {
let mut recover_async = false;
let in_if_guard = self.restrictions.contains(Restrictions::IN_IF_GUARD);
let mut async_block_err = |e: &mut Diagnostic, span: Span| {
let mut async_block_err = |e: &mut DiagnosticBuilder<'_>, span: Span| {
recover_async = true;
errors::AsyncBlockIn2015 { span }.add_to_diagnostic(e);
errors::HelpUseLatestEdition::new().add_to_diagnostic(e);