diff --git a/compiler/rustc_hir_analysis/locales/en-US.ftl b/compiler/rustc_hir_analysis/locales/en-US.ftl index 1d313945b52..50e857ef60d 100644 --- a/compiler/rustc_hir_analysis/locales/en-US.ftl +++ b/compiler/rustc_hir_analysis/locales/en-US.ftl @@ -155,3 +155,11 @@ hir_analysis_cannot_capture_late_bound_ty_in_anon_const = hir_analysis_cannot_capture_late_bound_const_in_anon_const = cannot capture late-bound const parameter in a constant .label = parameter defined here + +hir_analysis_variances_of = {$variances_of} + +hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function + .suggestion = cast the value to `{$cast_ty}` + .help = cast the value to `{$cast_ty}` + +hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}` diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 3e069275775..74fec93d91e 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -399,3 +399,34 @@ pub(crate) enum CannotCaptureLateBoundInAnonConst { def_span: Span, }, } + +#[derive(Diagnostic)] +#[diag(hir_analysis_variances_of)] +pub(crate) struct VariancesOf { + #[primary_span] + pub span: Span, + pub variances_of: String, +} + +#[derive(Diagnostic)] +#[diag(hir_analysis_pass_to_variadic_function, code = "E0617")] +pub(crate) struct PassToVariadicFunction<'tcx, 'a> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub cast_ty: &'a str, + #[suggestion(code = "{replace}", applicability = "machine-applicable")] + pub sugg_span: Option, + pub replace: String, + #[help] + pub help: Option<()>, +} + +#[derive(Diagnostic)] +#[diag(hir_analysis_cast_thin_pointer_to_fat_pointer, code = "E0607")] +pub(crate) struct CastThinPointerToFatPointer<'tcx> { + #[primary_span] + pub span: Span, + pub expr_ty: Ty<'tcx>, + pub cast_ty: String, +} diff --git a/compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs b/compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs index 089491bef5e..0bfbf99cb0b 100644 --- a/compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs +++ b/compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs @@ -1,5 +1,5 @@ -use crate::structured_errors::StructuredDiagnostic; -use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed}; +use crate::{errors, structured_errors::StructuredDiagnostic}; +use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed}; use rustc_middle::ty::{Ty, TypeVisitableExt}; use rustc_session::Session; use rustc_span::Span; @@ -21,27 +21,26 @@ fn code(&self) -> DiagnosticId { } fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { - let mut err = self.sess.struct_span_err_with_code( - self.span, - &format!("can't pass `{}` to variadic function", self.ty), - self.code(), - ); + let (sugg_span, replace, help) = + if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) { + (Some(self.span), format!("{} as {}", snippet, self.cast_ty), None) + } else { + (None, "".to_string(), Some(())) + }; + + let mut err = self.sess.create_err(errors::PassToVariadicFunction { + span: self.span, + ty: self.ty, + cast_ty: self.cast_ty, + help, + replace, + sugg_span, + }); if self.ty.references_error() { err.downgrade_to_delayed_bug(); } - if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) { - err.span_suggestion( - self.span, - &format!("cast the value to `{}`", self.cast_ty), - format!("{} as {}", snippet, self.cast_ty), - Applicability::MachineApplicable, - ); - } else { - err.help(&format!("cast the value to `{}`", self.cast_ty)); - } - err } diff --git a/compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs b/compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs index 3b9fb367813..910417abe6e 100644 --- a/compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs +++ b/compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs @@ -1,4 +1,4 @@ -use crate::structured_errors::StructuredDiagnostic; +use crate::{errors, structured_errors::StructuredDiagnostic}; use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed}; use rustc_middle::ty::{Ty, TypeVisitableExt}; use rustc_session::Session; @@ -21,14 +21,11 @@ fn code(&self) -> DiagnosticId { } fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { - let mut err = self.sess.struct_span_err_with_code( - self.span, - &format!( - "cannot cast thin pointer `{}` to fat pointer `{}`", - self.expr_ty, self.cast_ty - ), - self.code(), - ); + let mut err = self.sess.create_err(errors::CastThinPointerToFatPointer { + span: self.span, + expr_ty: self.expr_ty, + cast_ty: self.cast_ty.to_owned(), + }); if self.expr_ty.references_error() { err.downgrade_to_delayed_bug(); diff --git a/compiler/rustc_hir_analysis/src/variance/test.rs b/compiler/rustc_hir_analysis/src/variance/test.rs index 5feeb92d337..64614831f56 100644 --- a/compiler/rustc_hir_analysis/src/variance/test.rs +++ b/compiler/rustc_hir_analysis/src/variance/test.rs @@ -1,6 +1,8 @@ use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; +use crate::errors; + pub fn test_variance(tcx: TyCtxt<'_>) { // For unit testing: check for a special "rustc_variance" // attribute and report an error with various results if found. @@ -8,7 +10,10 @@ pub fn test_variance(tcx: TyCtxt<'_>) { if tcx.has_attr(id.owner_id.to_def_id(), sym::rustc_variance) { let variances_of = tcx.variances_of(id.owner_id); - tcx.sess.struct_span_err(tcx.def_span(id.owner_id), format!("{variances_of:?}")).emit(); + tcx.sess.emit_err(errors::VariancesOf { + span: tcx.def_span(id.owner_id), + variances_of: format!("{variances_of:?}"), + }); } } }