Skip to content

Commit 9b33272

Browse files
committed
Allow generators to have a real return type of mixed or object
Fixes an edge case overlooked in #5313 Generators are objects, so `object` should have also been allowed in addition to `iterable` and `Traversable` Closes GH-5626
1 parent 446525c commit 9b33272

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

Zend/tests/return_types/generators001.phpt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,25 @@ function test3() : Traversable {
1414
yield 3;
1515
}
1616

17+
function test4() : mixed {
18+
yield 4;
19+
}
20+
21+
function test5() : object {
22+
yield 5;
23+
}
24+
25+
function test6() : object|callable {
26+
yield 6;
27+
}
28+
1729
var_dump(
1830
test1(),
1931
test2(),
20-
test3()
32+
test3(),
33+
test4(),
34+
test5(),
35+
test6(),
2136
);
2237
--EXPECTF--
2338
object(Generator)#%d (%d) {
@@ -26,3 +41,9 @@ object(Generator)#%d (%d) {
2641
}
2742
object(Generator)#%d (%d) {
2843
}
44+
object(Generator)#%d (%d) {
45+
}
46+
object(Generator)#%d (%d) {
47+
}
48+
object(Generator)#%d (%d) {
49+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
--TEST--
2-
Generator return type must be Generator, Iterator or Traversable
2+
Generator return type must be a supertype of Generator
33
--FILE--
44
<?php
55
function test1() : StdClass {
66
yield 1;
77
}
88
--EXPECTF--
9-
Fatal error: Generators may only declare a return type containing Generator, Iterator, Traversable, or iterable, StdClass is not permitted in %s on line %d
9+
Fatal error: Generator return type must be a supertype of Generator, StdClass given in %s on line %d
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Generator return type must be a supertype of Generator (with union types)
3+
--FILE--
4+
<?php
5+
function test1() : StdClass|ArrayObject|array {
6+
yield 1;
7+
}
8+
--EXPECTF--
9+
Fatal error: Generator return type must be a supertype of Generator, StdClass|ArrayObject|array given in %s on line %d

Zend/zend_compile.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ static void zend_mark_function_as_generator() /* {{{ */
12631263

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

0 commit comments

Comments
 (0)