Skip to content

Allow named args after unpack #7009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions Zend/tests/named_params/unpack_and_named_1.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
--TEST--
Mixing unpacking and named params (1)
Named args after unpacking (supported)
--FILE--
<?php

test(...[], a: 42);
function test(...$args) {
var_dump($args);
}

test(...[1, 2], a: 3);
test(...[1, 'a' => 2], b: 3);

function test2($a, $b, $c = 3, $d = 4) {
var_dump($a, $b, $c, $d);
}

test2(...[1, 2], d: 40);
test2(...['b' => 2, 'a' => 1], d: 40);

try {
test2(...[1, 2], b: 20);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
test2(...[1, 'b' => 2], b: 20);
Copy link
Contributor

@mvorisek mvorisek May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about ...[1, 'b' => 2], 20, is it tested anywhere?

Copy link
Member Author

@nikic nikic May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positional arguments after unpacking are forbidden in general (no relation to named args).

} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
Fatal error: Cannot combine named arguments and argument unpacking in %s on line %d
--EXPECT--
array(3) {
[0]=>
int(1)
[1]=>
int(2)
["a"]=>
int(3)
}
array(3) {
[0]=>
int(1)
["a"]=>
int(2)
["b"]=>
int(3)
}
int(1)
int(2)
int(3)
int(40)
int(1)
int(2)
int(3)
int(40)
Named parameter $b overwrites previous argument
Named parameter $b overwrites previous argument
4 changes: 2 additions & 2 deletions Zend/tests/named_params/unpack_and_named_2.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
--TEST--
Mixing unpacking and named params (2)
Named args before unpacking (not supported)
--FILE--
<?php

test(a: 42, ...[]);

?>
--EXPECTF--
Fatal error: Cannot combine named arguments and argument unpacking in %s on line %d
Fatal error: Cannot use argument unpacking after named arguments in %s on line %d
9 changes: 2 additions & 7 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3472,7 +3472,7 @@ uint32_t zend_compile_args(
if (arg->kind == ZEND_AST_UNPACK) {
if (uses_named_args) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot combine named arguments and argument unpacking");
"Cannot use argument unpacking after named arguments");
}

uses_arg_unpack = 1;
Expand All @@ -3492,16 +3492,11 @@ uint32_t zend_compile_args(
}

if (arg->kind == ZEND_AST_NAMED_ARG) {
if (uses_arg_unpack) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot combine named arguments and argument unpacking");
}

uses_named_args = 1;
arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
arg = arg->child[1];

if (fbc) {
if (fbc && !uses_arg_unpack) {
arg_num = zend_get_arg_num(fbc, arg_name);
if (arg_num == arg_count + 1 && !may_have_undef) {
/* Using named arguments, but passing in order. */
Expand Down