Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Zend/tests/return_types/generators001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ function test3() : Traversable {
yield 3;
}

function test4() : mixed {
yield 4;
}

function test5() : object {
yield 5;
}

function test6() : object|callable {
yield 6;
}

var_dump(
test1(),
test2(),
test3()
test3(),
test4(),
test5(),
test6(),
);
--EXPECTF--
object(Generator)#%d (%d) {
Expand All @@ -26,3 +41,9 @@ object(Generator)#%d (%d) {
}
object(Generator)#%d (%d) {
}
object(Generator)#%d (%d) {
}
object(Generator)#%d (%d) {
}
object(Generator)#%d (%d) {
}
4 changes: 2 additions & 2 deletions Zend/tests/return_types/generators002.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--TEST--
Generator return type must be Generator, Iterator or Traversable
Generator return type must be a supertype of Generator
--FILE--
<?php
function test1() : StdClass {
yield 1;
}
--EXPECTF--
Fatal error: Generators may only declare a return type containing Generator, Iterator, Traversable, or iterable, StdClass is not permitted in %s on line %d
Fatal error: Generator return type must be a supertype of Generator, StdClass given in %s on line %d
9 changes: 9 additions & 0 deletions Zend/tests/return_types/generators006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Generator return type must be a supertype of Generator (with union types)
--FILE--
<?php
function test1() : StdClass|ArrayObject|array {
yield 1;
}
--EXPECTF--
Fatal error: Generator return type must be a supertype of Generator, StdClass|ArrayObject|array given in %s on line %d
5 changes: 2 additions & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ static void zend_mark_function_as_generator() /* {{{ */

if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_type return_type = CG(active_op_array)->arg_info[-1].type;
zend_bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_ITERABLE) != 0;
zend_bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & (MAY_BE_ITERABLE | MAY_BE_OBJECT)) != 0;
if (!valid_type) {
zend_type *single_type;
ZEND_TYPE_FOREACH(return_type, single_type) {
Expand All @@ -1278,8 +1278,7 @@ static void zend_mark_function_as_generator() /* {{{ */
if (!valid_type) {
zend_string *str = zend_type_to_string(return_type);
zend_error_noreturn(E_COMPILE_ERROR,
"Generators may only declare a return type containing " \
"Generator, Iterator, Traversable, or iterable, %s is not permitted",
"Generator return type must be a supertype of Generator, %s given",
ZSTR_VAL(str));
}
}
Expand Down