Skip to content

Commit c83d5bd

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

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-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 cast into a 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 = " as {replace}", applicability = "machine-applicable")]
807+
pub sugg_span: Span,
808+
pub replace: String,
809+
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,15 @@ 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 fn_ptr = Ty::new_fn_ptr(self.tcx, arg_ty.fn_sig(self.tcx));
476+
let fn_ptr = self.resolve_vars_if_possible(fn_ptr).to_string();
477+
478+
let fn_item_span = arg.span;
479+
tcx.sess.dcx().emit_err(errors::PassFnItemToVariadicFunction {
480+
span: fn_item_span,
481+
sugg_span: fn_item_span.shrink_to_hi(),
482+
replace: fn_ptr,
483+
});
478484
}
479485
_ => {}
480486
}

0 commit comments

Comments
 (0)