Skip to content

Commit 6d9463c

Browse files
committed
Version 60: Remove "uses" child where not relevant
Fixes nikic#92.
1 parent 60ee513 commit 6d9463c

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ AST_EMPTY: expr
415415
AST_EXIT: expr
416416
AST_FOR: init, cond, loop, stmts
417417
AST_FOREACH: expr, value, key, stmts
418-
AST_FUNC_DECL: params, uses, stmts, returnType
418+
AST_FUNC_DECL: params, stmts, returnType
419+
uses // prior to version 60
419420
name, docComment // since version 50
420421
AST_GLOBAL: var
421422
AST_GOTO: label
@@ -429,7 +430,8 @@ AST_INSTANCEOF: expr, class
429430
AST_ISSET: var
430431
AST_LABEL: name
431432
AST_MAGIC_CONST:
432-
AST_METHOD: params, uses, stmts, returnType
433+
AST_METHOD: params, stmts, returnType
434+
uses // prior to version 60
433435
name, docComment // since version 50
434436
AST_METHOD_CALL: expr, method, args
435437
AST_METHOD_REFERENCE: class, method
@@ -511,11 +513,16 @@ function accepts a boolean argument that determines whether deprecated versions
511513
In the following the changes in the respective AST versions, as well as their current support state,
512514
are listed.
513515

516+
### 60 (experimental)
517+
518+
* `AST_FUNC_DECL` and `AST_METHOD` no longer generate a `uses` child. Previously this child was
519+
always `null`.
520+
514521
### 50 (current)
515522

516523
Supported since 2017-07-19.
517524

518-
* `ast\Node\Decl` nodes are no longer generated. AST kinds `AST_FUNCTION`, `AST_METHOD`,
525+
* `ast\Node\Decl` nodes are no longer generated. AST kinds `AST_FUNC_DECL`, `AST_METHOD`,
519526
`AST_CLOSURE` and `AST_CLASS` now also use the normal `ast\Node` class. The `name` and
520527
`docComment` properties are now represented as children. The `endLineno` is still represented as
521528
an (undeclared) property.

ast.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ static void ast_fill_children_ht(HashTable *ht, zend_ast *ast, ast_state_info_t
610610
&& (ast->kind == ZEND_AST_PROP_ELEM || ast->kind == ZEND_AST_CONST_ELEM)) {
611611
/* Skip docComment child -- It's handled separately */
612612
continue;
613+
} else if (state->version >= 60 && i == 1
614+
&& (ast->kind == ZEND_AST_FUNC_DECL || ast->kind == ZEND_AST_METHOD)) {
615+
/* Skip "uses" child, it is only relevant for closures */
616+
continue;
613617
#if PHP_VERSION_ID >= 70100
614618
} else if (ast->kind == ZEND_AST_LIST && child != NULL) {
615619
/* Emulate simple variable list */
@@ -787,7 +791,7 @@ static void ast_to_zval(zval *zv, zend_ast *ast, ast_state_info_t *state) {
787791
ast_fill_children_ht(Z_ARRVAL(children_zv), ast, state);
788792
}
789793

790-
static const zend_long versions[] = {30, 35, 40, 45, 50};
794+
static const zend_long versions[] = {30, 35, 40, 45, 50, 60};
791795
static const size_t versions_count = sizeof(versions)/sizeof(versions[0]);
792796

793797
static inline zend_bool ast_version_deprecated(zend_long version) {

tests/functions_dont_use.phpt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Uses only make sense on closures
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/../util.php';
7+
8+
$code = <<<'PHP'
9+
<?php
10+
function() {};
11+
function test() {}
12+
PHP;
13+
14+
echo ast_dump(ast\parse_code($code, $version=50)), "\n";
15+
echo ast_dump(ast\parse_code($code, $version=60)), "\n";
16+
17+
?>
18+
--EXPECT--
19+
AST_STMT_LIST
20+
0: AST_CLOSURE
21+
flags: 0
22+
name: "{closure}"
23+
docComment: null
24+
params: AST_PARAM_LIST
25+
uses: null
26+
stmts: AST_STMT_LIST
27+
returnType: null
28+
__declId: 0
29+
1: AST_FUNC_DECL
30+
flags: 0
31+
name: "test"
32+
docComment: null
33+
params: AST_PARAM_LIST
34+
uses: null
35+
stmts: AST_STMT_LIST
36+
returnType: null
37+
__declId: 1
38+
AST_STMT_LIST
39+
0: AST_CLOSURE
40+
flags: 0
41+
name: "{closure}"
42+
docComment: null
43+
params: AST_PARAM_LIST
44+
uses: null
45+
stmts: AST_STMT_LIST
46+
returnType: null
47+
__declId: 0
48+
1: AST_FUNC_DECL
49+
flags: 0
50+
name: "test"
51+
docComment: null
52+
params: AST_PARAM_LIST
53+
stmts: AST_STMT_LIST
54+
returnType: null
55+
__declId: 1

tests/get_supported_versions.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var_dump(ast\get_supported_versions(true));
88

99
?>
1010
--EXPECT--
11-
array(5) {
11+
array(6) {
1212
[0]=>
1313
int(30)
1414
[1]=>
@@ -19,8 +19,12 @@ array(5) {
1919
int(45)
2020
[4]=>
2121
int(50)
22+
[5]=>
23+
int(60)
2224
}
23-
array(1) {
25+
array(2) {
2426
[0]=>
2527
int(50)
28+
[1]=>
29+
int(60)
2630
}

0 commit comments

Comments
 (0)