Skip to content

[RFC] Implement block expressions #5403

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

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

print { "d\n" };

print 'e' . { 'f' };

?>
--EXPECT--
a
b
c
d
ef
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"
45 changes: 45 additions & 0 deletions Zend/tests/block_expr/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--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--
Test block expression compilation error when expression is missing
--FILE--
<?php

$a = {};

?>
--EXPECTF--
Fatal error: Block expression must return a value if its result is used 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"
12 changes: 12 additions & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int in
case ZEND_AST_USE_TRAIT:
case ZEND_AST_NAMESPACE:
case ZEND_AST_DECLARE:
case ZEND_AST_BLOCK_EXPR:
break;
default:
smart_str_appendc(str, ';');
Expand Down Expand Up @@ -1501,6 +1502,17 @@ 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);
if (ast->child[1] != NULL) {
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
31 changes: 31 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4506,6 +4506,13 @@ void zend_compile_return(zend_ast *ast) /* {{{ */
if (!expr_ast) {
expr_node.op_type = IS_CONST;
ZVAL_NULL(&expr_node.u.constant);
} else if (expr_ast->kind == ZEND_AST_BLOCK_EXPR && expr_ast->child[1] == NULL) {
expr_node.op_type = IS_CONST;
ZVAL_NULL(&expr_node.u.constant);

znode result;
zend_compile_expr(&result, expr_ast);
zend_do_free(&result);
} else if (by_ref && zend_is_variable(expr_ast)) {
zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
} else {
Expand Down Expand Up @@ -8332,6 +8339,27 @@ void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
}
/* }}} */

void zend_compile_block_expr(znode *result, zend_ast *ast) /* {{{ */
{
zend_bool result_unused = (ast->attr & ZEND_BLOCK_EXPR_RESULT_UNUSED) != 0;
zend_ast *statement_list = ast->child[0];
zend_ast *expr_ast = ast->child[1];

zend_compile_stmt(statement_list);

if (expr_ast != NULL) {
zend_compile_expr(result, expr_ast);
} else {
if (!result_unused) {
zend_error_noreturn(E_COMPILE_ERROR, "Block expression must return a value if its result is used");
}

ZVAL_NULL(&result->u.constant);
result->op_type = IS_CONST;
}
}
/* }}} */

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 @@ -8952,6 +8980,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
3 changes: 3 additions & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,9 @@ static zend_always_inline int zend_check_arg_send_type(const zend_function *zf,
/* Attribute for ternary inside parentheses */
#define ZEND_PARENTHESIZED_CONDITIONAL 1

/* Attribute for block expression at statement level */
#define ZEND_BLOCK_EXPR_RESULT_UNUSED 1

/* For "use" AST nodes and the seen symbol table */
#define ZEND_SYMBOL_CLASS (1<<0)
#define ZEND_SYMBOL_FUNCTION (1<<1)
Expand Down
Loading