Bug #81330 [Com]: Generator does not allow return null with implicit yield

From: Date: Wed, 04 Aug 2021 12:25:36 +0000
Subject: Bug #81330 [Com]: Generator does not allow return null with implicit yield
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235594@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81330&edit=1

 ID:                 81330
 Comment by:         greedy dot ivan at gmail dot com
 Reported by:        greedy dot ivan at gmail dot com
 Summary:            Generator does not allow return null with implicit
                     yield
 Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   All
 PHP Version:        8.0.9
 Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

There are three options here.

1. There is an yield operator inside a function. Parser gets it and allow to return null as a valid
end for generator.

2. There is no yield operator inside a function but there is a return instruction that returns
Generator. This line works fine.

3. The same as 2, but we have a line, that returns null. It will throws Error, because Parser
doesn't allow return an empty Generator such way. It doesn't get, that function return
Generator at all, because there is no yield operation inside function itself.

So, we must use some tricks to return empty Generator in this case, or use yield from
func() instead for return func().


Previous Comments:
------------------------------------------------------------------------
[2021-08-04 11:57:48] cmb@php.net

That type check is done at runtime, not compile time.  And there
is no implicit yield.

------------------------------------------------------------------------
[2021-08-04 11:49:42] greedy dot ivan at gmail dot com

It doesn't throw error on all other cases.

It will throw Type error only in case, when yield is implicit and null is returned.

------------------------------------------------------------------------
[2021-08-04 11:39:32] cmb@php.net

If the generator function does not necessarily return a Generator,
you shouldn't declare it as such.

------------------------------------------------------------------------
[2021-08-04 11:29:25] greedy dot ivan at gmail dot com

Description:
------------
If there is not explicit yield instruction in Generator, it will throw TypeError message when null
is returned.

https://3v4l.org/EpIjA

Test script:
---------------
<?php

function foo($flag = false): \Generator
{
    if ($flag) {
        return null;
    }
    
    yield from [1, 2];
}

function bar($flag = false): \Generator
{
    if ($flag) {
        return null;
    }
    
    return internal();
}

function internal(): \Generator
{
    yield from [1, 2];
}

foreach (foo() as $v){}
foreach (foo(true) as $v){}
foreach (bar() as $v){}
foreach (bar(true) as $v){}

Expected result:
----------------
No error 

Actual result:
--------------
Fatal error: Uncaught TypeError: bar(): Return value must be of type Generator, null returned in
/in/EpIjA:15


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81330&edit=1


Thread (9 messages)

« previous php.bugs (#235594) next »