Skip to content

Commit fd86e50

Browse files
committed
Fix various unhandled cases where AT is part of a union type
1 parent ff8e375 commit fd86e50

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Associated type cannot be in union (nullable type union with ?)
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
type T;
8+
public function foo(?T $param): ?T;
9+
}
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Associated type cannot be part of a union type in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Associated type cannot be in union (forced allowed null)
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
type T;
8+
public function foo(T $param = null): T;
9+
}
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Associated type cannot be part of a union type (implicitly nullable due to default null value) in %s on line %d

Zend/tests/type_declarations/associated/big_example.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ final class StringTable
9999

100100
?>
101101
--EXPECTF--
102-
Fatal error: Declaration of Sequence\StringTableSequence::next(): ?Sequence\StringTablePair must be compatible with Sequence\Sequence::next(): <Item : object|array|string|int|float|bool>|null in %s on line %d
102+
Fatal error: Associated type cannot be part of a union type in %s on line %d

Zend/zend_compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7384,6 +7384,12 @@ static zend_type zend_compile_typename_ex(
73847384
if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
73857385
zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
73867386
}
7387+
if (ZEND_TYPE_IS_ASSOCIATED(type) && is_marked_nullable) {
7388+
zend_error_noreturn(E_COMPILE_ERROR, "Associated type cannot be part of a union type");
7389+
}
7390+
if (ZEND_TYPE_IS_ASSOCIATED(type) && force_allow_null) {
7391+
zend_error_noreturn(E_COMPILE_ERROR, "Associated type cannot be part of a union type (implicitly nullable due to default null value)");
7392+
}
73877393

73887394
if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
73897395
*forced_allow_null = true;

0 commit comments

Comments
 (0)