Skip to content

Commit 6a195ca

Browse files
committed
Treat attribute argument lists like normal argument lists
Allow trailing comma. Syntactically allow unpacking, but forbid it during compilation. The trailing comma test-case is adopted from GH-5796.
1 parent d8dfb21 commit 6a195ca

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Cannot use unpacking in attribute argument list
3+
--FILE--
4+
<?php
5+
6+
<<MyAttribute(...[1, 2, 3])>>
7+
class Foo { }
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot use unpacking in attribute argument list in %s on line %d
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Trailing comma in attribute argument list
3+
--FILE--
4+
<?php
5+
6+
<<MyAttribute(
7+
"there",
8+
"are",
9+
"many",
10+
"arguments",
11+
)>>
12+
class Foo { }
13+
14+
$ref = new \ReflectionClass(Foo::class);
15+
$attr = $ref->getAttributes()[0];
16+
var_dump($attr->getName(), $attr->getArguments());
17+
18+
?>
19+
--EXPECT--
20+
string(11) "MyAttribute"
21+
array(4) {
22+
[0]=>
23+
string(5) "there"
24+
[1]=>
25+
string(3) "are"
26+
[2]=>
27+
string(4) "many"
28+
[3]=>
29+
string(9) "arguments"
30+
}

Zend/tests/varSyntax/globalNonSimpleVariableError.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ global $$foo->bar;
77

88
?>
99
--EXPECTF--
10-
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in %s on line %d
10+
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ';' or ',' in %s on line %d

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5743,6 +5743,11 @@ static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint3
57435743
ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
57445744

57455745
for (j = 0; j < args->children; j++) {
5746+
if (args->child[j]->kind == ZEND_AST_UNPACK) {
5747+
zend_error_noreturn(E_COMPILE_ERROR,
5748+
"Cannot use unpacking in attribute argument list");
5749+
}
5750+
57465751
zend_const_expr_to_zval(&attr->argv[j], args->child[j]);
57475752
}
57485753
}

Zend/zend_language_parser.y

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
260260
%type <ast> identifier type_expr_without_static union_type_without_static
261261
%type <ast> inline_function union_type
262262
%type <ast> attributed_statement attributed_class_statement attributed_parameter
263-
%type <ast> attribute_arguments attribute_decl attribute attributes
263+
%type <ast> attribute_decl attribute attributes
264264

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

320-
attribute_arguments:
321-
expr
322-
{ $$ = zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1); }
323-
| attribute_arguments ',' expr
324-
{ $$ = zend_ast_list_add($1, $3); }
325-
;
326-
327320
attribute_decl:
328321
class_name
329322
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL); }
330-
| class_name '(' ')'
331-
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL); }
332-
| class_name '(' attribute_arguments ')'
333-
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $3); }
323+
| class_name argument_list
324+
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $2); }
334325
;
335326

336327
attribute:

0 commit comments

Comments
 (0)