php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71252 yield from ignores yield-by-reference
Submitted: 2015-12-30 23:24 UTC Modified: -
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nikic@php.net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.0Git-2015-12-30 (Git) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
41 - 1 = ?
Subscribe to this entry?

 
 [2015-12-30 23:24 UTC] nikic@php.net
Description:
------------
When "yield from" was implemented, we did not consider how it interacts with yield-by-reference. Right now it is simply ignored and "yield from" yields all values by-value.

Likely this is not the expected or intended behavior. Instead "yield from" in a by-reference generator should yield array values and values from by-reference generators by-reference. "yield from" should generate an error for iterators (and generators) that do not support by-reference iteration.

Test script:
---------------
<?php
function &gen(array $array) {
    yield from $array;
}

$array = [1, 2, 3];
foreach (gen($array) as &$v) {
    $v += 1;
}
var_dump($array);

Expected result:
----------------
array(3) {
  [0]=>
  int(2)
  [1]=>
  int(3)
  [2]=>
  int(4)
}


Actual result:
--------------
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 13:01:24 2019 UTC