Skip to content

Commit 7883996

Browse files
authored
Support ast\flags\TYPE_FALSE for union types. (nikic#140)
AST_CAST already has some impossible flags such as TYPE_ITERABLE. This was overlooked when adding AST_TYPE_UNION
1 parent 34bad61 commit 7883996

File tree

6 files changed

+124
-10
lines changed

6 files changed

+124
-10
lines changed

ast.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static const char *param_flags[] = {
106106

107107
static const char *type_flags[] = {
108108
AST_FLAG(TYPE_NULL),
109+
AST_FLAG(TYPE_FALSE),
109110
AST_FLAG(TYPE_BOOL),
110111
AST_FLAG(TYPE_LONG),
111112
AST_FLAG(TYPE_DOUBLE),
@@ -1273,6 +1274,7 @@ PHP_MINIT_FUNCTION(ast) {
12731274
ast_register_flag_constant("PARAM_VARIADIC", ZEND_PARAM_VARIADIC);
12741275

12751276
ast_register_flag_constant("TYPE_NULL", IS_NULL);
1277+
ast_register_flag_constant("TYPE_FALSE", IS_FALSE);
12761278
ast_register_flag_constant("TYPE_BOOL", _IS_BOOL);
12771279
ast_register_flag_constant("TYPE_LONG", IS_LONG);
12781280
ast_register_flag_constant("TYPE_DOUBLE", IS_DOUBLE);

ast_stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
const PARAM_REF = 1;
129129
const PARAM_VARIADIC = 2;
130130
const TYPE_NULL = 1;
131+
const TYPE_FALSE = 2;
131132
const TYPE_BOOL = 18;
132133
const TYPE_LONG = 4;
133134
const TYPE_DOUBLE = 5;

package.xml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@
1212
<email>[email protected]</email>
1313
<active>yes</active>
1414
</lead>
15-
<developer>
15+
<lead>
1616
<name>Tyson Andre</name>
1717
<user>tandre</user>
1818
<email>[email protected]</email>
1919
<active>yes</active>
20-
</developer>
21-
<date>2019-11-10</date>
20+
</lead>
21+
<date>2019-11-23</date>
2222
<version>
23-
<release>1.0.4</release>
24-
<api>1.0.4</api>
23+
<release>1.0.5</release>
24+
<api>1.0.5</api>
2525
</version>
2626
<stability>
2727
<release>stable</release>
2828
<api>stable</api>
2929
</stability>
3030
<license uri="https://github.com/nikic/php-ast/blob/master/LICENSE">BSD-3-Clause</license>
3131
<notes>
32-
- Add AST_TYPE_UNION to support PHP 8.0 Union Types.
32+
- Add ast\flags\TYPE_FALSE to support PHP 8.0 Union Types.
3333
</notes>
3434
<contents>
3535
<dir name="/">
@@ -120,6 +120,21 @@
120120
<providesextension>ast</providesextension>
121121
<extsrcrelease />
122122
<changelog>
123+
<release>
124+
<date>2019-11-10</date>
125+
<version>
126+
<release>1.0.4</release>
127+
<api>1.0.4</api>
128+
</version>
129+
<stability>
130+
<release>stable</release>
131+
<api>stable</api>
132+
</stability>
133+
<license uri="https://github.com/nikic/php-ast/blob/master/LICENSE">BSD-3-Clause</license>
134+
<notes>
135+
- Add AST_TYPE_UNION to support PHP 8.0 Union Types.
136+
</notes>
137+
</release>
123138
<release>
124139
<date>2019-07-27</date>
125140
<version>

tests/metadata.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ AST_METHOD: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE,
5151
AST_ARROW_FUNC: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE, MODIFIER_STATIC, MODIFIER_ABSTRACT, MODIFIER_FINAL, FUNC_RETURNS_REF, FUNC_GENERATOR]
5252
AST_CLASS: [CLASS_ABSTRACT, CLASS_FINAL, CLASS_TRAIT, CLASS_INTERFACE, CLASS_ANONYMOUS]
5353
AST_MAGIC_CONST: [MAGIC_LINE, MAGIC_FILE, MAGIC_DIR, MAGIC_NAMESPACE, MAGIC_FUNCTION, MAGIC_METHOD, MAGIC_CLASS, MAGIC_TRAIT]
54-
AST_TYPE: [TYPE_NULL, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE]
54+
AST_TYPE: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE]
5555
AST_VAR: []
5656
AST_CONST: []
5757
AST_UNPACK: []
58-
AST_CAST: [TYPE_NULL, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE]
58+
AST_CAST: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE]
5959
AST_EMPTY: []
6060
AST_ISSET: []
6161
AST_SHELL_EXEC: []

tests/php80_union_types.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ AST_STMT_LIST
6565
0: AST_TYPE
6666
flags: TYPE_STRING (%d)
6767
1: AST_TYPE
68-
flags: 2
68+
flags: TYPE_FALSE (%d)
6969
__declId: 0
7070
2: AST_CLASS
7171
flags: 0
@@ -119,7 +119,7 @@ AST_STMT_LIST
119119
flags: NAME_RELATIVE (2)
120120
name: "Xyz"
121121
1: AST_TYPE
122-
flags: 2
122+
flags: TYPE_FALSE (%d)
123123
2: AST_TYPE
124124
flags: TYPE_NULL (%d)
125125
__declId: 2

tests/php80_union_types_false.phpt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
--TEST--
2+
Union types in PHP 8.0
3+
--SKIPIF--
4+
<?php if (PHP_VERSION_ID < 80000) die('skip PHP >= 8.0 only'); ?>
5+
--FILE--
6+
<?php
7+
8+
require __DIR__ . '/../util.php';
9+
10+
$code = <<<'PHP'
11+
<?php
12+
class Example {
13+
public iterable|null|false $value;
14+
}
15+
// These are semantic errors, not syntax errors
16+
$f = fn(OBJECT|false $a) : false => $a;
17+
$g = function (false $arg) : false|null { return false; };
18+
PHP;
19+
20+
$node = ast\parse_code($code, $version=70);
21+
echo ast_dump($node), "\n";
22+
--EXPECTF--
23+
AST_STMT_LIST
24+
0: AST_CLASS
25+
flags: 0
26+
name: "Example"
27+
docComment: null
28+
extends: null
29+
implements: null
30+
stmts: AST_STMT_LIST
31+
0: AST_PROP_GROUP
32+
flags: MODIFIER_PUBLIC (%d)
33+
type: AST_TYPE_UNION
34+
0: AST_TYPE
35+
flags: TYPE_ITERABLE (%d)
36+
1: AST_TYPE
37+
flags: TYPE_NULL (%d)
38+
2: AST_TYPE
39+
flags: TYPE_FALSE (%d)
40+
props: AST_PROP_DECL
41+
flags: 0
42+
0: AST_PROP_ELEM
43+
name: "value"
44+
default: null
45+
docComment: null
46+
__declId: 0
47+
1: AST_ASSIGN
48+
var: AST_VAR
49+
name: "f"
50+
expr: AST_ARROW_FUNC
51+
flags: 0
52+
name: "{closure}"
53+
docComment: null
54+
params: AST_PARAM_LIST
55+
0: AST_PARAM
56+
flags: 0
57+
type: AST_TYPE_UNION
58+
0: AST_TYPE
59+
flags: TYPE_OBJECT (%d)
60+
1: AST_TYPE
61+
flags: TYPE_FALSE (%d)
62+
name: "a"
63+
default: null
64+
stmts: AST_RETURN
65+
expr: AST_VAR
66+
name: "a"
67+
returnType: AST_TYPE
68+
flags: TYPE_FALSE (%d)
69+
__declId: 1
70+
2: AST_ASSIGN
71+
var: AST_VAR
72+
name: "g"
73+
expr: AST_CLOSURE
74+
flags: 0
75+
name: "{closure}"
76+
docComment: null
77+
params: AST_PARAM_LIST
78+
0: AST_PARAM
79+
flags: 0
80+
type: AST_TYPE
81+
flags: TYPE_FALSE (%d)
82+
name: "arg"
83+
default: null
84+
uses: null
85+
stmts: AST_STMT_LIST
86+
0: AST_RETURN
87+
expr: AST_CONST
88+
name: AST_NAME
89+
flags: NAME_NOT_FQ (%d)
90+
name: "false"
91+
returnType: AST_TYPE_UNION
92+
0: AST_TYPE
93+
flags: TYPE_FALSE (%d)
94+
1: AST_TYPE
95+
flags: TYPE_NULL (%d)
96+
__declId: 2

0 commit comments

Comments
 (0)