Skip to content

Commit 29eab39

Browse files
authored
changed error messages when number of arguments too few or too many (#1450)
* add new info in logtext, change tests * minifix * add escapes * minifix tests
1 parent b27c8f2 commit 29eab39

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

compiler/pipes/check-func-calls-and-vararg.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,14 @@ VertexPtr CheckFuncCallsAndVarargPass::on_func_call(VertexAdaptor<op_func_call>
279279

280280
if (f->is_extern() && func_param->type_hint && call_arg->type() == op_callback_of_builtin) {
281281
if (FunctionPtr f_callback = call_arg.as<op_callback_of_builtin>()->func_id) {
282-
int call_n_params = func_param->type_hint->try_as<TypeHintCallable>()->arg_types.size() + call_arg.as<op_callback_of_builtin>()->args().size();
282+
const auto *typed_callable = func_param->type_hint->try_as<TypeHintCallable>();
283+
int call_n_params = typed_callable->arg_types.size() + call_arg.as<op_callback_of_builtin>()->args().size();
283284
int delta_this = f_callback->has_implicit_this_arg() ? 1 : 0;
285+
auto expected_callback_signature = typed_callable->as_human_readable();
284286
kphp_error(call_n_params >= f_callback->get_min_argn(),
285-
fmt_format("Too few arguments in callback, expected {}, have {}", f_callback->get_min_argn() - delta_this, call_n_params - delta_this));
287+
fmt_format("Too many arguments for callback ({}), expected {}, have {}", expected_callback_signature, call_n_params - delta_this, f_callback->get_min_argn() - delta_this));
286288
kphp_error(f_callback->get_params().size() >= call_n_params,
287-
fmt_format("Too many arguments in callback, expected {}, have {}", f_callback->get_params().size() - delta_this, call_n_params - delta_this));
289+
fmt_format("Too few arguments for callback ({}), expected {}, have {}", expected_callback_signature, call_n_params - delta_this, f_callback->get_params().size() - delta_this));
288290

289291
for (auto p : f_callback->get_params()) {
290292
kphp_error(!p.as<op_func_param>()->var()->ref_flag, "You can't pass callbacks with &references to built-in functions");

tests/phpt/ffi/typing/070_callback_params_error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@kphp_should_fail k2_skip
2-
/Too few arguments in callback, expected 1, have 0/
2+
/Too many arguments for callback \(callable\(\): void\), expected 0, have 1/
33
<?php
44

55
function test() {

tests/phpt/ffi/typing/071_callback_params_error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@kphp_should_fail k2_skip
2-
/Too many arguments in callback, expected 0, have 1/
2+
/Too few arguments for callback \(callable\(int\): void\), expected 1, have 0/
33
<?php
44

55
function test() {

0 commit comments

Comments
 (0)