Skip to content

Commit b895d42

Browse files
committed
Require Closures in const-expr to be static
1 parent 9c29baf commit b895d42

9 files changed

+15
-13
lines changed

Zend/tests/closure_const_expr/attributes.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class Attr {
1212
}
1313
}
1414

15-
#[Attr(function () {
16-
17-
})]
18-
#[Attr(function (...$args) {
15+
#[Attr(static function () { })]
16+
#[Attr(static function (...$args) {
1917
var_dump($args);
2018
})]
2119
class C {}

Zend/tests/closure_const_expr/basic.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Allow defining Closures in const expressions.
33
--FILE--
44
<?php
55

6-
const Closure = function () {
6+
const Closure = static function () {
77
echo "called", PHP_EOL;
88
};
99

Zend/tests/closure_const_expr/class_const.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Allow defining Closures in class constants.
44
<?php
55

66
class C {
7-
const Closure = function () {
7+
const Closure = static function () {
88
echo "called", PHP_EOL;
99
};
1010
}

Zend/tests/closure_const_expr/default_args.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Closures in default argument
44
<?php
55

66
function test(
7-
Closure $name = function () {
7+
Closure $name = static function () {
88
echo "default", PHP_EOL;
99
},
1010
) {

Zend/tests/closure_const_expr/disallows_using_variables.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Disallows using variables.
55

66
$foo = "bar";
77

8-
const Closure = function () use ($foo) {
8+
const Closure = static function () use ($foo) {
99
echo $foo, PHP_EOL;
1010
};
1111

@@ -14,4 +14,4 @@ var_dump(Closure);
1414

1515
?>
1616
--EXPECTF--
17-
Fatal error: Cannot use(...) variables in constant expression in %s on line %d
17+
Fatal error: Cannot "use(...)" variables in constant expression in %s on line %d

Zend/tests/closure_const_expr/property_initializer.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Closure in property initializer
44
<?php
55

66
class C {
7-
public Closure $d = function () {
7+
public Closure $d = static function () {
88
echo "called", PHP_EOL;
99
};
1010
}

Zend/tests/closure_const_expr/static_initalizer.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Closure in static initializer
44
<?php
55

66
function foo() {
7-
static $closure = function () {
7+
static $closure = static function () {
88
echo "called", PHP_EOL;
99
};
1010

Zend/tests/closure_const_expr/static_property_initializer.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Closure in static property initializer
44
<?php
55

66
class C {
7-
public static Closure $d = function () {
7+
public static Closure $d = static function () {
88
echo "called", PHP_EOL;
99
};
1010
}

Zend/zend_compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11169,9 +11169,13 @@ static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
1116911169
{
1117011170
zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
1117111171
zend_ast *uses_ast = closure_ast->child[1];
11172+
if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11173+
zend_error_noreturn(E_COMPILE_ERROR,
11174+
"Closures in constant expressions must be \"static\"");
11175+
}
1117211176
if (uses_ast) {
1117311177
zend_error_noreturn(E_COMPILE_ERROR,
11174-
"Cannot use(...) variables in constant expression");
11178+
"Cannot \"use(...)\" variables in constant expression");
1117511179
}
1117611180

1117711181
znode node;

0 commit comments

Comments
 (0)