Skip to content

Commit b09570d

Browse files
authored
1 parent 28f5201 commit b09570d

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ ast\flags\TYPE_NULL // php 8.0 union types
275275
ast\flags\TYPE_FALSE // php 8.0 union types
276276
ast\flags\TYPE_STATIC // php 8.0 static return type
277277
ast\flags\TYPE_MIXED // php 8.0 mixed type
278+
ast\flags\TYPE_NEVER // php 8.1 never type
278279
279280
// Used by ast\AST_CAST (exclusive)
280281
ast\flags\TYPE_NULL

ast.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@
9898
# define PARAM_MODIFIER_PRIVATE ZEND_ACC_PRIVATE
9999
#endif
100100

101+
#if PHP_VERSION_ID < 80100
102+
# define IS_NEVER 22
103+
#endif
104+
101105
/* This contains state of the ast Node creator. */
102106
typedef struct ast_state_info {
103107
zend_long version;
@@ -159,6 +163,7 @@ static const char *type_flags[] = {
159163
AST_FLAG(TYPE_ITERABLE),
160164
AST_FLAG(TYPE_STATIC),
161165
AST_FLAG(TYPE_MIXED),
166+
AST_FLAG(TYPE_NEVER),
162167
NULL
163168
};
164169

@@ -535,6 +540,7 @@ static const builtin_type_info builtin_types[] = {
535540
{ZEND_STRL("false"), IS_FALSE},
536541
// {ZEND_STRL("static"), IS_STATIC}, /* Impossible to be parsed before php 8 */
537542
{ZEND_STRL("mixed"), IS_MIXED},
543+
{ZEND_STRL("never"), IS_NEVER},
538544
{NULL, 0, IS_UNDEF}
539545
};
540546
static inline zend_uchar lookup_builtin_type(const zend_string *name) {
@@ -1427,6 +1433,7 @@ PHP_MINIT_FUNCTION(ast) {
14271433
ast_register_flag_constant("TYPE_ITERABLE", IS_ITERABLE);
14281434
ast_register_flag_constant("TYPE_STATIC", IS_STATIC);
14291435
ast_register_flag_constant("TYPE_MIXED", IS_MIXED);
1436+
ast_register_flag_constant("TYPE_NEVER", IS_NEVER);
14301437

14311438
ast_register_flag_constant("UNARY_BOOL_NOT", ZEND_BOOL_NOT);
14321439
ast_register_flag_constant("UNARY_BITWISE_NOT", ZEND_BW_NOT);

ast_stub.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
const PARAM_VARIADIC = 16;
145145
const TYPE_NULL = 1;
146146
const TYPE_FALSE = 2;
147-
const TYPE_BOOL = 17;
147+
const TYPE_BOOL = 18;
148148
const TYPE_LONG = 4;
149149
const TYPE_DOUBLE = 5;
150150
const TYPE_STRING = 6;
@@ -155,6 +155,7 @@
155155
const TYPE_ITERABLE = 13;
156156
const TYPE_STATIC = 15;
157157
const TYPE_MIXED = 16;
158+
const TYPE_NEVER = 17;
158159
const UNARY_BOOL_NOT = 14;
159160
const UNARY_BITWISE_NOT = 13;
160161
const UNARY_SILENCE = 260;

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<license uri="https://github.com/nikic/php-ast/blob/master/LICENSE">BSD-3-Clause</license>
3131
<notes>
3232
- Support php 8.1 enums, add 'type' (for enum type) to AST_CLASS nodes in AST version 85+.
33+
- Support php 8.1 'never' return type.
3334
</notes>
3435
<contents>
3536
<dir name="/">

tests/metadata.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ AST_METHOD: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE,
5656
AST_ARROW_FUNC: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE, MODIFIER_STATIC, MODIFIER_ABSTRACT, MODIFIER_FINAL, FUNC_RETURNS_REF, FUNC_GENERATOR]
5757
AST_CLASS: (combinable) [CLASS_ABSTRACT, CLASS_FINAL, CLASS_TRAIT, CLASS_INTERFACE, CLASS_ANONYMOUS, CLASS_ENUM]
5858
AST_MAGIC_CONST: [MAGIC_LINE, MAGIC_FILE, MAGIC_DIR, MAGIC_NAMESPACE, MAGIC_FUNCTION, MAGIC_METHOD, MAGIC_CLASS, MAGIC_TRAIT]
59-
AST_TYPE: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE, TYPE_STATIC, TYPE_MIXED]
59+
AST_TYPE: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE, TYPE_STATIC, TYPE_MIXED, TYPE_NEVER]
6060
AST_VAR: []
6161
AST_CONST: []
6262
AST_UNPACK: []
63-
AST_CAST: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE, TYPE_STATIC, TYPE_MIXED]
63+
AST_CAST: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE, TYPE_STATIC, TYPE_MIXED, TYPE_NEVER]
6464
AST_EMPTY: []
6565
AST_ISSET: []
6666
AST_SHELL_EXEC: []

tests/never_return_type.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
'never' return type parsing
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/../util.php';
7+
8+
$code = <<<'PHP'
9+
<?php
10+
function up(): never {
11+
}
12+
PHP;
13+
14+
$node = ast\parse_code($code, $version=85);
15+
echo ast_dump($node), "\n";
16+
--EXPECTF--
17+
AST_STMT_LIST
18+
0: AST_FUNC_DECL
19+
flags: 0
20+
name: "up"
21+
docComment: null
22+
params: AST_PARAM_LIST
23+
stmts: AST_STMT_LIST
24+
returnType: AST_TYPE
25+
flags: TYPE_NEVER (%d)
26+
attributes: null
27+
__declId: 0

0 commit comments

Comments
 (0)