]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs
Rollup merge of #103236 - tspiteri:redoc-int-adc-sbb, r=m-ou-se
[rust.git] / compiler / rustc_hir_analysis / src / structured_errors / missing_cast_for_variadic_arg.rs
1 use crate::structured_errors::StructuredDiagnostic;
2 use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
3 use rustc_middle::ty::{Ty, TypeVisitable};
4 use rustc_session::Session;
5 use rustc_span::Span;
6
7 pub struct MissingCastForVariadicArg<'tcx, 's> {
8     pub sess: &'tcx Session,
9     pub span: Span,
10     pub ty: Ty<'tcx>,
11     pub cast_ty: &'s 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, ErrorGuaranteed> {
24         let mut err = self.sess.struct_span_err_with_code(
25             self.span,
26             &format!("can't pass `{}` to variadic function", self.ty),
27             self.code(),
28         );
29
30         if self.ty.references_error() {
31             err.downgrade_to_delayed_bug();
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(
49         &self,
50         mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
51     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
52         err.note(&format!(
53             "certain types, like `{}`, must be casted before passing them to a \
54                 variadic function, because of arcane ABI rules dictated by the C \
55                 standard",
56             self.ty
57         ));
58
59         err
60     }
61 }