Skip to content

Commit aaf836b

Browse files
committed
Revert to Rust syntax, add implicit default NULL
1 parent 11ea463 commit aaf836b

15 files changed

+597
-622
lines changed

Zend/Optimizer/dce.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ static inline bool may_have_side_effects(
154154
case ZEND_INCLUDE_OR_EVAL:
155155
case ZEND_THROW:
156156
case ZEND_MATCH_ERROR:
157-
case ZEND_MATCH_BLOCK_NO_VALUE_ERROR:
158157
case ZEND_EXT_STMT:
159158
case ZEND_EXT_FCALL_BEGIN:
160159
case ZEND_EXT_FCALL_END:

Zend/tests/match/block_basic.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function bar() {
1212

1313
function test($value) {
1414
var_dump(match ($value) {
15-
1 => { <- 1; },
15+
1 => { 1 },
1616
2 => {
1717
$x = 2;
18-
<- $x;
18+
$x
1919
},
2020
3 => {
2121
foo();
22-
<- bar();
22+
bar()
2323
},
2424
});
2525
}

Zend/tests/match/block_expr_break_no_escape.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var_dump(match (1) {
88
echo $value, "\n";
99
break;
1010
}
11-
<- 42;
11+
42
1212
},
1313
});
1414
?>

Zend/tests/match/block_expr_goto_into.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ goto in;
66
var_dump(match (1) {
77
1 => {
88
in:
9-
<- 42;
9+
42
1010
},
1111
});
1212
?>

Zend/tests/match/block_expr_no_result.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ try {
1717
?>
1818
--EXPECT--
1919
Not returning anything
20-
Match expected a value from block but none was returned
20+
NULL

Zend/tests/match/block_stmt_with_result.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Match statement block must not return a value
33
--FILE--
44
<?php
55
match (1) {
6-
1 => { <- 42; },
6+
1 => { new stdClass },
77
};
88
?>
9-
--EXPECTF--
10-
Fatal error: Blocks of match expression with unused result must not return a value in %s on line %d
9+
===DONE===
10+
--EXPECT--
11+
===DONE===

Zend/zend_ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ enum _zend_ast_kind {
148148
ZEND_AST_ATTRIBUTE,
149149
ZEND_AST_MATCH,
150150
ZEND_AST_MATCH_ARM,
151-
ZEND_AST_MATCH_ARM_BLOCK,
151+
ZEND_AST_BLOCK_EXPR,
152152
ZEND_AST_NAMED_ARG,
153153

154154
/* 3 child nodes */

Zend/zend_compile.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6057,26 +6057,7 @@ static void zend_compile_match(znode *result, zend_ast *ast)
60576057
}
60586058

60596059
znode body_node;
6060-
if (body_ast->kind == ZEND_AST_MATCH_ARM_BLOCK) {
6061-
bool prev_in_block_expr = CG(context).in_block_expr;
6062-
CG(context).in_block_expr = true;
6063-
zend_compile_stmt_list(body_ast->child[0]);
6064-
zend_ast *result_expr_ast = body_ast->child[1];
6065-
if (!result && result_expr_ast) {
6066-
zend_error_noreturn(E_COMPILE_ERROR, "Blocks of match expression with unused result must not return a value");
6067-
}
6068-
if (result_expr_ast) {
6069-
zend_compile_expr(&body_node, result_expr_ast);
6070-
} else if (result) {
6071-
zend_emit_op(NULL, ZEND_MATCH_BLOCK_NO_VALUE_ERROR, NULL, NULL);
6072-
body_node.op_type = IS_CONST;
6073-
ZVAL_NULL(&body_node.u.constant);
6074-
}
6075-
CG(context).in_block_expr = prev_in_block_expr;
6076-
} else {
6077-
zend_compile_expr(&body_node, body_ast);
6078-
}
6079-
6060+
zend_compile_expr(&body_node, body_ast);
60806061
if (result) {
60816062
if (is_first_case) {
60826063
zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
@@ -6085,6 +6066,8 @@ static void zend_compile_match(znode *result, zend_ast *ast)
60856066
zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
60866067
SET_NODE(opline_qm_assign->result, result);
60876068
}
6069+
} else {
6070+
zend_do_free(&body_node);
60886071
}
60896072

60906073
jmp_end_opnums[i] = zend_emit_jump(0);
@@ -10510,6 +10493,21 @@ static void zend_compile_stmt(zend_ast *ast) /* {{{ */
1051010493
}
1051110494
/* }}} */
1051210495

10496+
static void zend_compile_block_expr(znode *result, zend_ast *ast)
10497+
{
10498+
bool prev_in_block_expr = CG(context).in_block_expr;
10499+
CG(context).in_block_expr = true;
10500+
zend_compile_stmt_list(ast->child[0]);
10501+
zend_ast *result_expr_ast = ast->child[1];
10502+
if (result_expr_ast) {
10503+
zend_compile_expr(result, result_expr_ast);
10504+
} else {
10505+
result->op_type = IS_CONST;
10506+
ZVAL_NULL(&result->u.constant);
10507+
}
10508+
CG(context).in_block_expr = prev_in_block_expr;
10509+
}
10510+
1051310511
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
1051410512
{
1051510513
/* CG(zend_lineno) = ast->lineno; */
@@ -10648,6 +10646,9 @@ static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
1064810646
case ZEND_AST_MATCH:
1064910647
zend_compile_match(result, ast);
1065010648
return;
10649+
case ZEND_AST_BLOCK_EXPR:
10650+
zend_compile_block_expr(result, ast);
10651+
return;
1065110652
default:
1065210653
ZEND_ASSERT(0 /* not supported */);
1065310654
}

Zend/zend_execute.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -887,11 +887,6 @@ ZEND_COLD void zend_match_unhandled_error(const zval *value)
887887
smart_str_free(&msg);
888888
}
889889

890-
ZEND_COLD void zend_match_block_no_value_error(void)
891-
{
892-
zend_throw_exception_ex(zend_ce_unhandled_match_error, 0, "Match expected a value from block but none was returned");
893-
}
894-
895890
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(
896891
const zend_property_info *info) {
897892
zend_throw_error(NULL, "Cannot modify readonly property %s::$%s",

Zend/zend_language_parser.y

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
277277
%type <ast> inline_function union_type_element union_type intersection_type
278278
%type <ast> attributed_statement attributed_class_statement attributed_parameter
279279
%type <ast> attribute_decl attribute attributes attribute_group namespace_declaration_name
280-
%type <ast> match match_arm_list non_empty_match_arm_list match_arm match_arm_cond_list match_arm_body
280+
%type <ast> match match_arm_list non_empty_match_arm_list match_arm match_arm_cond_list match_arm_body block_expr
281281
%type <ast> enum_declaration_statement enum_backing_type enum_case enum_case_expr
282282
%type <ast> function_name non_empty_member_modifiers
283283

@@ -740,8 +740,11 @@ match_arm_cond_list:
740740

741741
match_arm_body:
742742
expr { $$ = $1; }
743-
| '{' inner_statement_list '}' { $$ = zend_ast_create(ZEND_AST_MATCH_ARM_BLOCK, $2, NULL); }
744-
| '{' inner_statement_list T_THIN_ARROW_LEFT optional_expr ';' '}' { $$ = zend_ast_create(ZEND_AST_MATCH_ARM_BLOCK, $2, $4); }
743+
| block_expr { $$ = $1; }
744+
;
745+
746+
block_expr:
747+
'{' inner_statement_list optional_expr '}' { $$ = zend_ast_create(ZEND_AST_BLOCK_EXPR, $2, $3); }
745748
;
746749

747750
while_statement:

Zend/zend_vm_def.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9223,13 +9223,6 @@ ZEND_VM_COLD_CONST_HANDLER(197, ZEND_MATCH_ERROR, CONST|TMPVARCV, UNUSED)
92239223
HANDLE_EXCEPTION();
92249224
}
92259225

9226-
ZEND_VM_COLD_CONST_HANDLER(204, ZEND_MATCH_BLOCK_NO_VALUE_ERROR, UNUSED, UNUSED)
9227-
{
9228-
SAVE_OPLINE();
9229-
zend_match_block_no_value_error();
9230-
HANDLE_EXCEPTION();
9231-
}
9232-
92339226
ZEND_VM_COLD_CONSTCONST_HANDLER(189, ZEND_IN_ARRAY, CONST|TMP|VAR|CV, CONST, NUM)
92349227
{
92359228
USE_OPLINE

0 commit comments

Comments
 (0)