Skip to content

Commit e659715

Browse files
committed
Fix nullable type support
1 parent c174250 commit e659715

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

Zend/tests/pattern_matching/is/type.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ var_dump('' is string);
1010
var_dump('' is int);
1111
var_dump('' is bool);
1212
var_dump('' is object);
13+
var_dump('' is ?string);
14+
var_dump(null is ?string);
15+
var_dump('' is ?object);
1316

1417
?>
1518
--EXPECT--
1619
bool(true)
1720
bool(false)
1821
bool(false)
1922
bool(false)
23+
bool(true)
24+
bool(true)
25+
bool(false)

Zend/tests/pattern_matching/match/basic.phpt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class Foo {
1111
const FOO = 'foo';
1212
}
1313

14+
var_dump(match (null) {
15+
is false => wrong(),
16+
is 0 => wrong(),
17+
is [] => wrong(),
18+
is null => 'Literal pattern with null',
19+
});
20+
1421
var_dump(match (true) {
1522
is false => wrong(),
1623
is true => 'Literal pattern with bool',
@@ -57,20 +64,14 @@ var_dump(match (15) {
5764
is 20 ..< 30 => wrong(),
5865
});
5966

60-
// var_dump(match (true) {
61-
// false if false => wrong(),
62-
// false if true => wrong(),
63-
// true if false => wrong(),
64-
// true if true => 'Guard',
65-
// });
66-
6767
// var_dump(match ('foo') {
6868
// is 'bar' => wrong(),
6969
// is Foo::FOO => 'Class constant literal',
7070
// });
7171

7272
?>
7373
--EXPECT--
74+
string(25) "Literal pattern with null"
7475
string(25) "Literal pattern with bool"
7576
string(24) "Literal pattern with int"
7677
string(27) "Literal pattern with string"

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6124,7 +6124,12 @@ static void zend_compile_type_pattern(zend_ast **ast_ptr)
61246124
{
61256125
zend_ast *type_pattern_ast = *ast_ptr;
61266126
zend_ast *type_ast = type_pattern_ast->child[0];
6127+
bool nullable = type_ast->attr & ZEND_TYPE_NULLABLE;
6128+
type_ast->attr &= ~ZEND_TYPE_NULLABLE;
61276129
zend_type type = zend_compile_single_typename(type_ast);
6130+
if (nullable) {
6131+
ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
6132+
}
61286133
uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
61296134

61306135
// FIXME: Make sure the type mask actually fits

Zend/zend_language_parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ compound_pattern:
13021302

13031303
type_pattern:
13041304
type_without_static { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $1); }
1305-
| '?' type_without_static { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $2); $$->attr |= ZEND_TYPE_NULLABLE; }
1305+
| '?' type_without_static { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $2); $2->attr |= ZEND_TYPE_NULLABLE; }
13061306
;
13071307

13081308
scalar_pattern:

0 commit comments

Comments
 (0)