]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/structured_errors.rs
Rustdoc render public underscore_imports as Re-exports
[rust.git] / compiler / rustc_typeck / src / structured_errors.rs
1 use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId};
2 use rustc_middle::ty::{Ty, TypeFoldable};
3 use rustc_session::Session;
4 use rustc_span::Span;
5
6 pub trait StructuredDiagnostic<'tcx> {
7     fn session(&self) -> &Session;
8
9     fn code(&self) -> DiagnosticId;
10
11     fn common(&self) -> DiagnosticBuilder<'tcx>;
12
13     fn diagnostic(&self) -> DiagnosticBuilder<'tcx> {
14         let err = self.common();
15         if self.session().teach(&self.code()) { self.extended(err) } else { self.regular(err) }
16     }
17
18     fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
19         err
20     }
21
22     fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
23         err
24     }
25 }
26
27 pub struct VariadicError<'tcx> {
28     sess: &'tcx Session,
29     span: Span,
30     t: Ty<'tcx>,
31     cast_ty: &'tcx str,
32 }
33
34 impl<'tcx> VariadicError<'tcx> {
35     pub fn new(
36         sess: &'tcx Session,
37         span: Span,
38         t: Ty<'tcx>,
39         cast_ty: &'tcx str,
40     ) -> VariadicError<'tcx> {
41         VariadicError { sess, span, t, cast_ty }
42     }
43 }
44
45 impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> {
46     fn session(&self) -> &Session {
47         self.sess
48     }
49
50     fn code(&self) -> DiagnosticId {
51         rustc_errors::error_code!(E0617)
52     }
53
54     fn common(&self) -> DiagnosticBuilder<'tcx> {
55         let mut err = if self.t.references_error() {
56             self.sess.diagnostic().struct_dummy()
57         } else {
58             self.sess.struct_span_fatal_with_code(
59                 self.span,
60                 &format!("can't pass `{}` to variadic function", self.t),
61                 self.code(),
62             )
63         };
64         if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
65             err.span_suggestion(
66                 self.span,
67                 &format!("cast the value to `{}`", self.cast_ty),
68                 format!("{} as {}", snippet, self.cast_ty),
69                 Applicability::MachineApplicable,
70             );
71         } else {
72             err.help(&format!("cast the value to `{}`", self.cast_ty));
73         }
74         err
75     }
76
77     fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
78         err.note(&format!(
79             "certain types, like `{}`, must be cast before passing them to a \
80                            variadic function, because of arcane ABI rules dictated by the C \
81                            standard",
82             self.t
83         ));
84         err
85     }
86 }
87
88 pub struct SizedUnsizedCastError<'tcx> {
89     sess: &'tcx Session,
90     span: Span,
91     expr_ty: Ty<'tcx>,
92     cast_ty: String,
93 }
94
95 impl<'tcx> SizedUnsizedCastError<'tcx> {
96     pub fn new(
97         sess: &'tcx Session,
98         span: Span,
99         expr_ty: Ty<'tcx>,
100         cast_ty: String,
101     ) -> SizedUnsizedCastError<'tcx> {
102         SizedUnsizedCastError { sess, span, expr_ty, cast_ty }
103     }
104 }
105
106 impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> {
107     fn session(&self) -> &Session {
108         self.sess
109     }
110
111     fn code(&self) -> DiagnosticId {
112         rustc_errors::error_code!(E0607)
113     }
114
115     fn common(&self) -> DiagnosticBuilder<'tcx> {
116         if self.expr_ty.references_error() {
117             self.sess.diagnostic().struct_dummy()
118         } else {
119             self.sess.struct_span_fatal_with_code(
120                 self.span,
121                 &format!(
122                     "cannot cast thin pointer `{}` to fat pointer `{}`",
123                     self.expr_ty, self.cast_ty
124                 ),
125                 self.code(),
126             )
127         }
128     }
129
130     fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
131         err.help(
132             "Thin pointers are \"simple\" pointers: they are purely a reference to a
133 memory address.
134
135 Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
136 called DST). DST don't have a statically known size, therefore they can
137 only exist behind some kind of pointers that contain additional
138 information. Slices and trait objects are DSTs. In the case of slices,
139 the additional information the fat pointer holds is their size.
140
141 To fix this error, don't try to cast directly between thin and fat
142 pointers.
143
144 For more information about casts, take a look at The Book:
145 https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
146         );
147         err
148     }
149 }