From: Nikita Popov Date: Mon, 25 Jan 2021 10:12:21 +0000 (+0100) Subject: Improve error message for leading comma in keyed list assignment X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=321bd6b1f466e193f25a9dfc16b73c327d60cda7;p=php Improve error message for leading comma in keyed list assignment Print "Cannot use empty array entries in keyed array assignment" instead of "Cannot mix keyed and unkeyed array entries in assignments" for a leading comma. --- diff --git a/Zend/tests/list_keyed_leading_comma.phpt b/Zend/tests/list_keyed_leading_comma.phpt new file mode 100644 index 0000000000..dcadcbf393 --- /dev/null +++ b/Zend/tests/list_keyed_leading_comma.phpt @@ -0,0 +1,10 @@ +--TEST-- +Leading comma in keyed list assignment +--FILE-- + $b] = [1, "a" => 2]; + +?> +--EXPECTF-- +Fatal error: Cannot use empty array entries in keyed array assignment in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index d1ea49342e..90ab92251a 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -2963,14 +2963,23 @@ static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */ } /* }}} */ +static bool list_is_keyed(zend_ast_list *list) +{ + for (uint32_t i = 0; i < list->children; i++) { + if (list->child[i]) { + return list->child[i]->child[1] != NULL; + } + } + return false; +} + static void zend_compile_list_assign( znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */ { zend_ast_list *list = zend_ast_get_list(ast); uint32_t i; bool has_elems = 0; - bool is_keyed = - list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL; + bool is_keyed = list_is_keyed(list); if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) { zval_make_interned_string(&expr_node->u.constant);