Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Zend/tests/short_foreach/short_foreach.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
array test
--FILE--
<?php

foreach([1,2,3,4]) {
echo 'a';
}


foreach([]) {
echo 'a';
}

$range = range(1,5);

foreach($range) {
echo 'c';
}

const RANGE = [1, 2, 3];

foreach(RANGE) {
echo 'c';
}



?>
--EXPECT--
aaaacccccccc
27 changes: 27 additions & 0 deletions Zend/tests/short_foreach/short_foreach_generator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
array test
--FILE--
<?php

function generator() {
yield from [0,1,2,3];
}

foreach(generator()) {
echo 'a';
}

function generator2() {
for($i=0;$i<4;$i++) {
yield $i;
}
}

foreach(generator2()) {
echo 'b';
}


?>
--EXPECT--
aaaabbbb
24 changes: 24 additions & 0 deletions Zend/tests/short_foreach/short_foreach_iterator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
array test
--FILE--
<?php
class ArrayIteratorEx extends ArrayIterator
{
function current(): mixed
{
return ArrayIterator::current();
}
}
$it = new ArrayIteratorEx(range(0,3));

foreach(new IteratorIterator($it) )
{
echo 'a';
}



?>
--EXPECT--


153 changes: 92 additions & 61 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5677,89 +5677,120 @@ static void zend_compile_foreach(zend_ast *ast) /* {{{ */
zend_ast *value_ast = ast->child[1];
zend_ast *key_ast = ast->child[2];
zend_ast *stmt_ast = ast->child[3];
bool by_ref = value_ast->kind == ZEND_AST_REF;
bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);

znode expr_node, reset_node, value_node, key_node;
zend_op *opline;
uint32_t opnum_reset, opnum_fetch;

if (key_ast) {
if (key_ast->kind == ZEND_AST_REF) {
zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
}
if (key_ast->kind == ZEND_AST_ARRAY) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
}
}
if(!value_ast) {

if (by_ref) {
value_ast = value_ast->child[0];
}
zend_compile_expr(&expr_node, expr_ast);
opnum_reset = get_next_op_number();
opline = zend_emit_op(&reset_node, ZEND_FE_RESET_R, &expr_node, NULL);

if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
by_ref = 1;
}
zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);

if (by_ref && is_variable) {
zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
} else {
zend_compile_expr(&expr_node, expr_ast);
}
opnum_fetch = get_next_op_number();
opline = zend_emit_op(NULL, ZEND_FE_FETCH_R, &reset_node, NULL);

if (by_ref) {
zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
}
opline->op2_type = IS_VAR;
opline->op2.var = get_temporary_variable();
GET_NODE(&value_node, opline->op2);

zend_compile_stmt(stmt_ast);

CG(zend_lineno) = ast->lineno;
zend_emit_jump(opnum_fetch);

opnum_reset = get_next_op_number();
opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
opline = &CG(active_op_array)->opcodes[opnum_reset];
opline->op2.opline_num = get_next_op_number();

zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
opline = &CG(active_op_array)->opcodes[opnum_fetch];
opline->extended_value = get_next_op_number();

opnum_fetch = get_next_op_number();
opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
zend_end_loop(opnum_fetch, &reset_node);

opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);

if (is_this_fetch(value_ast)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
} else if (value_ast->kind == ZEND_AST_VAR &&
zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
SET_NODE(opline->op2, &value_node);
} else {
opline->op2_type = IS_VAR;
opline->op2.var = get_temporary_variable();
GET_NODE(&value_node, opline->op2);
if (value_ast->kind == ZEND_AST_ARRAY) {
zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
} else if (by_ref) {
zend_emit_assign_ref_znode(value_ast, &value_node);
bool by_ref = value_ast->kind == ZEND_AST_REF;
bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);

if (key_ast) {
if (key_ast->kind == ZEND_AST_REF) {
zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
}
if (key_ast->kind == ZEND_AST_ARRAY) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
}
}

if (by_ref) {
value_ast = value_ast->child[0];
}

if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
by_ref = 1;
}

if (by_ref && is_variable) {
zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
} else {
zend_emit_assign_znode(value_ast, &value_node);
zend_compile_expr(&expr_node, expr_ast);
}
}

if (key_ast) {
opline = &CG(active_op_array)->opcodes[opnum_fetch];
zend_make_tmp_result(&key_node, opline);
zend_emit_assign_znode(key_ast, &key_node);
}
if (by_ref) {
zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
}

zend_compile_stmt(stmt_ast);
opnum_reset = get_next_op_number();
opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);

/* Place JMP and FE_FREE on the line where foreach starts. It would be
* better to use the end line, but this information is not available
* currently. */
CG(zend_lineno) = ast->lineno;
zend_emit_jump(opnum_fetch);
zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);

opline = &CG(active_op_array)->opcodes[opnum_reset];
opline->op2.opline_num = get_next_op_number();
opnum_fetch = get_next_op_number();
opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
if (is_this_fetch(value_ast)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
} else if (value_ast->kind == ZEND_AST_VAR &&
zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
SET_NODE(opline->op2, &value_node);
} else {
opline->op2_type = IS_VAR;
opline->op2.var = get_temporary_variable();
GET_NODE(&value_node, opline->op2);
if (value_ast->kind == ZEND_AST_ARRAY) {
zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
} else if (by_ref) {
zend_emit_assign_ref_znode(value_ast, &value_node);
} else {
zend_emit_assign_znode(value_ast, &value_node);
}
}

if (key_ast) {
opline = &CG(active_op_array)->opcodes[opnum_fetch];
zend_make_tmp_result(&key_node, opline);
zend_emit_assign_znode(key_ast, &key_node);
}

opline = &CG(active_op_array)->opcodes[opnum_fetch];
opline->extended_value = get_next_op_number();
zend_compile_stmt(stmt_ast);

/* Place JMP and FE_FREE on the line where foreach starts. It would be
* better to use the end line, but this information is not available
* currently. */
CG(zend_lineno) = ast->lineno;
zend_emit_jump(opnum_fetch);

opline = &CG(active_op_array)->opcodes[opnum_reset];
opline->op2.opline_num = get_next_op_number();

opline = &CG(active_op_array)->opcodes[opnum_fetch];
opline->extended_value = get_next_op_number();

zend_end_loop(opnum_fetch, &reset_node);
zend_end_loop(opnum_fetch, &reset_node);

opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
}
}
/* }}} */

Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ statement:
| T_UNSET '(' unset_variables possible_comma ')' ';' { $$ = $3; }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); }
| T_FOREACH '(' expr ')' foreach_statement
{ $$ = zend_ast_create(ZEND_AST_FOREACH, $3, NULL, NULL, $5); }
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')'
foreach_statement
{ $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $7, $5, $9); }
Expand Down