Skip to content

[RFC] Implement block expressions - alternative implementation #5407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 32 additions & 0 deletions Zend/tests/block_expr/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Test basic basic block expression
--FILE--
<?php

echo {
echo "a\n";
echo "b\n";
"c\n"
};

echo { "d\n" };

echo 'e' . { "f\n" };

echo { 'g' } . "h\n";

echo {
echo "i\n";
{ "j\n" }
};

?>
--EXPECT--
a
b
c
d
ef
gh
i
j
43 changes: 43 additions & 0 deletions Zend/tests/block_expr/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test block expression in arrow functions
--FILE--
<?php

$a = 'a';

var_dump((fn() => {
var_dump('b');
var_dump('c');
$a
})());

var_dump((fn() => {
var_dump('d');
var_dump('e');
return 'f';
})());

var_dump((fn() => {
var_dump('g');
var_dump('h');
})());

var_dump((fn() => {})());

var_dump((fn() => {
'i'
})());

?>
--EXPECT--
string(1) "b"
string(1) "c"
string(1) "a"
string(1) "d"
string(1) "e"
string(1) "f"
string(1) "g"
string(1) "h"
NULL
NULL
string(1) "i"
43 changes: 43 additions & 0 deletions Zend/tests/block_expr/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test block expression pretty printing
--FILE--
<?php

assert((function () {
return {
echo 'Foo' . PHP_EOL;
{
echo 'Bar' . PHP_EOL;
}
false
};
})());

assert(10 + {
echo 'Foo';
-10
});

assert({ false });

?>
--EXPECTF--
Foo
Bar

Warning: assert(): assert(function () {
return {
echo 'Foo' . PHP_EOL;
echo 'Bar' . PHP_EOL;
false
};
}()) failed in %s on line %d
Foo
Warning: assert(): assert(10 + {
echo 'Foo';
-10
}) failed in %s on line %d

Warning: assert(): assert({
false
}) failed in %s on line %d
10 changes: 10 additions & 0 deletions Zend/tests/block_expr/004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Parse error when expression in block expression is missing
--FILE--
<?php

$a = {};

?>
--EXPECTF--
Parse error: syntax error, unexpected '}' in %s on line %d
26 changes: 26 additions & 0 deletions Zend/tests/block_expr/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Test exceptions in block expression
--SKIPIF--
<?php
die("skip this test because it leaks memory");
?>
--FILE--
<?php

try {
$ary = [];
($ary + [1]) + { throw new Exception('a'); null };
} catch (Throwable $e) {
var_dump($e->getMessage());
}

try {
throw new Exception({ throw new Exception('b'); null });
} catch (Throwable $e) {
var_dump($e->getMessage());
}

?>
--EXPECT--
string(1) "a"
string(1) "b"
16 changes: 16 additions & 0 deletions Zend/tests/block_expr/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Parse error when semicolon after block expression is missing
--FILE--
<?php

function foo() {
{
echo "Foo\n";
echo "Bar\n";
'Baz'
}
}

?>
--EXPECTF--
Parse error: syntax error, unexpected '}' in %s on line %d
9 changes: 9 additions & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,15 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
case ZEND_AST_TRAIT_ADAPTATIONS:
zend_ast_export_stmt(str, ast, indent);
break;
case ZEND_AST_BLOCK_EXPR:
smart_str_appends(str, "{\n");
zend_ast_export_stmt(str, ast->child[0], indent + 1);
zend_ast_export_indent(str, indent + 1);
zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
smart_str_appendc(str, '\n');
zend_ast_export_indent(str, indent);
smart_str_appendc(str, '}');
break;
case ZEND_AST_IF:
zend_ast_export_if_stmt(str, (zend_ast_list*)ast, indent);
break;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ enum _zend_ast_kind {
ZEND_AST_YIELD,
ZEND_AST_COALESCE,
ZEND_AST_ASSIGN_COALESCE,
ZEND_AST_BLOCK_EXPR,

ZEND_AST_STATIC,
ZEND_AST_WHILE,
Expand Down
13 changes: 13 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8334,6 +8334,16 @@ void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
}
/* }}} */

void zend_compile_block_expr(znode *result, zend_ast *ast) /* {{{ */
{
zend_ast *statement_list = ast->child[0];
zend_ast *expr_ast = ast->child[1];

zend_compile_stmt(statement_list);
zend_compile_expr(result, expr_ast);
}
/* }}} */

static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
{
if (num == 0) {
Expand Down Expand Up @@ -8954,6 +8964,9 @@ void zend_compile_expr(znode *result, zend_ast *ast) /* {{{ */
case ZEND_AST_ARROW_FUNC:
zend_compile_func_decl(result, ast, 0);
return;
case ZEND_AST_BLOCK_EXPR:
zend_compile_block_expr(result, ast);
return;
default:
ZEND_ASSERT(0 /* not supported */);
}
Expand Down
10 changes: 10 additions & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ expr:
| T_YIELD_FROM expr { $$ = zend_ast_create(ZEND_AST_YIELD_FROM, $2); CG(extra_fn_flags) |= ZEND_ACC_GENERATOR; }
| inline_function { $$ = $1; }
| T_STATIC inline_function { $$ = $2; ((zend_ast_decl *) $$)->flags |= ZEND_ACC_STATIC; }
| '{' inner_statement_list expr '}' { $$ = zend_ast_create(ZEND_AST_BLOCK_EXPR, $2, $3); }
;


Expand All @@ -1036,6 +1037,15 @@ inline_function:
zend_ast_create(ZEND_AST_RETURN, $11), $6);
((zend_ast_decl *) $$)->lex_pos = $10;
CG(extra_fn_flags) = $9; }
| fn returns_ref '(' parameter_list ')' return_type backup_doc_comment T_DOUBLE_ARROW backup_fn_flags backup_lex_pos '{' inner_statement_list '}' backup_fn_flags {
zval zv; ZVAL_NULL(&zv);
zend_ast *null_ast = zend_ast_create_zval(&zv);
$$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $14, $1, $7,
zend_string_init("{closure}", sizeof("{closure}") - 1, 0), $4, NULL,
zend_ast_create(ZEND_AST_RETURN, zend_ast_create(ZEND_AST_BLOCK_EXPR, $12, null_ast)), $6);
((zend_ast_decl *) $$)->lex_pos = $10;
CG(extra_fn_flags) = $9;
}
;

fn:
Expand Down