Skip to content

Commit ced9c19

Browse files
committed
Adjustments for upstream changes
1 parent 8c25a23 commit ced9c19

File tree

7 files changed

+70
-123
lines changed

7 files changed

+70
-123
lines changed

Zend/tests/pattern_matching/is/bail.phpt

+17-28
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,30 @@ Object pattern matching
33
--FILE--
44
<?php
55

6-
$o = new stdClass();
6+
(function () {
7+
$o = new stdClass();
78

8-
try {
9-
var_dump($o is self {});
10-
} catch (Throwable $e) {
11-
echo $e->getMessage(), "\n";
12-
}
13-
14-
try {
15-
var_dump($o is parent {});
16-
} catch (Throwable $e) {
17-
echo $e->getMessage(), "\n";
18-
}
19-
20-
try {
21-
var_dump($o is static {});
22-
} catch (Throwable $e) {
23-
echo $e->getMessage(), "\n";
24-
}
9+
try {
10+
var_dump($o is self);
11+
} catch (Throwable $e) {
12+
echo $e->getMessage(), "\n";
13+
}
2514

26-
class C {
27-
public static function test($o) {
28-
try {
29-
var_dump($o is parent {});
30-
} catch (Throwable $e) {
31-
echo $e->getMessage(), "\n";
32-
}
15+
try {
16+
var_dump($o is parent);
17+
} catch (Throwable $e) {
18+
echo $e->getMessage(), "\n";
3319
}
34-
}
3520

36-
C::test($o);
21+
try {
22+
var_dump($o is static);
23+
} catch (Throwable $e) {
24+
echo $e->getMessage(), "\n";
25+
}
26+
})();
3727

3828
?>
3929
--EXPECT--
4030
Cannot access "self" when no class scope is active
4131
Cannot access "parent" when no class scope is active
4232
Cannot access "static" when no class scope is active
43-
Cannot access "parent" when current class scope has no parent

Zend/tests/pattern_matching/is/binding.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ var_dump($a);
4242
var_dump(new NotBox(43) is Box { value: $a });
4343
var_dump($a);
4444

45-
var_dump(43 is $a @ int);
45+
var_dump(43 is $a & int);
4646
var_dump($a);
4747

48-
var_dump([] is $a @ string);
48+
var_dump([] is $a & string);
4949
var_dump($a);
5050

5151
var_dump(new Many() is Many { $a, $b, $c, $d });

Zend/tests/pattern_matching/is/object.phpt

+15-15
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ $foo = new Foo(42);
3434
$bar = new Bar(43);
3535
$qux = new Qux();
3636

37-
var_dump($foo is Foo {});
38-
var_dump($foo is Bar {});
39-
var_dump($foo is Baz {});
40-
var_dump($foo is Qux {});
41-
var_dump($foo is Quux {});
42-
var_dump($bar is Foo {});
43-
var_dump($bar is Bar {});
44-
var_dump($bar is Baz {});
45-
var_dump($bar is Qux {});
46-
var_dump($bar is Quux {});
47-
var_dump(null is Foo {});
48-
var_dump(null is Bar {});
49-
var_dump(null is Baz {});
50-
var_dump(null is Qux {});
51-
var_dump(null is Quux {});
37+
var_dump($foo is Foo);
38+
var_dump($foo is Bar);
39+
var_dump($foo is Baz);
40+
var_dump($foo is Qux);
41+
var_dump($foo is Quux);
42+
var_dump($bar is Foo);
43+
var_dump($bar is Bar);
44+
var_dump($bar is Baz);
45+
var_dump($bar is Qux);
46+
var_dump($bar is Quux);
47+
var_dump(null is Foo);
48+
var_dump(null is Bar);
49+
var_dump(null is Baz);
50+
var_dump(null is Qux);
51+
var_dump(null is Quux);
5252

5353
var_dump($foo is Foo { a: 42 });
5454
var_dump($foo is Foo { a: 42|43 });

Zend/zend_ast.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum _zend_ast_kind {
115115
ZEND_AST_PROPERTY_HOOK_SHORT_BODY,
116116
ZEND_AST_TYPE_PATTERN,
117117
ZEND_AST_ARRAY_PATTERN,
118+
ZEND_AST_BINDING_PATTERN,
118119

119120
/* 2 child nodes */
120121
ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT,
@@ -164,7 +165,6 @@ enum _zend_ast_kind {
164165
ZEND_AST_OBJECT_PATTERN_ELEMENT,
165166
ZEND_AST_RANGE_PATTERN,
166167
ZEND_AST_ARRAY_PATTERN_ELEMENT,
167-
ZEND_AST_BINDING_PATTERN,
168168
ZEND_AST_CLASS_CONST_PATTERN,
169169

170170
/* 3 child nodes */

Zend/zend_compile.c

+1-40
Original file line numberDiff line numberDiff line change
@@ -6624,43 +6624,6 @@ static void zend_compile_match(znode *result, zend_ast *ast)
66246624
efree(jmp_end_opnums);
66256625
}
66266626

6627-
static void zend_compile_pattern_class_name(zend_ast *ast)
6628-
{
6629-
zend_ast *class_name_ast = ast->child[0];
6630-
ZEND_ASSERT(class_name_ast->kind == ZEND_AST_ZVAL);
6631-
6632-
zend_string *class_name = zend_ast_get_str(class_name_ast);
6633-
uint32_t fetch_type = zend_get_class_fetch_type(class_name);
6634-
6635-
switch (fetch_type) {
6636-
case ZEND_FETCH_CLASS_SELF:
6637-
case ZEND_FETCH_CLASS_PARENT:
6638-
case ZEND_FETCH_CLASS_STATIC:
6639-
/* For the const-eval representation store the fetch type instead of the name. */
6640-
zend_string_release(class_name);
6641-
ast->child[0] = NULL;
6642-
ast->attr = fetch_type;
6643-
return;
6644-
case ZEND_FETCH_CLASS_DEFAULT: {
6645-
zend_string *tmp = zend_resolve_class_name_ast(class_name_ast);
6646-
zend_string_release_ex(class_name, 0);
6647-
if (tmp != class_name) {
6648-
zval *zv = zend_ast_get_zval(class_name_ast);
6649-
ZVAL_STR(zv, tmp);
6650-
class_name_ast->attr = ZEND_NAME_FQ;
6651-
}
6652-
break;
6653-
}
6654-
EMPTY_SWITCH_DEFAULT_CASE()
6655-
}
6656-
}
6657-
6658-
static void zend_compile_object_pattern(zend_ast **ast_ptr)
6659-
{
6660-
zend_ast *ast = *ast_ptr;
6661-
zend_compile_pattern_class_name(ast);
6662-
}
6663-
66646627
static zend_type zend_compile_single_typename(zend_ast *ast);
66656628

66666629
static void zend_compile_type_pattern(zend_ast **ast_ptr)
@@ -6682,6 +6645,7 @@ static void zend_compile_type_pattern(zend_ast **ast_ptr)
66826645
type_pattern_ast->child[0] = NULL;
66836646

66846647
if (ZEND_TYPE_IS_COMPLEX(type)) {
6648+
ZEND_ASSERT(ZEND_TYPE_HAS_NAME(type));
66856649
zend_string *class_name = ZEND_TYPE_NAME(type);
66866650
type_pattern_ast->child[0] = zend_ast_create_zval_from_str(class_name);
66876651
}
@@ -6774,9 +6738,6 @@ static void zend_compile_pattern(zend_ast **ast_ptr, void *context)
67746738
bool prev_inside_or_pattern = pattern_context->inside_or_pattern;
67756739

67766740
switch (ast->kind) {
6777-
case ZEND_AST_OBJECT_PATTERN:
6778-
zend_compile_object_pattern(ast_ptr);
6779-
break;
67806741
case ZEND_AST_TYPE_PATTERN:
67816742
zend_compile_type_pattern(ast_ptr);
67826743
break;

Zend/zend_language_parser.y

+7-8
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,9 @@ attributed_class_statement:
964964
property_modifiers optional_type_without_static property_list ';'
965965
{ $$ = zend_ast_create(ZEND_AST_PROP_GROUP, $2, $3, NULL);
966966
$$->attr = $1; }
967-
| property_modifiers optional_type_without_static hooked_property
967+
/*| property_modifiers optional_type_without_static hooked_property
968968
{ $$ = zend_ast_create(ZEND_AST_PROP_GROUP, $2, zend_ast_create_list(1, ZEND_AST_PROP_DECL, $3), NULL);
969-
$$->attr = $1; }
969+
$$->attr = $1; }*/
970970
| class_const_modifiers T_CONST class_const_list ';'
971971
{ $$ = zend_ast_create(ZEND_AST_CLASS_CONST_GROUP, $3, NULL, NULL);
972972
$$->attr = $1; }
@@ -1125,7 +1125,7 @@ property_hook_list:
11251125

11261126
optional_property_hook_list:
11271127
%empty { $$ = NULL; }
1128-
| '{' property_hook_list '}' { $$ = $2; }
1128+
/*| '{' property_hook_list '}' { $$ = $2; }*/
11291129
;
11301130

11311131
property_hook_modifiers:
@@ -1379,8 +1379,8 @@ compound_pattern:
13791379
;
13801380

13811381
type_pattern:
1382-
type_without_static { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $1); }
1383-
| '?' type_without_static { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $2); $2->attr |= ZEND_TYPE_NULLABLE; }
1382+
type { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $1); }
1383+
| '?' type { $$ = zend_ast_create(ZEND_AST_TYPE_PATTERN, $2); $2->attr |= ZEND_TYPE_NULLABLE; }
13841384
;
13851385

13861386
scalar_pattern:
@@ -1394,7 +1394,7 @@ scalar_pattern:
13941394
;
13951395

13961396
object_pattern:
1397-
class_name '{' object_pattern_element_list '}' { $$ = zend_ast_create(ZEND_AST_OBJECT_PATTERN, $1, $3); }
1397+
atomic_pattern '{' object_pattern_element_list '}' { $$ = zend_ast_create(ZEND_AST_OBJECT_PATTERN, $1, $3); }
13981398
;
13991399

14001400
object_pattern_element_list:
@@ -1432,8 +1432,7 @@ range_pattern:
14321432
;
14331433

14341434
binding_pattern:
1435-
T_VARIABLE { $$ = zend_ast_create(ZEND_AST_BINDING_PATTERN, $1, NULL); }
1436-
| T_VARIABLE '@' pattern { $$ = zend_ast_create(ZEND_AST_BINDING_PATTERN, $1, $3); }
1435+
T_VARIABLE { $$ = zend_ast_create(ZEND_AST_BINDING_PATTERN, $1); }
14371436
;
14381437

14391438
array_pattern:

Zend/zend_pattern_matching.c

+27-29
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,45 @@ typedef enum {
2929
} pm_result;
3030

3131
pm_result zend_pattern_match_ex(zval *zv, zend_ast *pattern);
32+
static zend_class_entry *get_class_from_fetch_type(uint32_t fetch_type);
3233

3334
static pm_result match_type(zval *zv, zend_ast *type_ast)
3435
{
3536
zend_ast *class_name_ast = type_ast->child[0];
37+
uint32_t fetch_type;
38+
3639
if (class_name_ast) {
3740
if (Z_TYPE_P(zv) != IS_OBJECT) {
3841
return PM_MISMATCH;
3942
}
4043

41-
zend_object *obj = Z_OBJ_P(zv);
4244
zend_string *class_name = zend_ast_get_str(class_name_ast);
43-
if (!zend_string_equals_ci(obj->ce->name, class_name)) {
44-
zend_class_entry *expected_class = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
45-
if (!expected_class || !instanceof_function(Z_OBJ_P(zv)->ce, expected_class)) {
46-
return PM_MISMATCH;
45+
fetch_type = zend_get_class_fetch_type(class_name);
46+
47+
zend_class_entry *expected_class = NULL;
48+
if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
49+
zend_object *obj = Z_OBJ_P(zv);
50+
if (zend_string_equals_ci(obj->ce->name, class_name)) {
51+
return PM_MATCH;
4752
}
53+
expected_class = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
54+
} else {
55+
non_default_fetch_type:
56+
expected_class = get_class_from_fetch_type(fetch_type);
57+
if (!expected_class) {
58+
return PM_ERROR;
59+
}
60+
}
61+
if (!expected_class || !instanceof_function(Z_OBJ_P(zv)->ce, expected_class)) {
62+
return PM_MISMATCH;
4863
}
4964
} else {
5065
zend_type type = ZEND_TYPE_INIT_MASK(type_ast->attr);
5166
if (!ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(zv))) {
67+
if (ZEND_TYPE_CONTAINS_CODE(type, IS_STATIC)) {
68+
fetch_type = ZEND_FETCH_CLASS_STATIC;
69+
goto non_default_fetch_type;
70+
}
5271
return PM_MISMATCH;
5372
}
5473
}
@@ -105,25 +124,9 @@ static pm_result match_object(zval *zv, zend_ast *pattern)
105124
}
106125

107126
zend_object *obj = Z_OBJ_P(zv);
108-
zend_ast *class_name_ast = pattern->child[0];
109-
110-
if (class_name_ast) {
111-
zend_string *class_name = zend_ast_get_str(class_name_ast);
112-
if (!zend_string_equals_ci(obj->ce->name, class_name)) {
113-
zend_class_entry *expected_class = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
114-
if (!expected_class || !instanceof_function(Z_OBJ_P(zv)->ce, expected_class)) {
115-
return PM_MISMATCH;
116-
}
117-
}
118-
} else {
119-
uint32_t fetch_type = pattern->attr;
120-
zend_class_entry *scope = get_class_from_fetch_type(fetch_type);
121-
if (EG(exception)) {
122-
return PM_ERROR;
123-
}
124-
if (!instanceof_function(Z_OBJ_P(zv)->ce, scope)) {
125-
return PM_MISMATCH;
126-
}
127+
pm_result type_result = match_type(zv, pattern->child[0]);
128+
if (type_result != PM_MATCH) {
129+
return type_result;
127130
}
128131

129132
zend_ast_list *elements = zend_ast_get_list(pattern->child[1]);
@@ -165,11 +168,6 @@ static pm_result match_range(zval *zv, zend_ast *pattern)
165168

166169
static pm_result match_binding(zval *zv, zend_ast *pattern)
167170
{
168-
zend_ast *sub_pattern = pattern->child[1];
169-
if (sub_pattern && !zend_pattern_match_ex(zv, sub_pattern)) {
170-
return PM_MISMATCH;
171-
}
172-
173171
zend_pm_context *context = EG(pm_context);
174172
zend_pm_bindings *bindings = context->bindings;
175173
if (!bindings) {

0 commit comments

Comments
 (0)