Auto merge of #119751 - nnethercote:error-api-fixes, r=oli-obk

Diagnostic API fixes

Some improvements to diagnostic APIs: improve some naming, use shortcuts in more places, and add a couple of missing methods.

r? `@compiler-errors`
This commit is contained in:
bors 2024-01-10 18:03:53 +00:00
commit a2d9d73e60
135 changed files with 766 additions and 756 deletions

View file

@ -1,5 +1,5 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
@ -305,7 +305,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
binding.span,
format!("{} `{}` is private", assoc_item.kind, binding.item_name),
)
.span_label_mv(binding.span, format!("private {}", assoc_item.kind))
.with_span_label(binding.span, format!("private {}", assoc_item.kind))
.emit();
}
tcx.check_stability(assoc_item.def_id, Some(hir_ref_id), binding.span, None);
@ -462,7 +462,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
late_bound_in_trait_ref,
late_bound_in_ty,
|br_name| {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
binding.span,
E0582,

View file

@ -7,7 +7,7 @@ use crate::fluent_generated as fluent;
use crate::traits::error_reporting::report_object_safety_error;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_errors::{pluralize, struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_infer::traits::FulfillmentError;
@ -346,7 +346,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
candidates: Vec<DefId>,
span: Span,
) -> ErrorGuaranteed {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.tcx().dcx(),
name.span,
E0034,
@ -445,7 +445,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
String::new()
};
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
name.span,
E0220,
@ -697,7 +697,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let names = names.join(", ");
trait_bound_spans.sort();
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
trait_bound_spans,
E0191,

View file

@ -5,7 +5,7 @@ use crate::astconv::{
};
use crate::structured_errors::{GenericArgsInfo, StructuredDiagnostic, WrongNumberOfGenericArgs};
use rustc_ast::ast::ParamKindOrd;
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed, MultiSpan};
use rustc_errors::{struct_span_code_err, Applicability, Diagnostic, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
@ -27,7 +27,7 @@ fn generic_arg_mismatch_err(
help: Option<String>,
) -> ErrorGuaranteed {
let sess = tcx.sess;
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
arg.span(),
E0747,
@ -70,7 +70,7 @@ fn generic_arg_mismatch_err(
Res::Err => {
add_braces_suggestion(arg, &mut err);
return err
.primary_message_mv("unresolved item provided when a constant was expected")
.with_primary_message("unresolved item provided when a constant was expected")
.emit();
}
Res::Def(DefKind::TyParam, src_def_id) => {
@ -650,8 +650,8 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
if position == GenericArgPosition::Value
&& args.num_lifetime_params() != param_counts.lifetimes
{
struct_span_err!(tcx.dcx(), span, E0794, "{}", msg)
.span_note_mv(span_late, note)
struct_span_code_err!(tcx.dcx(), span, E0794, "{}", msg)
.with_span_note(span_late, note)
.emit();
} else {
let mut multispan = MultiSpan::from_span(span);

View file

@ -213,7 +213,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let msg = "trait objects must include the `dyn` keyword";
let label = "add `dyn` keyword before this trait";
let mut diag =
rustc_errors::struct_span_err!(tcx.dcx(), self_ty.span, E0782, "{}", msg);
rustc_errors::struct_span_code_err!(tcx.dcx(), self_ty.span, E0782, "{}", msg);
if self_ty.span.can_be_used_for_suggestions()
&& !self.maybe_lint_impl_trait(self_ty, &mut diag)
{

View file

@ -18,8 +18,8 @@ use crate::require_c_abi_if_c_variadic;
use rustc_ast::TraitObjectSyntax;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{
error_code, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
FatalError, MultiSpan,
error_code, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
ErrorGuaranteed, FatalError, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
@ -866,7 +866,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
traits: &[String],
name: Symbol,
) -> ErrorGuaranteed {
let mut err = struct_span_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
let mut err =
struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
if self
.tcx()
.resolutions(())
@ -1313,7 +1314,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let msg = format!("expected type, found variant `{assoc_ident}`");
tcx.dcx().span_err(span, msg)
} else if qself_ty.is_enum() {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
assoc_ident.span,
E0599,
@ -1354,7 +1355,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
reported
} else if let ty::Alias(ty::Opaque, alias_ty) = qself_ty.kind() {
// `<impl Trait as OtherTrait>::Assoc` makes no sense.
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(alias_ty.def_id),
E0667,
@ -1617,9 +1618,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let def_span = tcx.def_span(item);
tcx.dcx()
.struct_span_err(span, msg)
.code_mv(rustc_errors::error_code!(E0624))
.span_label_mv(span, format!("private {kind}"))
.span_label_mv(def_span, format!("{kind} defined here"))
.with_code(rustc_errors::error_code!(E0624))
.with_span_label(span, format!("private {kind}"))
.with_span_label(def_span, format!("{kind} defined here"))
.emit();
}
tcx.check_stability(item, Some(block), span, None);
@ -1850,7 +1851,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
};
let last_span = *arg_spans.last().unwrap();
let span: MultiSpan = arg_spans.into();
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.tcx().dcx(),
span,
E0109,
@ -2601,7 +2602,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(&output);
self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
decl.output.span(),
E0581,

View file

@ -2,7 +2,7 @@ use crate::astconv::{GenericArgCountMismatch, GenericArgCountResult, OnlySelfBou
use crate::bounds::Bounds;
use crate::errors::TraitObjectDeclaredWithNoTraits;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
@ -89,7 +89,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if regular_traits.len() > 1 {
let first_trait = &regular_traits[0];
let additional_trait = &regular_traits[1];
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
additional_trait.bottom().1,
E0225,
@ -290,7 +290,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if references_self {
let def_id = i.bottom().0.def_id();
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
i.bottom().1,
E0038,
@ -298,7 +298,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
tcx.def_descr(def_id),
tcx.item_name(def_id),
)
.note_mv(
.with_note(
rustc_middle::traits::ObjectSafetyViolation::SupertraitSelf(smallvec![])
.error_msg(),
)
@ -375,7 +375,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.ast_region_to_region(lifetime, None)
} else {
self.re_infer(None, span).unwrap_or_else(|| {
let err = struct_span_err!(
let err = struct_span_code_err!(
tcx.dcx(),
span,
E0228,

View file

@ -37,7 +37,7 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
match tcx.sess.target.is_abi_supported(abi) {
Some(true) => (),
Some(false) => {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
span,
E0570,
@ -58,7 +58,7 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
// This ABI is only allowed on function pointers
if abi == Abi::CCmseNonSecureCall {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
span,
E0781,
@ -560,14 +560,14 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
(0, _) => ("const", "consts", None),
_ => ("type or const", "types or consts", None),
};
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
item.span,
E0044,
"foreign items may not have {kinds} parameters",
)
.span_label_mv(item.span, format!("can't have {kinds} parameters"))
.help_mv(
.with_span_label(item.span, format!("can't have {kinds} parameters"))
.with_help(
// FIXME: once we start storing spans for type arguments, turn this
// into a suggestion.
format!(
@ -659,10 +659,7 @@ pub(super) fn check_specialization_validity<'tcx>(
if !tcx.is_impl_trait_in_trait(impl_item) {
report_forbidden_specialization(tcx, impl_item, parent_impl);
} else {
tcx.dcx().span_delayed_bug(
DUMMY_SP,
format!("parent item: {parent_impl:?} not marked as default"),
);
tcx.dcx().delayed_bug(format!("parent item: {parent_impl:?} not marked as default"));
}
}
}
@ -687,7 +684,7 @@ fn check_impl_items_against_trait<'tcx>(
ty::ImplPolarity::Negative => {
if let [first_item_ref, ..] = impl_item_refs {
let first_item_span = tcx.def_span(first_item_ref);
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
first_item_span,
E0749,
@ -804,10 +801,9 @@ fn check_impl_items_against_trait<'tcx>(
};
tcx.dcx()
.struct_span_err(tcx.def_span(def_id), msg)
.note_mv(format!(
"specialization behaves in inconsistent and \
surprising ways with {feature}, \
and for now is disallowed"
.with_note(format!(
"specialization behaves in inconsistent and surprising ways with \
{feature}, and for now is disallowed"
))
.emit();
}
@ -840,13 +836,13 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
{
let fields = &def.non_enum_variant().fields;
if fields.is_empty() {
struct_span_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
return;
}
let e = fields[FieldIdx::from_u32(0)].ty(tcx, args);
if !fields.iter().all(|f| f.ty(tcx, args) == e) {
struct_span_err!(tcx.dcx(), sp, E0076, "SIMD vector should be homogeneous")
.span_label_mv(sp, "SIMD elements must have the same type")
struct_span_code_err!(tcx.dcx(), sp, E0076, "SIMD vector should be homogeneous")
.with_span_label(sp, "SIMD elements must have the same type")
.emit();
return;
}
@ -858,10 +854,10 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
};
if let Some(len) = len {
if len == 0 {
struct_span_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
return;
} else if len > MAX_SIMD_LANES {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
sp,
E0075,
@ -884,7 +880,7 @@ pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
if matches!(t.kind(), ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)) =>
{ /* struct([f32; 4]) is ok */ }
_ => {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
sp,
E0077,
@ -907,7 +903,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
&& let Some(repr_pack) = repr.pack
&& pack as u64 != repr_pack.bytes()
{
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
sp,
E0634,
@ -918,7 +914,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
}
}
if repr.align.is_some() {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
sp,
E0587,
@ -927,7 +923,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
.emit();
} else {
if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
sp,
E0588,
@ -1117,13 +1113,13 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
if def.variants().is_empty() {
if let Some(attr) = tcx.get_attrs(def_id, sym::repr).next() {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
attr.span,
E0084,
"unsupported representation for zero-variant enum"
)
.span_label_mv(tcx.def_span(def_id), "zero-variant enum")
.with_span_label(tcx.def_span(def_id), "zero-variant enum")
.emit();
}
}
@ -1156,7 +1152,7 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let disr_non_unit = def.variants().iter().any(|var| !is_unit(var) && has_disr(var));
if disr_non_unit || (disr_units && has_non_units) {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(def_id),
E0732,
@ -1242,7 +1238,7 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
if discrs[i].1.val == discrs[o].1.val {
let err = error.get_or_insert_with(|| {
let mut ret = struct_span_err!(
let mut ret = struct_span_code_err!(
tcx.dcx(),
tcx.def_span(adt.did()),
E0081,
@ -1309,9 +1305,15 @@ pub(super) fn check_type_params_are_used<'tcx>(
&& let ty::GenericParamDefKind::Type { .. } = param.kind
{
let span = tcx.def_span(param.def_id);
struct_span_err!(tcx.dcx(), span, E0091, "type parameter `{}` is unused", param.name,)
.span_label_mv(span, "unused type parameter")
.emit();
struct_span_code_err!(
tcx.dcx(),
span,
E0091,
"type parameter `{}` is unused",
param.name,
)
.with_span_label(span, "unused type parameter")
.emit();
}
}
}
@ -1329,7 +1331,7 @@ fn opaque_type_cycle_error(
opaque_def_id: LocalDefId,
span: Span,
) -> ErrorGuaranteed {
let mut err = struct_span_err!(tcx.dcx(), span, E0720, "cannot resolve opaque type");
let mut err = struct_span_code_err!(tcx.dcx(), span, E0720, "cannot resolve opaque type");
let mut label = false;
if let Some((def_id, visitor)) = get_owner_return_paths(tcx, opaque_def_id) {

View file

@ -2,7 +2,7 @@ use super::potentially_plural_count;
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
use hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorGuaranteed};
use rustc_errors::{pluralize, struct_span_code_err, Applicability, DiagnosticId, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit;
@ -19,7 +19,7 @@ use rustc_middle::ty::{
self, GenericArgs, Ty, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
};
use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
use rustc_trait_selection::traits::{
@ -625,7 +625,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
match ocx.eq(&cause, param_env, trait_return_ty, impl_return_ty) {
Ok(()) => {}
Err(terr) => {
let mut diag = struct_span_err!(
let mut diag = struct_span_code_err!(
tcx.dcx(),
cause.span(),
E0053,
@ -934,17 +934,15 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
return_span,
"return type captures more lifetimes than trait definition",
)
.span_label_mv(self.tcx.def_span(def_id), "this lifetime was captured")
.span_note_mv(
.with_span_label(self.tcx.def_span(def_id), "this lifetime was captured")
.with_span_note(
self.tcx.def_span(self.def_id),
"hidden type must only reference lifetimes captured by this impl trait",
)
.note_mv(format!("hidden type inferred to be `{}`", self.ty))
.with_note(format!("hidden type inferred to be `{}`", self.ty))
.emit()
}
_ => {
self.tcx.dcx().span_delayed_bug(DUMMY_SP, "should've been able to remap region")
}
_ => self.tcx.dcx().delayed_bug("should've been able to remap region"),
};
return Err(guar);
};
@ -972,7 +970,7 @@ fn report_trait_method_mismatch<'tcx>(
let (impl_err_span, trait_err_span) =
extract_spans_for_error_reporting(infcx, terr, &cause, impl_m, trait_m);
let mut diag = struct_span_err!(
let mut diag = struct_span_code_err!(
tcx.dcx(),
impl_err_span,
E0053,
@ -1217,7 +1215,7 @@ fn compare_self_type<'tcx>(
(false, true) => {
let self_descr = self_string(impl_m);
let impl_m_span = tcx.def_span(impl_m.def_id);
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_m_span,
E0185,
@ -1237,7 +1235,7 @@ fn compare_self_type<'tcx>(
(true, false) => {
let self_descr = self_string(trait_m);
let impl_m_span = tcx.def_span(impl_m.def_id);
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_m_span,
E0186,
@ -1303,8 +1301,7 @@ fn compare_number_of_generics<'tcx>(
// inheriting the generics from will also have mismatched arguments, and
// we'll report an error for that instead. Delay a bug for safety, though.
if trait_.is_impl_trait_in_trait() {
return Err(tcx.dcx().span_delayed_bug(
rustc_span::DUMMY_SP,
return Err(tcx.dcx().delayed_bug(
"errors comparing numbers of generics of trait/impl functions were not emitted",
));
}
@ -1463,7 +1460,7 @@ fn compare_number_of_method_arguments<'tcx>(
})
.unwrap_or_else(|| tcx.def_span(impl_m.def_id));
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_span,
E0050,
@ -1530,7 +1527,7 @@ fn compare_synthetic_generics<'tcx>(
let impl_def_id = impl_def_id.expect_local();
let impl_span = tcx.def_span(impl_def_id);
let trait_span = tcx.def_span(trait_def_id);
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_span,
E0643,
@ -1689,7 +1686,7 @@ fn compare_generic_param_kinds<'tcx>(
let param_impl_span = tcx.def_span(param_impl.def_id);
let param_trait_span = tcx.def_span(param_trait.def_id);
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
param_impl_span,
E0053,
@ -1836,7 +1833,7 @@ fn compare_const_predicate_entailment<'tcx>(
let (ty, _) = tcx.hir().expect_impl_item(impl_ct_def_id).expect_const();
cause.span = ty.span;
let mut diag = struct_span_err!(
let mut diag = struct_span_code_err!(
tcx.dcx(),
cause.span,
E0326,

View file

@ -7,7 +7,7 @@ use rustc_middle::traits::{ObligationCause, Reveal};
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;
use rustc_trait_selection::traits::{
elaborate, normalize_param_env_or_error, outlives_bounds::InferCtxtExt, ObligationCtxt,
};
@ -153,10 +153,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
trait_m_sig.inputs_and_output,
));
if !ocx.select_all_or_error().is_empty() {
tcx.dcx().span_delayed_bug(
DUMMY_SP,
"encountered errors when checking RPITIT refinement (selection)",
);
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
return;
}
let outlives_env = OutlivesEnvironment::with_bounds(
@ -165,18 +162,12 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
);
let errors = infcx.resolve_regions(&outlives_env);
if !errors.is_empty() {
tcx.dcx().span_delayed_bug(
DUMMY_SP,
"encountered errors when checking RPITIT refinement (regions)",
);
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)");
return;
}
// Resolve any lifetime variables that may have been introduced during normalization.
let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else {
tcx.dcx().span_delayed_bug(
DUMMY_SP,
"encountered errors when checking RPITIT refinement (resolution)",
);
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (resolution)");
return;
};

View file

@ -2,7 +2,7 @@
//
// We don't do any drop checking during hir typeck.
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{struct_span_err, ErrorGuaranteed};
use rustc_errors::{struct_span_code_err, ErrorGuaranteed};
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
use rustc_middle::ty::util::CheckRegions;
@ -88,8 +88,12 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
let drop_impl_span = tcx.def_span(drop_impl_did);
let item_span = tcx.def_span(self_type_did);
let self_descr = tcx.def_descr(self_type_did);
let mut err =
struct_span_err!(tcx.dcx(), drop_impl_span, E0366, "`Drop` impls cannot be specialized");
let mut err = struct_span_code_err!(
tcx.dcx(),
drop_impl_span,
E0366,
"`Drop` impls cannot be specialized"
);
match arg {
ty::util::NotUniqueParam::DuplicateParam(arg) => {
err.note(format!("`{arg}` is mentioned multiple times"))
@ -154,14 +158,14 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
let item_span = tcx.def_span(adt_def_id);
let self_descr = tcx.def_descr(adt_def_id.to_def_id());
guar = Some(
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
error.root_obligation.cause.span,
E0367,
"`Drop` impl requires `{root_predicate}` \
but the {self_descr} it is implemented for does not",
)
.span_note_mv(item_span, "the implementor must specify the same requirement")
.with_span_note(item_span, "the implementor must specify the same requirement")
.emit(),
);
}
@ -186,14 +190,14 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
}
};
guar = Some(
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
error.origin().span(),
E0367,
"`Drop` impl requires `{outlives}` \
but the {self_descr} it is implemented for does not",
)
.span_note_mv(item_span, "the implementor must specify the same requirement")
.with_span_note(item_span, "the implementor must specify the same requirement")
.emit(),
);
}

View file

@ -8,7 +8,7 @@ use crate::errors::{
};
use hir::def_id::DefId;
use rustc_errors::{struct_span_err, DiagnosticMessage};
use rustc_errors::{struct_span_code_err, DiagnosticMessage};
use rustc_hir as hir;
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::{self, Ty, TyCtxt};
@ -29,8 +29,8 @@ fn equate_intrinsic_type<'tcx>(
(own_counts, generics.span)
}
_ => {
struct_span_err!(tcx.dcx(), it.span, E0622, "intrinsic must be a function")
.span_label_mv(it.span, "expected a function")
struct_span_code_err!(tcx.dcx(), it.span, E0622, "intrinsic must be a function")
.with_span_label(it.span, "expected a function")
.emit();
return;
}
@ -552,7 +552,7 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
sym::simd_shuffle_generic => (2, 1, vec![param(0), param(0)], param(1)),
_ => {
let msg = format!("unrecognized platform-specific intrinsic function: `{name}`");
tcx.dcx().struct_span_err(it.span, msg).emit();
tcx.dcx().span_err(it.span, msg);
return;
}
};

View file

@ -4,7 +4,7 @@ use rustc_hir as hir;
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
use rustc_session::lint;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Symbol, DUMMY_SP};
use rustc_span::Symbol;
use rustc_target::abi::FieldIdx;
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
@ -156,7 +156,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(
.with_note(
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
)
@ -171,7 +171,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(format!("`{ty}` does not implement the Copy trait"))
.with_note(format!("`{ty}` does not implement the Copy trait"))
.emit();
}
@ -191,11 +191,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
self.tcx
.dcx()
.struct_span_err(vec![in_expr.span, expr.span], msg)
.span_label_mv(in_expr.span, format!("type `{in_expr_ty}`"))
.span_label_mv(expr.span, format!("type `{ty}`"))
.note_mv(
.with_span_label(in_expr.span, format!("type `{in_expr_ty}`"))
.with_span_label(expr.span, format!("type `{ty}`"))
.with_note(
"asm inout arguments must have the same type, \
unless they are both pointers or integers of the same size",
unless they are both pointers or integers of the same size",
)
.emit();
}
@ -242,7 +242,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(format!(
.with_note(format!(
"this is required to use type `{}` with register class `{}`",
ty,
reg_class.name(),
@ -294,7 +294,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
let Some(asm_arch) = self.tcx.sess.asm_arch else {
self.tcx.dcx().span_delayed_bug(DUMMY_SP, "target architecture does not support asm");
self.tcx.dcx().delayed_bug("target architecture does not support asm");
return;
};
for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
@ -325,7 +325,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
op.is_clobber(),
) {
let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
continue;
}
}
@ -364,7 +364,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
reg_class.name(),
feature
);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
// register isn't enabled, don't do more checks
continue;
}
@ -378,7 +378,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
.intersperse(", ")
.collect::<String>(),
);
self.tcx.dcx().struct_span_err(*op_sp, msg).emit();
self.tcx.dcx().span_err(*op_sp, msg);
// register isn't enabled, don't do more checks
continue;
}
@ -459,11 +459,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
self.tcx
.dcx()
.struct_span_err(*op_sp, "invalid `sym` operand")
.span_label_mv(
.with_span_label(
self.tcx.def_span(anon_const.def_id),
format!("is {} `{}`", ty.kind().article(), ty),
)
.help_mv(
.with_help(
"`sym` operands must refer to either a function or a static",
)
.emit();

View file

@ -78,7 +78,7 @@ use std::num::NonZeroU32;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorGuaranteed;
use rustc_errors::{pluralize, struct_span_err, Diagnostic, DiagnosticBuilder};
use rustc_errors::{pluralize, struct_span_code_err, Diagnostic, DiagnosticBuilder};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_index::bit_set::BitSet;

View file

@ -3,7 +3,9 @@ use crate::constrained_generic_params::{identify_constrained_generic_params, Par
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{
pluralize, struct_span_code_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
use rustc_hir::lang_items::LangItem;
@ -200,8 +202,8 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
res = Err(tcx
.dcx()
.struct_span_err(sp, "impls of auto traits cannot be default")
.span_labels_mv(impl_.defaultness_span, "default because of this")
.span_label_mv(sp, "auto trait")
.with_span_labels(impl_.defaultness_span, "default because of this")
.with_span_label(sp, "auto trait")
.emit());
}
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
@ -217,7 +219,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
if let hir::Defaultness::Default { .. } = impl_.defaultness {
let mut spans = vec![span];
spans.extend(impl_.defaultness_span);
res = Err(struct_span_err!(
res = Err(struct_span_code_err!(
tcx.dcx(),
spans,
E0750,
@ -502,19 +504,18 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
gat_item_hir.span,
format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
)
.span_suggestion_mv(
.with_span_suggestion(
gat_item_hir.generics.tail_span_for_predicate_suggestion(),
format!("add the required where clause{plural}"),
suggestion,
Applicability::MachineApplicable,
)
.note_mv(format!(
.with_note(format!(
"{bound} currently required to ensure that impls have maximum flexibility"
))
.note_mv(
.with_note(
"we are soliciting feedback, see issue #87479 \
<https://github.com/rust-lang/rust/issues/87479> \
for more information",
<https://github.com/rust-lang/rust/issues/87479> for more information",
)
.emit();
}
@ -837,8 +838,8 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
trait_should_be_self,
"associated item referring to unboxed trait object for its own trait",
)
.span_label_mv(trait_name.span, "in this trait")
.multipart_suggestion_mv(
.with_span_label(trait_name.span, "in this trait")
.with_multipart_suggestion(
"you might have meant to use `Self` to refer to the implementing type",
sugg,
Applicability::MachineApplicable,
@ -1116,7 +1117,7 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) -> Result<(), ErrorGuarant
|| matches!(trait_def.specialization_kind, TraitSpecializationKind::Marker)
{
for associated_def_id in &*tcx.associated_item_def_ids(def_id) {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(*associated_def_id),
E0714,
@ -1598,7 +1599,7 @@ fn check_method_receiver<'tcx>(
the `arbitrary_self_types` feature",
),
)
.help_mv(HELP_FOR_SELF_TYPE)
.with_help(HELP_FOR_SELF_TYPE)
.emit()
} else {
// Report error; would not have worked with `arbitrary_self_types`.
@ -1610,9 +1611,9 @@ fn check_method_receiver<'tcx>(
}
fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) -> ErrorGuaranteed {
struct_span_err!(tcx.dcx(), span, E0307, "invalid `self` parameter type: {receiver_ty}")
.note_mv("type of `self` must be `Self` or a type that dereferences to it")
.help_mv(HELP_FOR_SELF_TYPE)
struct_span_code_err!(tcx.dcx(), span, E0307, "invalid `self` parameter type: {receiver_ty}")
.with_note("type of `self` must be `Self` or a type that dereferences to it")
.with_help(HELP_FOR_SELF_TYPE)
.emit()
}
@ -1920,8 +1921,8 @@ fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), Error
}
fn error_392(tcx: TyCtxt<'_>, span: Span, param_name: Symbol) -> DiagnosticBuilder<'_> {
struct_span_err!(tcx.dcx(), span, E0392, "parameter `{param_name}` is never used")
.span_label_mv(span, "unused parameter")
struct_span_code_err!(tcx.dcx(), span, E0392, "parameter `{param_name}` is never used")
.with_span_label(span, "unused parameter")
}
pub fn provide(providers: &mut Providers) {

View file

@ -1,5 +1,5 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
@ -70,15 +70,15 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
match seen_items.entry(norm_ident) {
Entry::Occupied(entry) => {
let former = entry.get();
struct_span_err!(
struct_span_code_err!(
self.tcx.dcx(),
span,
E0592,
"duplicate definitions with name `{}`",
ident,
)
.span_label_mv(span, format!("duplicate definitions for `{ident}`"))
.span_label_mv(*former, format!("other definition for `{ident}`"))
.with_span_label(span, format!("duplicate definitions for `{ident}`"))
.with_span_label(*former, format!("other definition for `{ident}`"))
.emit();
}
Entry::Vacant(entry) => {
@ -104,7 +104,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
if let Some(item2) = collision {
let name = item1.ident(self.tcx).normalize_to_macros_2_0();
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.tcx.dcx(),
self.tcx.def_span(item1.def_id),
E0592,

View file

@ -6,7 +6,7 @@
// mappings. That mapping code resides here.
use crate::errors;
use rustc_errors::{error_code, struct_span_err};
use rustc_errors::{error_code, struct_span_code_err};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
@ -45,7 +45,7 @@ fn enforce_trait_manually_implementable(
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`
if tcx.trait_def(trait_def_id).deny_explicit_impl {
let trait_name = tcx.item_name(trait_def_id);
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
impl_header_span,
E0322,
@ -88,7 +88,7 @@ fn enforce_empty_impls_for_marker_traits(
return;
}
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(impl_def_id),
E0715,
@ -173,7 +173,7 @@ fn check_object_overlap<'tcx>(
let mut supertrait_def_ids = traits::supertrait_def_ids(tcx, component_def_id);
if supertrait_def_ids.any(|d| d == trait_def_id) {
let span = tcx.def_span(impl_def_id);
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
span,
E0371,
@ -181,7 +181,7 @@ fn check_object_overlap<'tcx>(
trait_ref.self_ty(),
tcx.def_path_str(trait_def_id)
)
.span_label_mv(
.with_span_label(
span,
format!(
"`{}` automatically implements trait `{}`",

View file

@ -1,7 +1,7 @@
//! Unsafety checker: every impl either implements a trait defined in this
//! crate or pertains to a type defined in this crate.
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::Unsafety;
use rustc_middle::ty::TyCtxt;
@ -18,14 +18,14 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
impl_.generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
match (trait_def.unsafety, unsafe_attr, impl_.unsafety, impl_.polarity) {
(Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(def_id),
E0199,
"implementing the trait `{}` is not unsafe",
trait_ref.print_trait_sugared()
)
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)),
"remove `unsafe` from this trait implementation",
"",
@ -35,20 +35,20 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
(Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(def_id),
E0200,
"the trait `{}` requires an `unsafe impl` declaration",
trait_ref.print_trait_sugared()
)
.note_mv(format!(
.with_note(format!(
"the trait `{}` enforces invariants that the compiler can't check. \
Review the trait documentation and make sure this implementation \
upholds those invariants before adding the `unsafe` keyword",
trait_ref.print_trait_sugared()
))
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
item.span.shrink_to_lo(),
"add `unsafe` to this trait implementation",
"unsafe ",
@ -58,20 +58,20 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
(Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
tcx.def_span(def_id),
E0569,
"requires an `unsafe impl` declaration due to `#[{}]` attribute",
attr_name
)
.note_mv(format!(
.with_note(format!(
"the trait `{}` enforces invariants that the compiler can't check. \
Review the trait documentation and make sure this implementation \
upholds those invariants before adding the `unsafe` keyword",
trait_ref.print_trait_sugared()
))
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
item.span.shrink_to_lo(),
"add `unsafe` to this trait implementation",
"unsafe ",

View file

@ -8,7 +8,7 @@
use rustc_ast::walk_list;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LocalDefId;
@ -22,7 +22,7 @@ use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
use rustc_session::lint;
use rustc_span::def_id::DefId;
use rustc_span::symbol::{sym, Ident};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;
use std::fmt;
use crate::errors;
@ -335,13 +335,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
// though this may happen when we call `poly_trait_ref_binder_info` with
// an (erroneous, #113423) associated return type bound in an impl header.
if !supertrait_bound_vars.is_empty() {
self.tcx.dcx().span_delayed_bug(
DUMMY_SP,
format!(
"found supertrait lifetimes without a binder to append \
self.tcx.dcx().delayed_bug(format!(
"found supertrait lifetimes without a binder to append \
them to: {supertrait_bound_vars:?}"
),
);
));
}
break (vec![], BinderScopeType::Normal);
}
@ -737,7 +734,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
// Ensure that the parent of the def is an item, not HRTB
let parent_id = self.tcx.hir().parent_id(hir_id);
if !parent_id.is_owner() {
struct_span_err!(
struct_span_code_err!(
self.tcx.dcx(),
lifetime.ident.span,
E0657,
@ -754,7 +751,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
lifetime.ident.span,
"higher kinded lifetime bounds on nested opaque types are not supported yet",
)
.span_note_mv(self.tcx.def_span(def_id), "lifetime declared here")
.with_span_note(self.tcx.def_span(def_id), "lifetime declared here")
.emit();
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
}

View file

@ -12,7 +12,7 @@ use crate::constrained_generic_params as cgp;
use min_specialization::check_min_specialization;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_errors::struct_span_code_err;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
use rustc_middle::query::Providers;
@ -170,7 +170,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
}
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol) {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
tcx.dcx(),
span,
E0207,

View file

@ -523,7 +523,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
fn start_diagnostics(&self) -> DiagnosticBuilder<'tcx> {
let span = self.path_segment.ident.span;
let msg = self.create_error_message();
self.tcx.dcx().struct_span_err(span, msg).code_mv(self.code())
self.tcx.dcx().struct_span_err(span, msg).with_code(self.code())
}
/// Builds the `expected 1 type argument / supplied 2 type arguments` message.