Skip to content

Commit

Permalink
Treat attribute argument lists like normal argument lists
Browse files Browse the repository at this point in the history
Allow trailing comma. Syntactically allow unpacking, but forbid it
during compilation.

The trailing comma test-case is adopted from GH-5796.
  • Loading branch information
nikic committed Jul 2, 2020
1 parent d8dfb21 commit 6a195ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
11 changes: 11 additions & 0 deletions Zend/tests/attributes/026_unpack_in_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Cannot use unpacking in attribute argument list
--FILE--
<?php

<<MyAttribute(...[1, 2, 3])>>
class Foo { }

?>
--EXPECTF--
Fatal error: Cannot use unpacking in attribute argument list in %s on line %d
30 changes: 30 additions & 0 deletions Zend/tests/attributes/027_trailing_comma_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Trailing comma in attribute argument list
--FILE--
<?php

<<MyAttribute(
"there",
"are",
"many",
"arguments",
)>>
class Foo { }

$ref = new \ReflectionClass(Foo::class);
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getArguments());

?>
--EXPECT--
string(11) "MyAttribute"
array(4) {
[0]=>
string(5) "there"
[1]=>
string(3) "are"
[2]=>
string(4) "many"
[3]=>
string(9) "arguments"
}
2 changes: 1 addition & 1 deletion Zend/tests/varSyntax/globalNonSimpleVariableError.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ global $$foo->bar;

?>
--EXPECTF--
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in %s on line %d
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ';' or ',' in %s on line %d
5 changes: 5 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5743,6 +5743,11 @@ static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint3
ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);

for (j = 0; j < args->children; j++) {
if (args->child[j]->kind == ZEND_AST_UNPACK) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use unpacking in attribute argument list");
}

zend_const_expr_to_zval(&attr->argv[j], args->child[j]);
}
}
Expand Down
15 changes: 3 additions & 12 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%type <ast> identifier type_expr_without_static union_type_without_static
%type <ast> inline_function union_type
%type <ast> attributed_statement attributed_class_statement attributed_parameter
%type <ast> attribute_arguments attribute_decl attribute attributes
%type <ast> attribute_decl attribute attributes

%type <num> returns_ref function fn is_reference is_variadic variable_modifiers
%type <num> method_modifiers non_empty_member_modifiers member_modifier optional_visibility_modifier
Expand Down Expand Up @@ -317,20 +317,11 @@ name:
| T_NS_SEPARATOR namespace_name { $$ = $2; $$->attr = ZEND_NAME_FQ; }
;

attribute_arguments:
expr
{ $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1); }
| attribute_arguments ',' expr
{ $$ = zend_ast_list_add($1, $3); }
;

attribute_decl:
class_name
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL); }
| class_name '(' ')'
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL); }
| class_name '(' attribute_arguments ')'
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $3); }
| class_name argument_list
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $2); }
;

attribute:
Expand Down

0 comments on commit 6a195ca

Please sign in to comment.