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

From: Date: Wed, 04 Aug 2021 11:29:26 +0000
Subject: Bug #81330 [NEW]: Generator does not allow return null with implicit yield
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235588@lists.php.net to get a copy of this message
From:             greedy dot ivan at gmail dot com
Operating system: All
PHP version:      8.0.9
Package:          *General Issues
Bug Type:         Bug
Bug description:Generator does not allow return null with implicit yield

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 bug report at https://bugs.php.net/bug.php?id=81330&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81330&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81330&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81330&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81330&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81330&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81330&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81330&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81330&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81330&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81330&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81330&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81330&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81330&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81330&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81330&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81330&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81330&r=mysqlcfg


Thread (9 messages)

« previous php.bugs (#235588) next »