Rollup merge of #108460 - obeis:hir-analysis-migrate-diagnostics-2, r=compiler-errors

migrate `rustc_hir_analysis` to session diagnostic [Part Two]

migrate `rustc_hir_analysis` to session diagnostic (part two)
files list:
- rustc_hir_analysis/variance/*
- rustc_hir_analysis/missing_cast_for_variadic_arg.rs
- rustc_hir_analysis/sized_unsized_cast.rs

Updates #100717
This commit is contained in:
Matthias Krüger 2023-03-07 19:57:43 +01:00 committed by GitHub
commit 14260c83ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 28 deletions

View File

@ -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}`

View File

@ -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<Span>,
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,
}

View File

@ -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
}

View File

@ -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();

View File

@ -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:?}"),
});
}
}
}