Skip to content

Commit feae85b

Browse files
authored
Support php 8.0 non-capturing catch (nikic#157)
1 parent f5532bf commit feae85b

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static inline zend_bool ast_is_type(zend_ast *ast, zend_ast *parent, uint32_t i)
424424

425425
static inline zend_bool ast_is_var_name(zend_ast *ast, zend_ast *parent, uint32_t i) {
426426
return (parent->kind == ZEND_AST_STATIC && i == 0)
427-
|| (parent->kind == ZEND_AST_CATCH && i == 1);
427+
|| (parent->kind == ZEND_AST_CATCH && i == 1 && ast != NULL);
428428
}
429429

430430
/* Whether this node may need statement list normalization */

tests/php80_noncapturing_catch.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Non-capturing catches 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+
try {
13+
foo();
14+
} catch (Exception) {
15+
echo "ignoring";
16+
}
17+
PHP;
18+
19+
$node = ast\parse_code($code, $version=70);
20+
echo ast_dump($node), "\n";
21+
--EXPECTF--
22+
AST_STMT_LIST
23+
0: AST_TRY
24+
try: AST_STMT_LIST
25+
0: AST_CALL
26+
expr: AST_NAME
27+
flags: NAME_NOT_FQ (1)
28+
name: "foo"
29+
args: AST_ARG_LIST
30+
catches: AST_CATCH_LIST
31+
0: AST_CATCH
32+
class: AST_NAME_LIST
33+
0: AST_NAME
34+
flags: NAME_NOT_FQ (1)
35+
name: "Exception"
36+
var: null
37+
stmts: AST_STMT_LIST
38+
0: AST_ECHO
39+
expr: "ignoring"
40+
finally: null

0 commit comments

Comments
 (0)