Returning compatible sub generator produces a warning

Bug #69419 Returning compatible sub generator produces a warning
Submitted: 2015-04-10 13:02 UTC Modified: 2015-04-10 16:07 UTC
From: elliotlevin at hotmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: master-Git-2015-04-10 (Git) OS:
Private report: No CVE-ID: None

 [2015-04-10 13:02 UTC] elliotlevin at hotmail dot com

Description:
------------
When generators were implemented, a generator function could return another generator even if it was a return by reference generator.

In PHP7 this now produces a warning because it is not a variable.

See http://3v4l.org/sFR11#vphp7@20141101

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

function & genRefInner() {
    $var = 1;
    yield $var;
}

function & genRefOuter() {
    return genRefInner();
}

foreach(genRefOuter() as $i) {
    var_dump($i);
}

?>

Expected result:
----------------
int(1)

Actual result:
--------------
Notice: Only variable references should be returned by reference in /in/sFR11 on line 9
int(1)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [2015-04-10 15:33 UTC] nikic@php.net

The behavior in PHP 7 is correct, I'll fix it to throw a strict standards notice in 5.x as well.

 [2015-04-10 16:07 UTC] nikic@php.net

Another interesting case I didn't consider before is this:

<?php

class A {
    public function &gen() { yield; }
}
class B extends A {
    public function gen() { yield; }
}

?>

Currently this throws the usual LSP strict standards notice, because by-reference returns are covariant.

Intuitively this seem correct to me as well, as you can iterate a by-ref generator by-val, but can't iterate a by-val generator by-ref.

 [2015-04-14 14:39 UTC] nikic@php.net

-Status: Assigned +Status: Closed