Skip to content

Commit 93592ea

Browse files
jhdxrcmb69
authored andcommitted
Fix GH-9769: Misleading error message for unpacking of objects
Only arrays can be unpacked in constant expressions. Closes GH-9776.
1 parent 29f0f4e commit 93592ea

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PHP NEWS
99
README.REDIST.BINS file). (Akama Hitoshi)
1010
. Fixed bug GH-9650 (Can't initialize heap: [0x000001e7]). (Michael Voříšek)
1111
. Fixed potentially undefined behavior in Windows ftok(3) emulation. (cmb)
12+
. Fixed GH-9769 (Misleading error message for unpacking of objects). (jhdxr)
1213

1314
- Date:
1415
. Fixed bug GH-9699 (DateTimeImmutable::diff differences in 8.1.10 onwards -

Zend/tests/array_unpack/gh9769.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Unpacking arrays in constant expression
3+
--FILE--
4+
<?php
5+
6+
const A = [...[1, 2, 3]];
7+
const B = [...['a'=>1, 'b'=>2, 'c'=>3]];
8+
const C = [...new ArrayObject()];
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Uncaught Error: Only arrays can be unpacked in constant expression in %sgh9769.php:5
13+
Stack trace:
14+
#0 {main}
15+
thrown in %sgh9769.php on line 5

Zend/zend_ast.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,7 @@ static zend_result zend_ast_add_unpacked_element(zval *result, zval *expr) {
475475
return SUCCESS;
476476
}
477477

478-
/* Objects or references cannot occur in a constant expression. */
479-
zend_throw_error(NULL, "Only arrays and Traversables can be unpacked");
478+
zend_throw_error(NULL, "Only arrays can be unpacked in constant expression");
480479
return FAILURE;
481480
}
482481

0 commit comments

Comments
 (0)