Skip to content

Commit f79148f

Browse files
Girgiasnikic
authored andcommitted
Revert "Allow self and parent to be part of an intersection type"
Let's not accept those either This reverts commit 386b104335c7205cb93ad18e40b27b1c0bd9fdc6.
1 parent 1227f45 commit f79148f

File tree

6 files changed

+34
-87
lines changed

6 files changed

+34
-87
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
parent type cannot take part in an intersection type
3+
--FILE--
4+
<?php
5+
6+
class A {}
7+
8+
class B extends A {
9+
public function foo(): parent&Iterator {}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Type parent cannot be part of an intersection type in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
self type cannot take part in an intersection type
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function foo(): self&Iterator {}
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Type self cannot be part of an intersection type in %s on line %d

Zend/tests/type_declarations/intersection_types/redundant_types/duplicate_parent_type.phpt

Lines changed: 0 additions & 14 deletions
This file was deleted.

Zend/tests/type_declarations/intersection_types/redundant_types/duplicate_self_type.phpt

Lines changed: 0 additions & 13 deletions
This file was deleted.

Zend/tests/type_declarations/intersection_types/self_parent_in_intersection.phpt

Lines changed: 0 additions & 57 deletions
This file was deleted.

Zend/zend_compile.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6302,7 +6302,6 @@ static zend_type zend_compile_typename(
63026302
zend_ast_list *list = zend_ast_get_list(ast);
63036303
zend_type_list *type_list;
63046304

6305-
// TODO Is this still true if self/parent are accepted?
63066305
/* Allocate the type list directly on the arena as it must be a type
63076306
* list of the same number of elements as the AST list has children */
63086307
type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
@@ -6313,14 +6312,20 @@ static zend_type zend_compile_typename(
63136312
for (uint32_t i = 0; i < list->children; i++) {
63146313
zend_ast *type_ast = list->child[i];
63156314
zend_type single_type = zend_compile_single_typename(type_ast);
6315+
zend_string *standard_type_str = zend_type_to_string(single_type);
63166316

63176317
/* An intersection of standard types cannot exist so invalidate it */
63186318
if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
6319-
zend_string *standard_type_str = zend_type_to_string(single_type);
63206319
zend_error_noreturn(E_COMPILE_ERROR,
63216320
"Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
6322-
zend_string_release_ex(standard_type_str, false);
63236321
}
6322+
/* Check for "self" and "parent" too */
6323+
if (zend_string_equals_literal_ci(standard_type_str, "self")
6324+
|| zend_string_equals_literal_ci(standard_type_str, "parent")) {
6325+
zend_error_noreturn(E_COMPILE_ERROR,
6326+
"Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
6327+
}
6328+
zend_string_release_ex(standard_type_str, false);
63246329

63256330
/* Add type to the type list */
63266331
type_list->types[type_list->num_types++] = single_type;

0 commit comments

Comments
 (0)