Add tests for list() in assignment in array literals

Array literals will constant evaluate their elements. These can include
assignments, even though these are not valid constant expressions. The lhs of
assignments can be a list() element (or []) which is parsed as an array with a
special flag.
This commit is contained in:
Ilija Tovilo 2023-05-25 23:39:48 +02:00
parent 58a191c659
commit 8ed66b4347
3 changed files with 48 additions and 0 deletions

28
Zend/tests/gh11320_1.phpt Normal file
View File

@ -0,0 +1,28 @@
--TEST--
GH-11320: Array literals can contain list() assignments
--FILE--
<?php
$index = 1;
function getList() { return [2, 3]; }
var_dump([$index => list($x, $y) = getList()]);
var_dump([$index => [$x, $y] = getList()]);
?>
--EXPECT--
array(1) {
[1]=>
array(2) {
[0]=>
int(2)
[1]=>
int(3)
}
}
array(1) {
[1]=>
array(2) {
[0]=>
int(2)
[1]=>
int(3)
}
}

12
Zend/tests/gh11320_2.phpt Normal file
View File

@ -0,0 +1,12 @@
--TEST--
GH-11320: list() expressions can contain magic constants
--FILE--
<?php
[list(__FILE__ => $foo) = [__FILE__ => 'foo']];
var_dump($foo);
[[__FILE__ => $foo] = [__FILE__ => 'foo']];
var_dump($foo);
?>
--EXPECT--
string(3) "foo"
string(3) "foo"

View File

@ -0,0 +1,8 @@
--TEST--
GH-11320: list() must not appear as a standalone array element
--FILE--
<?php
[list($a)];
?>
--EXPECTF--
Fatal error: Cannot use list() as standalone expression in %s on line %d