1
Fork 0

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
This commit is contained in:
Obei Sideg 2023-02-25 20:26:48 +03:00
parent b1719530f4
commit 44eb974b23
5 changed files with 68 additions and 28 deletions

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 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
}
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 @@ impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
}
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();