1
Fork 0

Use .into_diagnostic() less.

This commit replaces this pattern:
```
err.into_diagnostic(dcx)
```
with this pattern:
```
dcx.create_err(err)
```
in a lot of places.

It's a little shorter, makes the error level explicit, avoids some
`IntoDiagnostic` imports, and is a necessary prerequisite for the next
commit which will add a `level` arg to `into_diagnostic`.

This requires adding `track_caller` on `create_err` to avoid mucking up
the output of `tests/ui/track-diagnostics/track4.rs`. It probably should
have been there already.
This commit is contained in:
Nicholas Nethercote 2023-12-18 14:00:17 +11:00
parent cda4736f1e
commit cea683c08f
13 changed files with 109 additions and 130 deletions

View file

@ -5,7 +5,6 @@ use crate::errors::{
use crate::infer::error_reporting::TypeErrCtxt;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::InferCtxt;
use rustc_errors::IntoDiagnostic;
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_hir as hir;
use rustc_hir::def::Res;
@ -367,7 +366,7 @@ impl<'tcx> InferCtxt<'tcx> {
let multi_suggestions = Vec::new();
let bad_label = Some(arg_data.make_bad_error(span));
match error_code {
TypeAnnotationNeeded::E0282 => AnnotationRequired {
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
span,
source_kind,
source_name,
@ -375,9 +374,8 @@ impl<'tcx> InferCtxt<'tcx> {
infer_subdiags,
multi_suggestions,
bad_label,
}
.into_diagnostic(self.tcx.sess.dcx()),
TypeAnnotationNeeded::E0283 => AmbiguousImpl {
}),
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
span,
source_kind,
source_name,
@ -385,9 +383,8 @@ impl<'tcx> InferCtxt<'tcx> {
infer_subdiags,
multi_suggestions,
bad_label,
}
.into_diagnostic(self.tcx.sess.dcx()),
TypeAnnotationNeeded::E0284 => AmbiguousReturn {
}),
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
span,
source_kind,
source_name,
@ -395,8 +392,7 @@ impl<'tcx> InferCtxt<'tcx> {
infer_subdiags,
multi_suggestions,
bad_label,
}
.into_diagnostic(self.tcx.sess.dcx()),
}),
}
}
}
@ -574,7 +570,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
match error_code {
TypeAnnotationNeeded::E0282 => AnnotationRequired {
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
span,
source_kind,
source_name: &name,
@ -582,9 +578,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
infer_subdiags,
multi_suggestions,
bad_label: None,
}
.into_diagnostic(self.tcx.sess.dcx()),
TypeAnnotationNeeded::E0283 => AmbiguousImpl {
}),
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
span,
source_kind,
source_name: &name,
@ -592,9 +587,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
infer_subdiags,
multi_suggestions,
bad_label: None,
}
.into_diagnostic(self.tcx.sess.dcx()),
TypeAnnotationNeeded::E0284 => AmbiguousReturn {
}),
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
span,
source_kind,
source_name: &name,
@ -602,8 +596,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
infer_subdiags,
multi_suggestions,
bad_label: None,
}
.into_diagnostic(self.tcx.sess.dcx()),
}),
}
}
}

View file

@ -5,9 +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, ErrorGuaranteed, IntoDiagnostic,
};
use rustc_errors::{AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::error::TypeError;
@ -136,11 +134,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::ContentValidFor,
note_and_explain::SuffixKind::Empty,
);
OutlivesContent {
self.tcx.sess.dcx().create_err(OutlivesContent {
span,
notes: reference_valid.into_iter().chain(content_valid).collect(),
}
.into_diagnostic(self.tcx.sess.dcx())
})
}
infer::RelateObjectBound(span) => {
let object_valid = note_and_explain::RegionExplanation::new(
@ -157,11 +154,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::SourcePointerValidFor,
note_and_explain::SuffixKind::Empty,
);
OutlivesBound {
self.tcx.sess.dcx().create_err(OutlivesBound {
span,
notes: object_valid.into_iter().chain(pointer_valid).collect(),
}
.into_diagnostic(self.tcx.sess.dcx())
})
}
infer::RelateParamBound(span, ty, opt_span) => {
let prefix = match *sub {
@ -176,8 +172,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let note = note_and_explain::RegionExplanation::new(
self.tcx, sub, opt_span, prefix, suffix,
);
FulfillReqLifetime { span, ty: self.resolve_vars_if_possible(ty), note }
.into_diagnostic(self.tcx.sess.dcx())
self.tcx.sess.dcx().create_err(FulfillReqLifetime {
span,
ty: self.resolve_vars_if_possible(ty),
note,
})
}
infer::RelateRegionParamBound(span) => {
let param_instantiated = note_and_explain::RegionExplanation::new(
@ -194,11 +193,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::LfParamMustOutlive,
note_and_explain::SuffixKind::Empty,
);
LfBoundNotSatisfied {
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
span,
notes: param_instantiated.into_iter().chain(param_must_outlive).collect(),
}
.into_diagnostic(self.tcx.sess.dcx())
})
}
infer::ReferenceOutlivesReferent(ty, span) => {
let pointer_valid = note_and_explain::RegionExplanation::new(
@ -215,12 +213,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::DataValidFor,
note_and_explain::SuffixKind::Empty,
);
RefLongerThanData {
self.tcx.sess.dcx().create_err(RefLongerThanData {
span,
ty: self.resolve_vars_if_possible(ty),
notes: pointer_valid.into_iter().chain(data_valid).collect(),
}
.into_diagnostic(self.tcx.sess.dcx())
})
}
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
let mut err = self.report_extra_impl_obligation(
@ -277,11 +274,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
note_and_explain::PrefixKind::LfMustOutlive,
note_and_explain::SuffixKind::Empty,
);
LfBoundNotSatisfied {
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
span,
notes: instantiated.into_iter().chain(must_outlive).collect(),
}
.into_diagnostic(self.tcx.sess.dcx())
})
}
};
if sub.is_error() || sup.is_error() {