Skip to content

Commit 186b0f3

Browse files
committed
Don't allow leading \ in group use decl list
We recently added support for "use \Foo\{Bar}". This commit drops support for the reverse "use Foo\{\Bar}". Those two got mixed up in the initial implementation.
1 parent 4d7a8f1 commit 186b0f3

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

Zend/tests/ns_094.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ use const Foo\Bar\{
1212
};
1313

1414
--EXPECTF--
15-
16-
Parse error: syntax error, unexpected 'const' (T_CONST), expecting identifier (T_STRING) or \\ (T_NS_SEPARATOR) in %s on line 7
15+
Parse error: syntax error, unexpected 'const' (T_CONST), expecting identifier (T_STRING) in %s on line 7

Zend/tests/ns_096.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Group use declaration list should not contain leading separator
3+
--FILE--
4+
<?php
5+
6+
use Foo\Bar\{\Baz};
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) in /home/nikic/php-src/Zend/tests/ns_096.php on line 3

Zend/zend_language_parser.y

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
229229
%type <ast> class_declaration_statement trait_declaration_statement
230230
%type <ast> interface_declaration_statement interface_extends_list
231231
%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
232-
%type <ast> mixed_group_use_declaration use_declaration const_decl inner_statement
232+
%type <ast> mixed_group_use_declaration use_declaration unprefixed_use_declaration
233+
%type <ast> unprefixed_use_declarations const_decl inner_statement
233234
%type <ast> expr optional_expr while_statement for_statement foreach_variable
234235
%type <ast> foreach_statement declare_statement finally_statement unset_variable variable
235236
%type <ast> extends_from parameter optional_type argument expr_without_variable global_var
@@ -335,9 +336,9 @@ use_type:
335336
;
336337

337338
group_use_declaration:
338-
namespace_name T_NS_SEPARATOR '{' use_declarations '}'
339+
namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
339340
{ $$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4); }
340-
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' use_declarations '}'
341+
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
341342
{ $$ = zend_ast_create(ZEND_AST_GROUP_USE, $2, $5); }
342343
;
343344

@@ -355,9 +356,11 @@ inline_use_declarations:
355356
{ $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
356357
;
357358

358-
inline_use_declaration:
359-
use_declaration { $$ = $1; $$->attr = T_CLASS; }
360-
| use_type use_declaration { $$ = $2; $$->attr = $1; }
359+
unprefixed_use_declarations:
360+
unprefixed_use_declarations ',' unprefixed_use_declaration
361+
{ $$ = zend_ast_list_add($1, $3); }
362+
| unprefixed_use_declaration
363+
{ $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
361364
;
362365

363366
use_declarations:
@@ -367,15 +370,21 @@ use_declarations:
367370
{ $$ = zend_ast_create_list(1, ZEND_AST_USE, $1); }
368371
;
369372

370-
use_declaration:
373+
inline_use_declaration:
374+
unprefixed_use_declaration { $$ = $1; $$->attr = T_CLASS; }
375+
| use_type unprefixed_use_declaration { $$ = $2; $$->attr = $1; }
376+
;
377+
378+
unprefixed_use_declaration:
371379
namespace_name
372380
{ $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, NULL); }
373381
| namespace_name T_AS T_STRING
374382
{ $$ = zend_ast_create(ZEND_AST_USE_ELEM, $1, $3); }
375-
| T_NS_SEPARATOR namespace_name
376-
{ $$ = zend_ast_create(ZEND_AST_USE_ELEM, $2, NULL); }
377-
| T_NS_SEPARATOR namespace_name T_AS T_STRING
378-
{ $$ = zend_ast_create(ZEND_AST_USE_ELEM, $2, $4); }
383+
;
384+
385+
use_declaration:
386+
unprefixed_use_declaration { $$ = $1; }
387+
| T_NS_SEPARATOR unprefixed_use_declaration { $$ = $2; }
379388
;
380389

381390
const_list:

0 commit comments

Comments
 (0)