From a064bfc0efef9e279ea98686f4ed2aaba67fc1c9 Mon Sep 17 00:00:00 2001 From: Guilherme Pires Altmann Date: Sun, 22 Nov 2026 00:04:04 -0300 Subject: [PATCH 1/3] Fixed bug GH-8623 (Improve first-class callable error for undefined function) --- NEWS | 2 + .../error_namespace_no_fallback_001.phpt | 2 +- .../error_namespace_no_fallback_002.phpt | 2 +- .../constexpr/error_unknown_function.phpt | 2 +- .../first_class_callable_errors.phpt | 2 +- Zend/tests/first_class_callable/gh8623.phpt | 57 +++++++++++++++++++ Zend/zend_ast.c | 8 ++- Zend/zend_execute.c | 9 ++- Zend/zend_vm_def.h | 10 +++- Zend/zend_vm_execute.h | 32 ++++++++--- .../array_map_foreach_optimization_010.phpt | 2 +- 11 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 Zend/tests/first_class_callable/gh8623.phpt diff --git a/NEWS b/NEWS index eaeb9894e2b2..b41509406e70 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ PHP NEWS ?? ??? ????, PHP 8.7.0alpha1 - Core: + . Fixed bug GH-8623 (Better error message when using first-class callable + syntax on undefined function). (Guilherme Pires Altmann) . A flat unkeyed list assignment from an array literal with one value per target, such as [$a, $b] = [$b, $a], no longer builds the array when the result is unused (GH-23048). (Artem Ukrainskiy) diff --git a/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_001.phpt b/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_001.phpt index b80a222c2941..4ab46311b065 100644 --- a/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_001.phpt +++ b/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_001.phpt @@ -12,7 +12,7 @@ var_dump((Closure)("abc")); ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to undefined function Foo\strrev() in %s:%d +Fatal error: Uncaught Error: Failed to create closure from undefined function Foo\strrev() in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_002.phpt b/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_002.phpt index 62bc7e9e0b8b..72a6e5bed54b 100644 --- a/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_002.phpt +++ b/Zend/tests/first_class_callable/constexpr/error_namespace_no_fallback_002.phpt @@ -12,7 +12,7 @@ var_dump((Closure)("abc")); ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to undefined function Foo\strrev() in %s:%d +Fatal error: Uncaught Error: Failed to create closure from undefined function Foo\strrev() in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/first_class_callable/constexpr/error_unknown_function.phpt b/Zend/tests/first_class_callable/constexpr/error_unknown_function.phpt index 38b291d9907a..e2223a1ca2bc 100644 --- a/Zend/tests/first_class_callable/constexpr/error_unknown_function.phpt +++ b/Zend/tests/first_class_callable/constexpr/error_unknown_function.phpt @@ -9,7 +9,7 @@ var_dump(Closure); ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to undefined function this_function_does_not_exist() in %s:%d +Fatal error: Uncaught Error: Failed to create closure from undefined function this_function_does_not_exist() in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/first_class_callable/first_class_callable_errors.phpt b/Zend/tests/first_class_callable/first_class_callable_errors.phpt index 4e7503027b14..6391d7c596a4 100644 --- a/Zend/tests/first_class_callable/first_class_callable_errors.phpt +++ b/Zend/tests/first_class_callable/first_class_callable_errors.phpt @@ -49,7 +49,7 @@ try { ?> --EXPECT-- Error: Value of type int is not callable -Error: Call to undefined function does_not_exist() +Error: Failed to create closure from undefined function does_not_exist() Error: Call to undefined method stdClass::doesNotExist() Error: Call to undefined method stdClass::doesNotExist() Error: Call to undefined method stdClass::doesNotExist() diff --git a/Zend/tests/first_class_callable/gh8623.phpt b/Zend/tests/first_class_callable/gh8623.phpt new file mode 100644 index 000000000000..d5027213e37c --- /dev/null +++ b/Zend/tests/first_class_callable/gh8623.phpt @@ -0,0 +1,57 @@ +--TEST-- +GH-8623: Error message when creating first-class callable from undefined function +--FILE-- +getMessage(), "\n"; +} +try { + $f = missing(...); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} +try { + $f = bar\missing(...); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} +$name = 'missing'; +try { + $f = $name(...); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +/* The error message for actual calls is unchanged. */ +try { + missing(); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} +try { + $name(); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +/* Partial applications are not affected for now. */ +try { + $f = missing(?); +} catch (\Error $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +Failed to create closure from undefined function missing() +Failed to create closure from undefined function Foo\missing() +Failed to create closure from undefined function Foo\bar\missing() +Failed to create closure from undefined function missing() +Call to undefined function Foo\missing() +Call to undefined function missing() +Call to undefined function Foo\missing() diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 9ef1cf3b6788..c70308721c6e 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -1261,7 +1261,13 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( } if (!fptr) { - zend_throw_error(NULL, "Call to undefined function %pS()", function_name); + if (args->children == 1 + && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + function_name); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", function_name); + } return FAILURE; } diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index b166bf7dc75c..6df8f656422b 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -5138,7 +5138,7 @@ static void zend_swap_operands(zend_op *op) /* {{{ */ /* }}} */ #endif -static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_string *function, uint32_t num_args) /* {{{ */ +static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_string *function, uint32_t num_args, bool is_first_class_callable) /* {{{ */ { zend_function *fbc; zend_class_entry *called_scope; @@ -5194,7 +5194,12 @@ static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_s } else { fbc = zend_fetch_function(function); if (UNEXPECTED(fbc == NULL)) { - zend_throw_error(NULL, "Call to undefined function %pS()", function); + if (UNEXPECTED(is_first_class_callable)) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + function); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", function); + } return NULL; } called_scope = NULL; diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index bc9163d3ba03..1f52e2c4f4a5 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -1002,7 +1002,12 @@ ZEND_VM_COLD_HELPER(zend_undefined_function_helper, ANY, ANY) SAVE_OPLINE(); function_name = RT_CONSTANT(opline, opline->op2); - zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + if (UNEXPECTED((opline+1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + Z_STR_P(function_name)); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + } HANDLE_EXCEPTION(); } @@ -3933,7 +3938,8 @@ ZEND_VM_HANDLER(128, ZEND_INIT_DYNAMIC_CALL, ANY, CONST|TMP|CV, NUM) ZEND_VM_C_LABEL(try_function_name): if (OP2_TYPE != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (OP2_TYPE != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 4e2557f2086c..d21500dfe0a7 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -773,7 +773,12 @@ static zend_never_inline ZEND_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_F SAVE_OPLINE(); function_name = RT_CONSTANT(opline, opline->op2); - zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + if (UNEXPECTED((opline+1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + Z_STR_P(function_name)); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + } HANDLE_EXCEPTION(); } @@ -4121,7 +4126,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_INIT_DYNAMIC_ try_function_name: if (IS_CONST != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_CONST != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { @@ -4305,7 +4311,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_INIT_DYNAMIC_ try_function_name: if (IS_TMP_VAR != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_TMP_VAR != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { @@ -4503,7 +4510,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV ZEND_INIT_DYNAMIC_ try_function_name: if (IS_CV != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_CV != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { @@ -53804,7 +53812,12 @@ static zend_never_inline ZEND_COLD ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_C SAVE_OPLINE(); function_name = RT_CONSTANT(opline, opline->op2); - zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + if (UNEXPECTED((opline+1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + Z_STR_P(function_name)); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + } HANDLE_EXCEPTION(); } @@ -57000,7 +57013,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_INIT_DYNAMIC_CALL_ try_function_name: if (IS_CONST != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_CONST != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { @@ -57184,7 +57198,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_INIT_DYNAMIC_CALL_ try_function_name: if (IS_TMP_VAR != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_TMP_VAR != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { @@ -57382,7 +57397,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV ZEND_INIT_DYNAMIC_CALL_ try_function_name: if (IS_CV != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_STRING)) { - call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value); + call = zend_init_dynamic_call_string(Z_STR_P(function_name), opline->extended_value, + (opline+1)->opcode == ZEND_CALLABLE_CONVERT); } else if (IS_CV != IS_CONST && EXPECTED(Z_TYPE_P(function_name) == IS_OBJECT)) { call = zend_init_dynamic_call_object(Z_OBJ_P(function_name), opline->extended_value); } else if (EXPECTED(Z_TYPE_P(function_name) == IS_ARRAY)) { diff --git a/ext/opcache/tests/array_map_foreach_optimization_010.phpt b/ext/opcache/tests/array_map_foreach_optimization_010.phpt index 758c3504a88f..4d0dc6bdf430 100644 --- a/ext/opcache/tests/array_map_foreach_optimization_010.phpt +++ b/ext/opcache/tests/array_map_foreach_optimization_010.phpt @@ -85,7 +85,7 @@ empty direct array(0) { } empty dynamic-call control -Error: Call to undefined function cand_86013_missing() +Error: Failed to create closure from undefined function cand_86013_missing() direct array_map static call array(1) { [0]=> From cd8de8573a9d225c054281c66aec9f5ceed3a93e Mon Sep 17 00:00:00 2001 From: Guilherme Pires Altmann Date: Sun, 22 Nov 2026 00:56:10 -0300 Subject: [PATCH 2/3] Handle first-class callable of undefined function in JIT The JIT has its own error emission path for unresolved function calls: in function JIT mode, the guard emitted by zend_jit_init_fcall() jumps to the zend_jit_undefined_function_stub, which threw a hardcoded "Call to undefined function %s()" message, ignoring the ZEND_CALLABLE_CONVERT opcode that follows the failed INIT_FCALL for first-class callable syntax. Replace the inline zend_throw_error() call with a new zend_jit_undefined_function() helper (zend_jit_helpers.c) that mirrors the check introduced for zend_undefined_function_helper(), so the JIT also reports "Failed to create closure from undefined function %s()" when appropriate. --- ext/opcache/jit/zend_jit_helpers.c | 14 ++++++++++++++ ext/opcache/jit/zend_jit_ir.c | 22 +++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ext/opcache/jit/zend_jit_helpers.c b/ext/opcache/jit/zend_jit_helpers.c index 97136a5cd112..8b7af1691751 100644 --- a/ext/opcache/jit/zend_jit_helpers.c +++ b/ext/opcache/jit/zend_jit_helpers.c @@ -94,6 +94,20 @@ static zend_function* ZEND_FASTCALL zend_jit_find_ns_func_helper(zval *func_name return fbc; } +static ZEND_COLD void ZEND_FASTCALL zend_jit_undefined_function(void) +{ + zend_execute_data *execute_data = EG(current_execute_data); + const zend_op *opline = EX(opline); + zval *function_name = RT_CONSTANT(opline, opline->op2); + + if (UNEXPECTED((opline + 1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from undefined function %pS()", + Z_STR_P(function_name)); + } else { + zend_throw_error(NULL, "Call to undefined function %pS()", Z_STR_P(function_name)); + } +} + static ZEND_COLD void ZEND_FASTCALL zend_jit_invalid_method_call(zval *object) { zend_execute_data *execute_data = EG(current_execute_data); diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c index f71fd287ee25..1d521159b335 100644 --- a/ext/opcache/jit/zend_jit_ir.c +++ b/ext/opcache/jit/zend_jit_ir.c @@ -2164,23 +2164,10 @@ static int zend_jit_invalid_this_stub(zend_jit_ctx *jit) static int zend_jit_undefined_function_stub(zend_jit_ctx *jit) { - // JIT: load EX(opline) - ir_ref ref = ir_LOAD_A(jit_FP(jit)); - ir_ref arg3 = ir_LOAD_U32(ir_ADD_OFFSET(ref, offsetof(zend_op, op2.constant))); - - if (sizeof(void*) == 8) { - arg3 = ir_LOAD_A(ir_ADD_A(ref, ir_SEXT_A(arg3))); - } else { - arg3 = ir_LOAD_A(arg3); - } - arg3 = ir_ADD_OFFSET(arg3, offsetof(zend_string, val)); - - ir_CALL_3(IR_VOID, - ir_CONST_FUNC_PROTO(zend_throw_error, - ir_proto_2(&jit->ctx, IR_VARARG_FUNC, IR_VOID, IR_ADDR, IR_ADDR)), - IR_NULL, - ir_CONST_ADDR("Call to undefined function %s()"), - arg3); + // JIT: zend_jit_undefined_function() may check the next opcode + // (ZEND_CALLABLE_CONVERT) to provide a better error message for + // first-class callable syntax of undefined functions. + ir_CALL(IR_VOID, ir_CONST_FC_FUNC(zend_jit_undefined_function)); ir_IJMP(jit_STUB_ADDR(jit, jit_stub_exception_handler)); @@ -3111,6 +3098,7 @@ static void zend_jit_setup_disasm(void) REGISTER_HELPER(zend_jit_init_func_run_time_cache_helper); REGISTER_HELPER(zend_jit_find_func_helper); REGISTER_HELPER(zend_jit_find_ns_func_helper); + REGISTER_HELPER(zend_jit_undefined_function); REGISTER_HELPER(zend_jit_jmp_frameless_helper); REGISTER_HELPER(zend_jit_unref_helper); REGISTER_HELPER(zend_jit_invalid_method_call); From e98fbe1da62ac13a443de23dec7c4eb493ad3aaa Mon Sep 17 00:00:00 2001 From: Guilherme Pires Altmann Date: Sun, 22 Nov 2026 10:31:42 -0300 Subject: [PATCH 3/3] Improve error message for first-class callable of undefined method Extend GH-8623 to method calls: creating a first-class callable from an undefined, non-public or non-static method now reports "Failed to create closure from ..." instead of the call-centric "Call to ..." message. The check is implemented in the shared error emitters (zend_undefined_method(), zend_non_static_method_call() and zend_bad_method_call()) by inspecting the opcode that follows the failing INIT_*CALL, which covers the interpreter, the dynamic call helpers and the JIT without regenerating the VM templates. The constant expression paths in zend_ast.c emit the improved message directly when the placeholder-variadic FCC syntax is detected. Non-FCC error messages are unchanged. --- NEWS | 2 +- .../constexpr/attributes_scope_002.phpt | 2 +- .../error_static_call_instance_method.phpt | 2 +- .../constexpr/error_unknown_method.phpt | 2 +- .../property_initializer_scope_002.phpt | 2 +- .../constexpr/trait_scope_004.phpt | 2 +- .../constexpr/trait_scope_005.phpt | 2 +- .../first_class_callable_errors.phpt | 10 ++++---- Zend/zend_ast.c | 18 ++++++++++++- Zend/zend_execute.c | 25 +++++++++++++++---- Zend/zend_object_handlers.c | 20 +++++++++++---- 11 files changed, 64 insertions(+), 23 deletions(-) diff --git a/NEWS b/NEWS index b41509406e70..378691ec993e 100644 --- a/NEWS +++ b/NEWS @@ -4,7 +4,7 @@ PHP NEWS - Core: . Fixed bug GH-8623 (Better error message when using first-class callable - syntax on undefined function). (Guilherme Pires Altmann) + syntax on undefined function or method). (Guilherme Pires Altmann) . A flat unkeyed list assignment from an array literal with one value per target, such as [$a, $b] = [$b, $a], no longer builds the array when the result is unused (GH-23048). (Artem Ukrainskiy) diff --git a/Zend/tests/first_class_callable/constexpr/attributes_scope_002.phpt b/Zend/tests/first_class_callable/constexpr/attributes_scope_002.phpt index f93240c5fac8..d49207161f87 100644 --- a/Zend/tests/first_class_callable/constexpr/attributes_scope_002.phpt +++ b/Zend/tests/first_class_callable/constexpr/attributes_scope_002.phpt @@ -27,7 +27,7 @@ foreach ((new ReflectionClass(C::class))->getAttributes() as $reflectionAttribut ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to private method E::myMethod() from scope C in %s:%d +Fatal error: Uncaught Error: Failed to create closure from private method E::myMethod() from scope C in %s:%d Stack trace: #0 %s(%d): ReflectionAttribute->newInstance() #1 {main} diff --git a/Zend/tests/first_class_callable/constexpr/error_static_call_instance_method.phpt b/Zend/tests/first_class_callable/constexpr/error_static_call_instance_method.phpt index 9a6b27b232e6..04867c5fc3b4 100644 --- a/Zend/tests/first_class_callable/constexpr/error_static_call_instance_method.phpt +++ b/Zend/tests/first_class_callable/constexpr/error_static_call_instance_method.phpt @@ -17,7 +17,7 @@ var_dump(Closure); ?> --EXPECTF-- -Fatal error: Uncaught Error: Non-static method Foo::myMethod() cannot be called statically in %s:%d +Fatal error: Uncaught Error: Failed to create closure from non-static method Foo::myMethod() in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/first_class_callable/constexpr/error_unknown_method.phpt b/Zend/tests/first_class_callable/constexpr/error_unknown_method.phpt index 345d67ee74da..c279ba428d46 100644 --- a/Zend/tests/first_class_callable/constexpr/error_unknown_method.phpt +++ b/Zend/tests/first_class_callable/constexpr/error_unknown_method.phpt @@ -11,7 +11,7 @@ var_dump(Closure); ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to undefined method ThisClassDoesExist::thisMethodDoesNotExist() in %s:%d +Fatal error: Uncaught Error: Failed to create closure from undefined method ThisClassDoesExist::thisMethodDoesNotExist() in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/first_class_callable/constexpr/property_initializer_scope_002.phpt b/Zend/tests/first_class_callable/constexpr/property_initializer_scope_002.phpt index ecee256012ec..c721610ff975 100644 --- a/Zend/tests/first_class_callable/constexpr/property_initializer_scope_002.phpt +++ b/Zend/tests/first_class_callable/constexpr/property_initializer_scope_002.phpt @@ -20,7 +20,7 @@ var_dump(($c->d)("abc")); ?> --EXPECTF-- -Fatal error: Uncaught Error: Call to private method E::myMethod() from scope C in %s:%d +Fatal error: Uncaught Error: Failed to create closure from private method E::myMethod() from scope C in %s:%d Stack trace: #0 %s(%d): [constant expression]() #1 {main} diff --git a/Zend/tests/first_class_callable/constexpr/trait_scope_004.phpt b/Zend/tests/first_class_callable/constexpr/trait_scope_004.phpt index da1608e2d918..89bdff2a63d6 100644 --- a/Zend/tests/first_class_callable/constexpr/trait_scope_004.phpt +++ b/Zend/tests/first_class_callable/constexpr/trait_scope_004.phpt @@ -19,4 +19,4 @@ try { ?> --EXPECT-- A -Error: Call to undefined method B::who() +Error: Failed to create closure from undefined method B::who() diff --git a/Zend/tests/first_class_callable/constexpr/trait_scope_005.phpt b/Zend/tests/first_class_callable/constexpr/trait_scope_005.phpt index 50382a449939..80712afac701 100644 --- a/Zend/tests/first_class_callable/constexpr/trait_scope_005.phpt +++ b/Zend/tests/first_class_callable/constexpr/trait_scope_005.phpt @@ -19,4 +19,4 @@ try { ?> --EXPECT-- A -Error: Call to private method A::who() from scope B +Error: Failed to create closure from private method A::who() from scope B diff --git a/Zend/tests/first_class_callable/first_class_callable_errors.phpt b/Zend/tests/first_class_callable/first_class_callable_errors.phpt index 6391d7c596a4..e5c1d17960b2 100644 --- a/Zend/tests/first_class_callable/first_class_callable_errors.phpt +++ b/Zend/tests/first_class_callable/first_class_callable_errors.phpt @@ -50,8 +50,8 @@ try { --EXPECT-- Error: Value of type int is not callable Error: Failed to create closure from undefined function does_not_exist() -Error: Call to undefined method stdClass::doesNotExist() -Error: Call to undefined method stdClass::doesNotExist() -Error: Call to undefined method stdClass::doesNotExist() -Error: Call to private method Test::privateMethod() from global scope -Error: Non-static method Test::instanceMethod() cannot be called statically +Error: Failed to create closure from undefined method stdClass::doesNotExist() +Error: Failed to create closure from undefined method stdClass::doesNotExist() +Error: Failed to create closure from undefined method stdClass::doesNotExist() +Error: Failed to create closure from private method Test::privateMethod() from global scope +Error: Failed to create closure from non-static method Test::instanceMethod() diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index c70308721c6e..3f326a0d53ef 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -1295,6 +1295,12 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( if (!zend_check_method_accessible(fptr, scope)) { if (ce->__callstatic) { zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions"); + } else if (args->children == 1 + && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { + zend_throw_error(NULL, "Failed to create closure from %s method %pS::%pS() from %s%pS", + zend_visibility_string(fptr->common.fn_flags), fptr->common.scope->name, method_name, + scope ? "scope " : "global scope", + scope ? scope->name : ZSTR_EMPTY_ALLOC()); } else { zend_bad_method_call(fptr, method_name, scope); } @@ -1304,6 +1310,10 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( } else { if (ce->__callstatic) { zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions"); + } else if (args->children == 1 + && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { + zend_throw_error(NULL, "Failed to create closure from undefined method %pS::%pS()", + ce->name, method_name); } else { zend_undefined_method(ce, method_name); } @@ -1313,7 +1323,13 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( } if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) { - zend_non_static_method_call(fptr); + if (args->children == 1 + && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { + zend_throw_error(NULL, "Failed to create closure from non-static method %pS::%pS()", + fptr->common.scope->name, fptr->common.function_name); + } else { + zend_non_static_method_call(fptr); + } return FAILURE; } diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 6df8f656422b..6b53b917e4a8 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -2563,7 +2563,13 @@ ZEND_API ZEND_COLD zval* ZEND_FASTCALL zend_undefined_index_write(HashTable *ht, ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_method(const zend_class_entry *ce, const zend_string *method) { - zend_throw_error(NULL, "Call to undefined method %pS::%pS()", ce->name, method); + zend_execute_data *execute_data = EG(current_execute_data); + + if (UNEXPECTED(execute_data != NULL && (EX(opline) + 1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from undefined method %pS::%pS()", ce->name, method); + } else { + zend_throw_error(NULL, "Call to undefined method %pS::%pS()", ce->name, method); + } } static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(const zval *object, const zval *function_name) @@ -2574,10 +2580,19 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(c ZEND_API zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_non_static_method_call(const zend_function *fbc) { - zend_throw_error( - zend_ce_error, - "Non-static method %pS::%pS() cannot be called statically", - fbc->common.scope->name, fbc->common.function_name); + zend_execute_data *execute_data = EG(current_execute_data); + + if (UNEXPECTED(execute_data != NULL && (EX(opline) + 1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error( + zend_ce_error, + "Failed to create closure from non-static method %pS::%pS()", + fbc->common.scope->name, fbc->common.function_name); + } else { + zend_throw_error( + zend_ce_error, + "Non-static method %pS::%pS() cannot be called statically", + fbc->common.scope->name, fbc->common.function_name); + } } ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(const zend_function *func, uint32_t arg_num) diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index 90f8c841b47e..459b84389ae8 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -1967,11 +1967,21 @@ ZEND_API zend_function *zend_get_property_hook_trampoline( ZEND_API ZEND_COLD zend_never_inline void zend_bad_method_call(const zend_function *fbc, const zend_string *method_name, const zend_class_entry *scope) /* {{{ */ { - zend_throw_error(NULL, "Call to %s method %pS::%pS() from %s%pS", - zend_visibility_string(fbc->common.fn_flags), fbc->common.scope->name, method_name, - scope ? "scope " : "global scope", - scope ? scope->name : ZSTR_EMPTY_ALLOC() - ); + zend_execute_data *execute_data = EG(current_execute_data); + + if (UNEXPECTED(execute_data != NULL && (EX(opline) + 1)->opcode == ZEND_CALLABLE_CONVERT)) { + zend_throw_error(NULL, "Failed to create closure from %s method %pS::%pS() from %s%pS", + zend_visibility_string(fbc->common.fn_flags), fbc->common.scope->name, method_name, + scope ? "scope " : "global scope", + scope ? scope->name : ZSTR_EMPTY_ALLOC() + ); + } else { + zend_throw_error(NULL, "Call to %s method %pS::%pS() from %s%pS", + zend_visibility_string(fbc->common.fn_flags), fbc->common.scope->name, method_name, + scope ? "scope " : "global scope", + scope ? scope->name : ZSTR_EMPTY_ALLOC() + ); + } } /* }}} */