rustc_typeck to rustc_hir_analysis

This commit is contained in:
lcnr 2022-09-26 13:00:29 +02:00
parent de0b511daa
commit 1fc86a63f4
140 changed files with 101 additions and 102 deletions

View file

@ -0,0 +1,61 @@
use crate::structured_errors::StructuredDiagnostic;
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
use rustc_middle::ty::{Ty, TypeVisitable};
use rustc_session::Session;
use rustc_span::Span;
pub struct MissingCastForVariadicArg<'tcx, 's> {
pub sess: &'tcx Session,
pub span: Span,
pub ty: Ty<'tcx>,
pub cast_ty: &'s str,
}
impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
fn session(&self) -> &Session {
self.sess
}
fn code(&self) -> DiagnosticId {
rustc_errors::error_code!(E0617)
}
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(),
);
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
}
fn diagnostic_extended(
&self,
mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
err.note(&format!(
"certain types, like `{}`, must be casted before passing them to a \
variadic function, because of arcane ABI rules dictated by the C \
standard",
self.ty
));
err
}
}

View file

@ -0,0 +1,62 @@
use crate::structured_errors::StructuredDiagnostic;
use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
use rustc_middle::ty::{Ty, TypeVisitable};
use rustc_session::Session;
use rustc_span::Span;
pub struct SizedUnsizedCast<'tcx> {
pub sess: &'tcx Session,
pub span: Span,
pub expr_ty: Ty<'tcx>,
pub cast_ty: String,
}
impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
fn session(&self) -> &Session {
self.sess
}
fn code(&self) -> DiagnosticId {
rustc_errors::error_code!(E0607)
}
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(),
);
if self.expr_ty.references_error() {
err.downgrade_to_delayed_bug();
}
err
}
fn diagnostic_extended(
&self,
mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
err.help(
"Thin pointers are \"simple\" pointers: they are purely a reference to a
memory address.
Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
called DST). DST don't have a statically known size, therefore they can
only exist behind some kind of pointers that contain additional
information. Slices and trait objects are DSTs. In the case of slices,
the additional information the fat pointer holds is their size.
To fix this error, don't try to cast directly between thin and fat
pointers.
For more information about casts, take a look at The Book:
https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
);
err
}
}

File diff suppressed because it is too large Load diff