Skip to content

Commit fb0da7c

Browse files
committed
enhance diagnostic emitted for E0617 in missing cast for variadic arg
1 parent 8ace7ea commit fb0da7c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{errors, structured_errors::StructuredDiagnostic};
2-
use rustc_errors::{codes::*, DiagnosticBuilder, ErrCode};
2+
use rustc_errors::{codes::*, Applicability, DiagnosticBuilder, ErrCode};
33
use rustc_middle::ty::{Ty, TypeVisitableExt};
44
use rustc_session::Session;
55
use rustc_span::Span;
@@ -41,6 +41,28 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
4141
err.downgrade_to_delayed_bug();
4242
}
4343

44+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
45+
if self.ty.is_fn() {
46+
err.span_suggestion(
47+
self.span,
48+
"cast the value into a function pointer",
49+
format!("{} as {}", snippet, self.cast_ty),
50+
Applicability::MachineApplicable,
51+
)
52+
.help("a function item is zero-sized and needs to be casted into a function pointer to be used in FFI")
53+
.note("for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html");
54+
} else {
55+
err.span_suggestion(
56+
self.span,
57+
&format!("cast the value to `{}`", self.cast_ty),
58+
format!("{} as {}", snippet, self.cast_ty),
59+
Applicability::MachineApplicable,
60+
);
61+
}
62+
} else {
63+
err.help(&format!("cast the value into `{}`", self.cast_ty));
64+
}
65+
4466
err
4567
}
4668

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
662662
if with_no_queries() {
663663
p!(print_def_path(def_id, args));
664664
} else {
665-
let sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args);
666-
p!(print(sig), " {{", print_value_path(def_id, args), "}}");
665+
p!(print(def_id), " {{individual function type for", print_value_path(def_id, args), "}}");
667666
}
668667
}
669668
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),

0 commit comments

Comments
 (0)