]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_typeck / src / structured_errors / missing_cast_for_variadic_arg.rs
CommitLineData
5869c6ff 1use crate::structured_errors::StructuredDiagnostic;
5e7ed085 2use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
064997fb 3use rustc_middle::ty::{Ty, TypeVisitable};
5869c6ff
XL
4use rustc_session::Session;
5use rustc_span::Span;
6
923072b8 7pub struct MissingCastForVariadicArg<'tcx, 's> {
5869c6ff
XL
8 pub sess: &'tcx Session,
9 pub span: Span,
10 pub ty: Ty<'tcx>,
923072b8 11 pub cast_ty: &'s str,
5869c6ff
XL
12}
13
923072b8 14impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
5869c6ff
XL
15 fn session(&self) -> &Session {
16 self.sess
17 }
18
19 fn code(&self) -> DiagnosticId {
20 rustc_errors::error_code!(E0617)
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!("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 }
5869c6ff
XL
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
5e7ed085
FG
48 fn diagnostic_extended(
49 &self,
50 mut err: DiagnosticBuilder<'tcx, ErrorGuaranteed>,
51 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
5869c6ff
XL
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}