Skip to content

Commit 883ef2e

Browse files
author
Orion Gonzalez
committed
Improve the diagnostic of fn item in variadic fn
1 parent 5f8a240 commit 883ef2e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ hir_typeck_field_multiply_specified_in_initializer =
7979
.label = used more than once
8080
.previous_use_label = first use of `{$ident}`
8181
82+
hir_typeck_fn_item_to_variadic_function = can't pass a function item to a variadic function
83+
.suggestion = use a function pointer instead
84+
.help = a function item is zero-sized and needs to be casted into a pointer-sized function pointer to be used in FFI
85+
.note = for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
86+
8287
hir_typeck_fru_expr = this expression does not end in a comma...
8388
hir_typeck_fru_expr2 = ... so this is interpreted as a `..` range expression, instead of functional record update syntax
8489
hir_typeck_fru_note = this expression may have been misinterpreted as a `..` range expression

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,13 @@ pub(crate) struct PassToVariadicFunction<'a, 'tcx> {
797797
#[note(hir_typeck_teach_help)]
798798
pub(crate) teach: bool,
799799
}
800+
801+
#[derive(Diagnostic)]
802+
#[diag(hir_typeck_fn_item_to_variadic_function, code = E0617)]
803+
pub(crate) struct PassFnItemToVariadicFunction {
804+
#[primary_span]
805+
pub span: Span,
806+
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
807+
pub sugg_span: Option<Span>,
808+
pub replace: String,
809+
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
472472
variadic_error(tcx.sess, arg.span, arg_ty, "c_uint");
473473
}
474474
ty::FnDef(..) => {
475-
let ptr_ty = Ty::new_fn_ptr(self.tcx, arg_ty.fn_sig(self.tcx));
476-
let ptr_ty = self.resolve_vars_if_possible(ptr_ty);
477-
variadic_error(tcx.sess, arg.span, arg_ty, &ptr_ty.to_string());
475+
let sess = tcx.sess;
476+
let span = arg.span;
477+
let (sugg_span, replace) =
478+
if let Ok(snippet) = sess.source_map().span_to_snippet(span) {
479+
(Some(span), format!("&{snippet}"))
480+
} else {
481+
(None, "".to_string())
482+
};
483+
484+
sess.dcx().emit_err(errors::PassFnItemToVariadicFunction {
485+
span,
486+
replace,
487+
sugg_span,
488+
});
478489
}
479490
_ => {}
480491
}

0 commit comments

Comments
 (0)