From 93592ea743d6161fc65f1f28e483f675618068d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9D=A6=E7=B1=B3?= Date: Thu, 17 Nov 2022 16:32:33 +0100 Subject: [PATCH] Fix GH-9769: Misleading error message for unpacking of objects Only arrays can be unpacked in constant expressions. Closes GH-9776. --- NEWS | 1 + Zend/tests/array_unpack/gh9769.phpt | 15 +++++++++++++++ Zend/zend_ast.c | 3 +-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Zend/tests/array_unpack/gh9769.phpt diff --git a/NEWS b/NEWS index 80f4f772683..ab7c416ed33 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,7 @@ PHP NEWS README.REDIST.BINS file). (Akama Hitoshi) . Fixed bug GH-9650 (Can't initialize heap: [0x000001e7]). (Michael Voříšek) . Fixed potentially undefined behavior in Windows ftok(3) emulation. (cmb) + . Fixed GH-9769 (Misleading error message for unpacking of objects). (jhdxr) - Date: . Fixed bug GH-9699 (DateTimeImmutable::diff differences in 8.1.10 onwards - diff --git a/Zend/tests/array_unpack/gh9769.phpt b/Zend/tests/array_unpack/gh9769.phpt new file mode 100644 index 00000000000..9eb8c6aea76 --- /dev/null +++ b/Zend/tests/array_unpack/gh9769.phpt @@ -0,0 +1,15 @@ +--TEST-- +Unpacking arrays in constant expression +--FILE-- +1, 'b'=>2, 'c'=>3]]; +const C = [...new ArrayObject()]; + +?> +--EXPECTF-- +Fatal error: Uncaught Error: Only arrays can be unpacked in constant expression in %sgh9769.php:5 +Stack trace: +#0 {main} + thrown in %sgh9769.php on line 5 diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index 07cb9c5fbb7..0e5e5ebac16 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -475,8 +475,7 @@ static zend_result zend_ast_add_unpacked_element(zval *result, zval *expr) { return SUCCESS; } - /* Objects or references cannot occur in a constant expression. */ - zend_throw_error(NULL, "Only arrays and Traversables can be unpacked"); + zend_throw_error(NULL, "Only arrays can be unpacked in constant expression"); return FAILURE; }