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:
parent
8b21296b5d
commit
b18f3e11fa
66 changed files with 536 additions and 454 deletions
|
@ -7,7 +7,7 @@ use rustc_ast_pretty::pprust;
|
|||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::{
|
||||
codes::*, pluralize, report_ambiguity_error, struct_span_code_err, Applicability, DiagCtxt,
|
||||
Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, SuggestionStyle,
|
||||
DiagnosticBuilder, ErrorGuaranteed, MultiSpan, SuggestionStyle,
|
||||
};
|
||||
use rustc_feature::BUILTIN_ATTRIBUTES;
|
||||
use rustc_hir::def::Namespace::{self, *};
|
||||
|
@ -360,7 +360,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
/// ```
|
||||
fn add_suggestion_for_rename_of_use(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
name: Symbol,
|
||||
import: Import<'_>,
|
||||
binding_span: Span,
|
||||
|
@ -436,7 +436,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
/// as characters expected by span manipulations won't be present.
|
||||
fn add_suggestion_for_duplicate_nested_use(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
import: Import<'_>,
|
||||
binding_span: Span,
|
||||
) {
|
||||
|
@ -1399,7 +1399,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
|
||||
pub(crate) fn unresolved_macro_suggestions(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
macro_kind: MacroKind,
|
||||
parent_scope: &ParentScope<'a>,
|
||||
ident: Ident,
|
||||
|
@ -1515,7 +1515,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
|
||||
pub(crate) fn add_typo_suggestion(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
suggestion: Option<TypoSuggestion>,
|
||||
span: Span,
|
||||
) -> bool {
|
||||
|
@ -2461,7 +2461,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
/// Finds a cfg-ed out item inside `module` with the matching name.
|
||||
pub(crate) fn find_cfg_stripped(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
segment: &Symbol,
|
||||
module: DefId,
|
||||
) {
|
||||
|
@ -2670,7 +2670,7 @@ pub(crate) enum DiagnosticMode {
|
|||
|
||||
pub(crate) fn import_candidates(
|
||||
tcx: TyCtxt<'_>,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
// This is `None` if all placement locations are inside expansions
|
||||
use_placement_span: Option<Span>,
|
||||
candidates: &[ImportSuggestion],
|
||||
|
@ -2696,7 +2696,7 @@ pub(crate) fn import_candidates(
|
|||
/// found and suggested, returns `true`, otherwise returns `false`.
|
||||
fn show_candidates(
|
||||
tcx: TyCtxt<'_>,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
// This is `None` if all placement locations are inside expansions
|
||||
use_placement_span: Option<Span>,
|
||||
candidates: &[ImportSuggestion],
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// ignore-tidy-filelength
|
||||
|
||||
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
|
||||
use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind};
|
||||
use crate::late::{LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseSet};
|
||||
|
@ -16,8 +18,8 @@ use rustc_ast::{
|
|||
use rustc_ast_pretty::pprust::where_bound_predicate_to_string;
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::{
|
||||
codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
|
||||
ErrorGuaranteed, MultiSpan, SuggestionStyle,
|
||||
codes::*, pluralize, struct_span_code_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
|
||||
MultiSpan, SuggestionStyle,
|
||||
};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
|
||||
|
@ -496,7 +498,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn detect_assoc_type_constraint_meant_as_path(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
base_error: &BaseError,
|
||||
) {
|
||||
let Some(ty) = self.diagnostic_metadata.current_type_path else {
|
||||
|
@ -537,7 +539,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn suggest_self_or_self_ref(&mut self, err: &mut Diagnostic, path: &[Segment], span: Span) {
|
||||
fn suggest_self_or_self_ref(
|
||||
&mut self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
) {
|
||||
if !self.self_type_is_available() {
|
||||
return;
|
||||
}
|
||||
|
@ -582,7 +589,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn try_lookup_name_relaxed(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
following_seg: Option<&Segment>,
|
||||
|
@ -786,7 +793,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_trait_and_bounds(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
res: Option<Res>,
|
||||
span: Span,
|
||||
|
@ -863,7 +870,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_typo(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
following_seg: Option<&Segment>,
|
||||
|
@ -903,7 +910,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_shadowed(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
following_seg: Option<&Segment>,
|
||||
|
@ -936,7 +943,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn err_code_special_cases(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
|
@ -981,7 +988,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
/// Emit special messages for unresolved `Self` and `self`.
|
||||
fn suggest_self_ty(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
|
@ -1008,7 +1015,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_self_value(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
|
@ -1090,7 +1097,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_at_operator_in_slice_pat_with_range(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
path: &[Segment],
|
||||
) {
|
||||
let Some(pat) = self.diagnostic_metadata.current_pat else { return };
|
||||
|
@ -1129,7 +1136,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_swapping_misplaced_self_ty_and_trait(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
res: Option<Res>,
|
||||
span: Span,
|
||||
|
@ -1155,7 +1162,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn suggest_bare_struct_literal(&mut self, err: &mut Diagnostic) {
|
||||
fn suggest_bare_struct_literal(&mut self, err: &mut DiagnosticBuilder<'_>) {
|
||||
if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
|
||||
err.multipart_suggestion(
|
||||
"you might have meant to write a `struct` literal",
|
||||
|
@ -1170,7 +1177,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_changing_type_to_const_param(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
res: Option<Res>,
|
||||
source: PathSource<'_>,
|
||||
span: Span,
|
||||
|
@ -1222,7 +1229,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_pattern_match_with_let(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
span: Span,
|
||||
) -> bool {
|
||||
|
@ -1277,7 +1284,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
}
|
||||
|
||||
/// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
|
||||
fn restrict_assoc_type_in_where_clause(&mut self, span: Span, err: &mut Diagnostic) -> bool {
|
||||
fn restrict_assoc_type_in_where_clause(
|
||||
&mut self,
|
||||
span: Span,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
) -> bool {
|
||||
// Detect that we are actually in a `where` predicate.
|
||||
let (bounded_ty, bounds, where_span) =
|
||||
if let Some(ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
|
||||
|
@ -1410,7 +1421,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
/// Returns `true` if able to provide context-dependent help.
|
||||
fn smart_resolve_context_dependent_help(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
span: Span,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
|
@ -1421,50 +1432,52 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
let ns = source.namespace();
|
||||
let is_expected = &|res| source.is_expected(res);
|
||||
|
||||
let path_sep = |this: &mut Self, err: &mut Diagnostic, expr: &Expr, kind: DefKind| {
|
||||
const MESSAGE: &str = "use the path separator to refer to an item";
|
||||
let path_sep =
|
||||
|this: &mut Self, err: &mut DiagnosticBuilder<'_>, expr: &Expr, kind: DefKind| {
|
||||
const MESSAGE: &str = "use the path separator to refer to an item";
|
||||
|
||||
let (lhs_span, rhs_span) = match &expr.kind {
|
||||
ExprKind::Field(base, ident) => (base.span, ident.span),
|
||||
ExprKind::MethodCall(box MethodCall { receiver, span, .. }) => {
|
||||
(receiver.span, *span)
|
||||
let (lhs_span, rhs_span) = match &expr.kind {
|
||||
ExprKind::Field(base, ident) => (base.span, ident.span),
|
||||
ExprKind::MethodCall(box MethodCall { receiver, span, .. }) => {
|
||||
(receiver.span, *span)
|
||||
}
|
||||
_ => return false,
|
||||
};
|
||||
|
||||
if lhs_span.eq_ctxt(rhs_span) {
|
||||
err.span_suggestion(
|
||||
lhs_span.between(rhs_span),
|
||||
MESSAGE,
|
||||
"::",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
true
|
||||
} else if kind == DefKind::Struct
|
||||
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
|
||||
&& let Ok(snippet) =
|
||||
this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
|
||||
{
|
||||
// The LHS is a type that originates from a macro call.
|
||||
// We have to add angle brackets around it.
|
||||
|
||||
err.span_suggestion_verbose(
|
||||
lhs_source_span.until(rhs_span),
|
||||
MESSAGE,
|
||||
format!("<{snippet}>::"),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
true
|
||||
} else {
|
||||
// Either we were unable to obtain the source span / the snippet or
|
||||
// the LHS originates from a macro call and it is not a type and thus
|
||||
// there is no way to replace `.` with `::` and still somehow suggest
|
||||
// valid Rust code.
|
||||
|
||||
false
|
||||
}
|
||||
_ => return false,
|
||||
};
|
||||
|
||||
if lhs_span.eq_ctxt(rhs_span) {
|
||||
err.span_suggestion(
|
||||
lhs_span.between(rhs_span),
|
||||
MESSAGE,
|
||||
"::",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
true
|
||||
} else if kind == DefKind::Struct
|
||||
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
|
||||
&& let Ok(snippet) = this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
|
||||
{
|
||||
// The LHS is a type that originates from a macro call.
|
||||
// We have to add angle brackets around it.
|
||||
|
||||
err.span_suggestion_verbose(
|
||||
lhs_source_span.until(rhs_span),
|
||||
MESSAGE,
|
||||
format!("<{snippet}>::"),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
true
|
||||
} else {
|
||||
// Either we were unable to obtain the source span / the snippet or
|
||||
// the LHS originates from a macro call and it is not a type and thus
|
||||
// there is no way to replace `.` with `::` and still somehow suggest
|
||||
// valid Rust code.
|
||||
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
let find_span = |source: &PathSource<'_>, err: &mut Diagnostic| {
|
||||
let find_span = |source: &PathSource<'_>, err: &mut DiagnosticBuilder<'_>| {
|
||||
match source {
|
||||
PathSource::Expr(Some(Expr { span, kind: ExprKind::Call(_, _), .. }))
|
||||
| PathSource::TupleStruct(span, _) => {
|
||||
|
@ -1820,7 +1833,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
fn suggest_alternative_construction_methods(
|
||||
&mut self,
|
||||
def_id: DefId,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
path_span: Span,
|
||||
call_span: Span,
|
||||
args: &[P<Expr>],
|
||||
|
@ -2250,7 +2263,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
// try to give a suggestion for this pattern: `name = blah`, which is common in other languages
|
||||
// suggest `let name = blah` to introduce a new binding
|
||||
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
|
||||
fn let_binding_suggestion(
|
||||
&mut self,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
ident_span: Span,
|
||||
) -> bool {
|
||||
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
|
||||
self.diagnostic_metadata.in_assignment
|
||||
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
|
||||
|
@ -2351,7 +2368,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
/// Adds a suggestion for using an enum's variant when an enum is used instead.
|
||||
fn suggest_using_enum_variant(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
source: PathSource<'_>,
|
||||
def_id: DefId,
|
||||
span: Span,
|
||||
|
@ -2727,9 +2744,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn suggest_introducing_lifetime(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
name: Option<&str>,
|
||||
suggest: impl Fn(&mut Diagnostic, bool, Span, Cow<'static, str>, String) -> bool,
|
||||
suggest: impl Fn(&mut DiagnosticBuilder<'_>, bool, Span, Cow<'static, str>, String) -> bool,
|
||||
) {
|
||||
let mut suggest_note = true;
|
||||
for rib in self.lifetime_ribs.iter().rev() {
|
||||
|
@ -2887,7 +2904,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||
|
||||
fn add_missing_lifetime_specifiers_label(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
lifetime_refs: Vec<MissingLifetime>,
|
||||
function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
|
||||
) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue