Skip to content

Commit 628a8c6

Browse files
committed
fixup! Support first-class callables in const-expressions
1 parent b3a12db commit 628a8c6

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Allow defining FCC in const expressions in a namespace.
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
const Closure = \strrev(...);
9+
10+
var_dump(Closure);
11+
var_dump((Closure)("abc"));
12+
13+
?>
14+
--EXPECTF--
15+
object(Closure)#%d (2) {
16+
["function"]=>
17+
string(%d) "%s"
18+
["parameter"]=>
19+
array(1) {
20+
["$string"]=>
21+
string(10) "<required>"
22+
}
23+
}
24+
string(3) "cba"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Allow defining FCC in const expressions in a namespace with global function fallback.
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
const Closure = strrev(...);
9+
10+
var_dump(Closure);
11+
var_dump((Closure)("abc"));
12+
13+
?>
14+
--EXPECTF--
15+
object(Closure)#%d (2) {
16+
["function"]=>
17+
string(%d) "%s"
18+
["parameter"]=>
19+
array(1) {
20+
["$string"]=>
21+
string(10) "<required>"
22+
}
23+
}
24+
string(3) "cba"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Allow defining FCC in const expressions in a namespace with function matching a global function.
3+
--FILE--
4+
<?php
5+
6+
namespace Foo;
7+
8+
function strrev(string $value) {
9+
return 'not the global one';
10+
}
11+
12+
const Closure = strrev(...);
13+
14+
var_dump(Closure);
15+
var_dump((Closure)("abc"));
16+
17+
?>
18+
--EXPECTF--
19+
object(Closure)#%d (2) {
20+
["function"]=>
21+
string(%d) "%s"
22+
["parameter"]=>
23+
array(1) {
24+
["$value"]=>
25+
string(10) "<required>"
26+
}
27+
}
28+
string(18) "not the global one"

Zend/zend_compile.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11254,9 +11254,14 @@ static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
1125411254
switch ((*ast_ptr)->kind) {
1125511255
case ZEND_AST_CALL: {
1125611256
zend_ast *name_ast = (*ast_ptr)->child[0];
11257-
if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
11257+
zval *name_ast_zv;
11258+
if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(name_ast_zv = zend_ast_get_zval(name_ast)) != IS_STRING) {
1125811259
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
1125911260
}
11261+
bool is_fully_qualified;
11262+
zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11263+
zval_ptr_dtor_nogc(name_ast_zv);
11264+
ZVAL_STR(name_ast_zv, name);
1126011265
break;
1126111266
}
1126211267
case ZEND_AST_STATIC_CALL: {

0 commit comments

Comments
 (0)