Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ try {
?>
--EXPECT--
A
Error: Call to undefined method B::who()
Error: Failed to create closure from undefined method B::who()
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions Zend/tests/first_class_callable/first_class_callable_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ try {
?>
--EXPECT--
Error: Value of type int is not callable
Error: Call to 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 function does_not_exist()
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()
57 changes: 57 additions & 0 deletions Zend/tests/first_class_callable/gh8623.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
GH-8623: Error message when creating first-class callable from undefined function
--FILE--
<?php

namespace Foo;

try {
$f = 'missing'(...);
} catch (\Error $e) {
echo $e->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()
26 changes: 24 additions & 2 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -1289,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);
}
Expand All @@ -1298,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);
}
Expand All @@ -1307,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;
}
Expand Down
34 changes: 27 additions & 7 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -5138,7 +5153,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;
Expand Down Expand Up @@ -5194,7 +5209,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;
Expand Down
20 changes: 15 additions & 5 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
/* }}} */

Expand Down
10 changes: 8 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)) {
Expand Down
Loading
Loading