]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs
Rollup merge of #90532 - fee1-dead:improve-const-fn-err-msg, r=oli-obk
[rust.git] / compiler / rustc_typeck / src / structured_errors / missing_cast_for_variadic_arg.rs
1 use crate::structured_errors::StructuredDiagnostic;
2 use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId};
3 use rustc_middle::ty::{Ty, TypeFoldable};
4 use rustc_session::Session;
5 use rustc_span::Span;
6
7 pub struct MissingCastForVariadicArg<'tcx> {
8     pub sess: &'tcx Session,
9     pub span: Span,
10     pub ty: Ty<'tcx>,
11     pub cast_ty: &'tcx str,
12 }
13
14 impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx> {
15     fn session(&self) -> &Session {
16         self.sess
17     }
18
19     fn code(&self) -> DiagnosticId {
20         rustc_errors::error_code!(E0617)
21     }
22
23     fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx> {
24         let mut err = if self.ty.references_error() {
25             self.sess.diagnostic().struct_dummy()
26         } else {
27             self.sess.struct_span_fatal_with_code(
28                 self.span,
29                 &format!("can't pass `{}` to variadic function", self.ty),
30                 self.code(),
31             )
32         };
33
34         if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
35             err.span_suggestion(
36                 self.span,
37                 &format!("cast the value to `{}`", self.cast_ty),
38                 format!("{} as {}", snippet, self.cast_ty),
39                 Applicability::MachineApplicable,
40             );
41         } else {
42             err.help(&format!("cast the value to `{}`", self.cast_ty));
43         }
44
45         err
46     }
47
48     fn diagnostic_extended(&self, mut err: DiagnosticBuilder<'tcx>) -> DiagnosticBuilder<'tcx> {
49         err.note(&format!(
50             "certain types, like `{}`, must be casted before passing them to a \
51                 variadic function, because of arcane ABI rules dictated by the C \
52                 standard",
53             self.ty
54         ));
55
56         err
57     }
58 }