1 use crate::structured_errors::StructuredDiagnostic;
2 use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
3 use rustc_middle::ty::{Ty, TypeVisitable};
4 use rustc_session::Session;
7 pub struct SizedUnsizedCast<'tcx> {
8 pub sess: &'tcx Session,
10 pub expr_ty: Ty<'tcx>,
14 impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
15 fn session(&self) -> &Session {
19 fn code(&self) -> DiagnosticId {
20 rustc_errors::error_code!(E0607)
23 fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
24 let mut err = self.sess.struct_span_err_with_code(
27 "cannot cast thin pointer `{}` to fat pointer `{}`",
28 self.expr_ty, self.cast_ty
33 if self.expr_ty.references_error() {
34 err.downgrade_to_delayed_bug();
40 fn diagnostic_extended(
42 mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
43 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
45 "Thin pointers are \"simple\" pointers: they are purely a reference to a
48 Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
49 called DST). DST don't have a statically known size, therefore they can
50 only exist behind some kind of pointers that contain additional
51 information. Slices and trait objects are DSTs. In the case of slices,
52 the additional information the fat pointer holds is their size.
54 To fix this error, don't try to cast directly between thin and fat
57 For more information about casts, take a look at The Book:
58 https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",