1 use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId};
2 use rustc_middle::ty::{Ty, TypeFoldable};
3 use rustc_session::Session;
6 pub trait StructuredDiagnostic<'tcx> {
7 fn session(&self) -> &Session;
9 fn code(&self) -> DiagnosticId;
11 fn common(&self) -> DiagnosticBuilder<'tcx>;
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) }
18 fn regular(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
22 fn extended(&self, err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
27 pub struct VariadicError<'tcx> {
34 impl<'tcx> VariadicError<'tcx> {
40 ) -> VariadicError<'tcx> {
41 VariadicError { sess, span, t, cast_ty }
45 impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> {
46 fn session(&self) -> &Session {
50 fn code(&self) -> DiagnosticId {
51 rustc_errors::error_code!(E0617)
54 fn common(&self) -> DiagnosticBuilder<'tcx> {
55 let mut err = if self.t.references_error() {
56 self.sess.diagnostic().struct_dummy()
58 self.sess.struct_span_fatal_with_code(
60 &format!("can't pass `{}` to variadic function", self.t),
64 if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
67 &format!("cast the value to `{}`", self.cast_ty),
68 format!("{} as {}", snippet, self.cast_ty),
69 Applicability::MachineApplicable,
72 err.help(&format!("cast the value to `{}`", self.cast_ty));
77 fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
79 "certain types, like `{}`, must be cast before passing them to a \
80 variadic function, because of arcane ABI rules dictated by the C \
88 pub struct SizedUnsizedCastError<'tcx> {
95 impl<'tcx> SizedUnsizedCastError<'tcx> {
101 ) -> SizedUnsizedCastError<'tcx> {
102 SizedUnsizedCastError { sess, span, expr_ty, cast_ty }
106 impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCastError<'tcx> {
107 fn session(&self) -> &Session {
111 fn code(&self) -> DiagnosticId {
112 rustc_errors::error_code!(E0607)
115 fn common(&self) -> DiagnosticBuilder<'tcx> {
116 if self.expr_ty.references_error() {
117 self.sess.diagnostic().struct_dummy()
119 self.sess.struct_span_fatal_with_code(
122 "cannot cast thin pointer `{}` to fat pointer `{}`",
123 self.expr_ty, self.cast_ty
130 fn extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
132 "Thin pointers are \"simple\" pointers: they are purely a reference to a
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.
141 To fix this error, don't try to cast directly between thin and fat
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",