Skip to content

Commit 203ec25

Browse files
committed
Revert "Allow self and parent to be part of an intersection type"
Let's not accept those either This reverts commit 386b104335c7205cb93ad18e40b27b1c0bd9fdc6.
1 parent 31103e4 commit 203ec25

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
@@ -6307,7 +6307,6 @@ static zend_type zend_compile_typename(
63076307
zend_ast_list *list = zend_ast_get_list(ast);
63086308
zend_type_list *type_list;
63096309

6310-
// TODO Is this still true if self/parent are accepted?
63116310
/* Allocate the type list directly on the arena as it must be a type
63126311
* list of the same number of elements as the AST list has children */
63136312
type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
@@ -6318,14 +6317,20 @@ static zend_type zend_compile_typename(
63186317
for (uint32_t i = 0; i < list->children; i++) {
63196318
zend_ast *type_ast = list->child[i];
63206319
zend_type single_type = zend_compile_single_typename(type_ast);
6320+
zend_string *standard_type_str = zend_type_to_string(single_type);
63216321

63226322
/* An intersection of standard types cannot exist so invalidate it */
63236323
if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
6324-
zend_string *standard_type_str = zend_type_to_string(single_type);
63256324
zend_error_noreturn(E_COMPILE_ERROR,
63266325
"Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
6327-
zend_string_release_ex(standard_type_str, false);
63286326
}
6327+
/* Check for "self" and "parent" too */
6328+
if (zend_string_equals_literal_ci(standard_type_str, "self")
6329+
|| zend_string_equals_literal_ci(standard_type_str, "parent")) {
6330+
zend_error_noreturn(E_COMPILE_ERROR,
6331+
"Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
6332+
}
6333+
zend_string_release_ex(standard_type_str, false);
63296334

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

0 commit comments

Comments
 (0)