]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_typeck/src/structured_errors/sized_unsized_cast.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_typeck / src / structured_errors / sized_unsized_cast.rs
CommitLineData
5869c6ff 1use crate::structured_errors::StructuredDiagnostic;
5e7ed085 2use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
064997fb 3use rustc_middle::ty::{Ty, TypeVisitable};
5869c6ff
XL
4use rustc_session::Session;
5use rustc_span::Span;
6
7pub struct SizedUnsizedCast<'tcx> {
8 pub sess: &'tcx Session,
9 pub span: Span,
10 pub expr_ty: Ty<'tcx>,
11 pub cast_ty: String,
12}
13
14impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
15 fn session(&self) -> &Session {
16 self.sess
17 }
18
19 fn code(&self) -> DiagnosticId {
20 rustc_errors::error_code!(E0607)
21 }
22
5e7ed085
FG
23 fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
24 let mut err = self.sess.struct_span_err_with_code(
25 self.span,
26 &format!(
27 "cannot cast thin pointer `{}` to fat pointer `{}`",
28 self.expr_ty, self.cast_ty
29 ),
30 self.code(),
31 );
32
5869c6ff 33 if self.expr_ty.references_error() {
5e7ed085 34 err.downgrade_to_delayed_bug();
5869c6ff 35 }
5e7ed085
FG
36
37 err
5869c6ff
XL
38 }
39
5e7ed085
FG
40 fn diagnostic_extended(
41 &self,
42 mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
43 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
5869c6ff
XL
44 err.help(
45 "Thin pointers are \"simple\" pointers: they are purely a reference to a
46memory address.
47
48Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
49called DST). DST don't have a statically known size, therefore they can
50only exist behind some kind of pointers that contain additional
51information. Slices and trait objects are DSTs. In the case of slices,
52the additional information the fat pointer holds is their size.
53
54To fix this error, don't try to cast directly between thin and fat
55pointers.
56
57For more information about casts, take a look at The Book:
58https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
59 );
60 err
61 }
62}