1
Fork 0

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

@ -60,8 +60,8 @@ use crate::traits::{
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{
codes::*, pluralize, struct_span_code_err, Applicability, DiagCtxt, Diagnostic,
DiagnosticBuilder, DiagnosticStyledString, ErrorGuaranteed, IntoDiagnosticArg,
codes::*, pluralize, struct_span_code_err, Applicability, DiagCtxt, DiagnosticBuilder,
DiagnosticStyledString, ErrorGuaranteed, IntoDiagnosticArg,
};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
@ -155,7 +155,7 @@ impl<'tcx> Deref for TypeErrCtxt<'_, 'tcx> {
pub(super) fn note_and_explain_region<'tcx>(
tcx: TyCtxt<'tcx>,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
prefix: &str,
region: ty::Region<'tcx>,
suffix: &str,
@ -180,7 +180,7 @@ pub(super) fn note_and_explain_region<'tcx>(
fn explain_free_region<'tcx>(
tcx: TyCtxt<'tcx>,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
prefix: &str,
region: ty::Region<'tcx>,
suffix: &str,
@ -262,7 +262,7 @@ fn msg_span_from_named_region<'tcx>(
}
fn emit_msg_span(
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
prefix: &str,
description: String,
span: Option<Span>,
@ -278,7 +278,7 @@ fn emit_msg_span(
}
fn label_msg_span(
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
prefix: &str,
description: String,
span: Option<Span>,
@ -577,7 +577,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
/// Adds a note if the types come from similarly named crates
fn check_and_note_conflicting_crates(&self, err: &mut Diagnostic, terr: TypeError<'tcx>) {
fn check_and_note_conflicting_crates(
&self,
err: &mut DiagnosticBuilder<'_>,
terr: TypeError<'tcx>,
) {
use hir::def_id::CrateNum;
use rustc_hir::definitions::DisambiguatedDefPathData;
use ty::print::Printer;
@ -651,7 +655,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
let report_path_match = |err: &mut Diagnostic, did1: DefId, did2: DefId| {
let report_path_match = |err: &mut DiagnosticBuilder<'_>, did1: DefId, did2: DefId| {
// Only report definitions from different crates. If both definitions
// are from a local module we could have false positives, e.g.
// let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
@ -701,7 +705,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
fn note_error_origin(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
cause: &ObligationCause<'tcx>,
exp_found: Option<ty::error::ExpectedFound<Ty<'tcx>>>,
terr: TypeError<'tcx>,
@ -1535,7 +1539,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
)]
pub fn note_type_err(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
cause: &ObligationCause<'tcx>,
secondary_span: Option<(Span, Cow<'static, str>)>,
mut values: Option<ValuePairs<'tcx>>,
@ -1582,14 +1586,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
types_visitor
}
fn report(&self, err: &mut Diagnostic) {
fn report(&self, err: &mut DiagnosticBuilder<'_>) {
self.add_labels_for_types(err, "expected", &self.expected);
self.add_labels_for_types(err, "found", &self.found);
}
fn add_labels_for_types(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
target: &str,
types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
) {
@ -1803,7 +1807,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|prim: Ty<'tcx>,
shadow: Ty<'tcx>,
defid: DefId,
diagnostic: &mut Diagnostic| {
diagnostic: &mut DiagnosticBuilder<'_>| {
let name = shadow.sort_string(self.tcx);
diagnostic.note(format!(
"{prim} and {name} have similar names, but are actually distinct types"
@ -1823,7 +1827,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let diagnose_adts =
|expected_adt: ty::AdtDef<'tcx>,
found_adt: ty::AdtDef<'tcx>,
diagnostic: &mut Diagnostic| {
diagnostic: &mut DiagnosticBuilder<'_>| {
let found_name = values.found.sort_string(self.tcx);
let expected_name = values.expected.sort_string(self.tcx);

View file

@ -12,7 +12,7 @@ use crate::infer::SubregionOrigin;
use crate::infer::TyCtxt;
use rustc_errors::AddToDiagnostic;
use rustc_errors::{Diagnostic, ErrorGuaranteed};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir::Ty;
use rustc_middle::ty::Region;
@ -142,7 +142,7 @@ pub fn suggest_adding_lifetime_params<'tcx>(
sub: Region<'tcx>,
ty_sup: &'tcx Ty<'_>,
ty_sub: &'tcx Ty<'_>,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
) {
let suggestion = AddLifetimeParamsSuggestion { tcx, sub, ty_sup, ty_sub, add_note: false };
suggestion.add_to_diagnostic(err);

View file

@ -9,7 +9,7 @@ use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::{SubregionOrigin, TypeTrace};
use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{AddToDiagnostic, Applicability, Diagnostic, ErrorGuaranteed, MultiSpan};
use rustc_errors::{AddToDiagnostic, Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_ty, Visitor};
use rustc_hir::{
@ -261,7 +261,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
pub fn suggest_new_region_bound(
tcx: TyCtxt<'_>,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
fn_returns: Vec<&rustc_hir::Ty<'_>>,
lifetime_name: String,
arg: Option<String>,
@ -488,7 +488,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// `'static` obligation. Suggest relaxing that implicit bound.
fn find_impl_on_dyn_trait(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
ty: Ty<'_>,
ctxt: &UnifyReceiverContext<'tcx>,
) -> bool {
@ -521,7 +521,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
fn suggest_constrain_dyn_trait_in_impl(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
found_dids: &FxIndexSet<DefId>,
ident: Ident,
self_ty: &hir::Ty<'_>,

View file

@ -5,7 +5,7 @@ use crate::errors::{
use crate::fluent_generated as fluent;
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
use crate::infer::{self, SubregionOrigin};
use rustc_errors::{AddToDiagnostic, Diagnostic, DiagnosticBuilder};
use rustc_errors::{AddToDiagnostic, DiagnosticBuilder};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::error::TypeError;
@ -15,7 +15,11 @@ use rustc_span::symbol::kw;
use super::ObligationCauseAsDiagArg;
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
pub(super) fn note_region_origin(&self, err: &mut Diagnostic, origin: &SubregionOrigin<'tcx>) {
pub(super) fn note_region_origin(
&self,
err: &mut DiagnosticBuilder<'_>,
origin: &SubregionOrigin<'tcx>,
) {
match *origin {
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
span: trace.cause.span,
@ -290,7 +294,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
trait_item_def_id: DefId,
impl_item_def_id: LocalDefId,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
) {
// FIXME(compiler-errors): Right now this is only being used for region
// predicate mismatches. Ideally, we'd use it for *all* predicate mismatches,

View file

@ -1,6 +1,6 @@
use super::TypeErrCtxt;
use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
use rustc_errors::{pluralize, Diagnostic, MultiSpan};
use rustc_errors::{pluralize, DiagnosticBuilder, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::traits::ObligationCauseCode;
@ -15,7 +15,7 @@ use rustc_span::{def_id::DefId, sym, BytePos, Span, Symbol};
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
pub fn note_and_explain_type_err(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
err: TypeError<'tcx>,
cause: &ObligationCause<'tcx>,
sp: Span,
@ -522,7 +522,7 @@ impl<T> Trait<T> for X {
fn suggest_constraint(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
msg: impl Fn() -> String,
body_owner_def_id: DefId,
proj_ty: &ty::AliasTy<'tcx>,
@ -595,7 +595,7 @@ impl<T> Trait<T> for X {
/// fn that returns the type.
fn expected_projection(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
proj_ty: &ty::AliasTy<'tcx>,
values: ExpectedFound<Ty<'tcx>>,
body_owner_def_id: DefId,
@ -705,7 +705,7 @@ fn foo(&self) -> Self::T { String::new() }
/// a return type. This can occur when dealing with `TryStream` (#71035).
fn suggest_constraining_opaque_associated_type(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
msg: impl Fn() -> String,
proj_ty: &ty::AliasTy<'tcx>,
ty: Ty<'tcx>,
@ -740,7 +740,7 @@ fn foo(&self) -> Self::T { String::new() }
fn point_at_methods_that_satisfy_associated_type(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
assoc_container_id: DefId,
current_method_ident: Option<Symbol>,
proj_ty_item_def_id: DefId,
@ -798,7 +798,7 @@ fn foo(&self) -> Self::T { String::new() }
fn point_at_associated_type(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
body_owner_def_id: DefId,
found: Ty<'tcx>,
) -> bool {
@ -879,7 +879,7 @@ fn foo(&self) -> Self::T { String::new() }
/// type is defined on a supertrait of the one present in the bounds.
fn constrain_generic_bound_associated_type_structured_suggestion(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
trait_ref: &ty::TraitRef<'tcx>,
bounds: hir::GenericBounds<'_>,
assoc: ty::AssocItem,
@ -916,7 +916,7 @@ fn foo(&self) -> Self::T { String::new() }
/// associated type to a given type `ty`.
fn constrain_associated_type_structured_suggestion(
&self,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
span: Span,
assoc: ty::AssocItem,
assoc_args: &[ty::GenericArg<'tcx>],

View file

@ -1,7 +1,7 @@
use hir::def::CtorKind;
use hir::intravisit::{walk_expr, walk_stmt, Visitor};
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{Applicability, Diagnostic};
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_middle::traits::{
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
@ -76,7 +76,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
pub(super) fn suggest_boxing_for_return_impl_trait(
&self,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
return_sp: Span,
arm_spans: impl Iterator<Item = Span>,
) {
@ -100,7 +100,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
cause: &ObligationCause<'tcx>,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
) {
// Heavily inspired by `FnCtxt::suggest_compatible_variants`, with
// some modifications due to that being in typeck and this being in infer.
@ -177,7 +177,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
cause: &ObligationCause<'tcx>,
exp_span: Span,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
) {
debug!(
"suggest_await_on_expect_found: exp_span={:?}, expected_ty={:?}, found_ty={:?}",
@ -258,7 +258,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
cause: &ObligationCause<'tcx>,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
) {
debug!(
"suggest_accessing_field_where_appropriate(cause={:?}, exp_found={:?})",
@ -298,7 +298,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
cause: &ObligationCause<'tcx>,
span: Span,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
) {
debug!("suggest_function_pointers(cause={:?}, exp_found={:?})", cause, exp_found);
let ty::error::ExpectedFound { expected, found } = exp_found;
@ -532,7 +532,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
span: Span,
hir: hir::Node<'_>,
exp_found: &ty::error::ExpectedFound<ty::PolyTraitRef<'tcx>>,
diag: &mut Diagnostic,
diag: &mut DiagnosticBuilder<'_>,
) {
// 0. Extract fn_decl from hir
let hir::Node::Expr(hir::Expr {
@ -818,7 +818,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
blk: &'tcx hir::Block<'tcx>,
expected_ty: Ty<'tcx>,
err: &mut Diagnostic,
err: &mut DiagnosticBuilder<'_>,
) -> bool {
let diag = self.consider_returning_binding_diag(blk, expected_ty);
match diag {