Skip to content

Commit 3ccbc43

Browse files
committed
Implement match blocks
1 parent 0fd226c commit 3ccbc43

18 files changed

+310
-28
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Match expression block must not use return
3+
--FILE--
4+
<?php
5+
foo(match (1) {
6+
1 => { return; null }
7+
});
8+
?>
9+
--EXPECTF--
10+
Fatal error: Match expression whose result is used must not contain return, break, continue or goto in %s on line %d

Zend/tests/match/block_basic.phpt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Basic match blocks
3+
--FILE--
4+
<?php
5+
function foo() {
6+
echo "foo()\n";
7+
}
8+
9+
function bar() {
10+
return 3;
11+
}
12+
13+
function test($value) {
14+
var_dump(match ($value) {
15+
1 => { 1 },
16+
2 => { $x = 2; $x },
17+
3 => {
18+
foo();
19+
bar()
20+
},
21+
});
22+
}
23+
24+
test(1);
25+
test(2);
26+
test(3);
27+
?>
28+
--EXPECT--
29+
int(1)
30+
int(2)
31+
foo()
32+
int(3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Match expression block must not use break
3+
--FILE--
4+
<?php
5+
var_dump(match ($value) {
6+
1 => { break; [] },
7+
});
8+
?>
9+
--EXPECTF--
10+
Fatal error: Match expression whose result is used must not contain return, break, continue or goto in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Match expression block may use break if block is not escaped
3+
--FILE--
4+
<?php
5+
var_dump(match (1) {
6+
1 => {
7+
foreach ([1, 2, 3] as $value) {
8+
echo $value, "\n";
9+
break;
10+
}
11+
42
12+
},
13+
});
14+
?>
15+
--EXPECT--
16+
1
17+
int(42)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Match expression block must not use goto
3+
--FILE--
4+
<?php
5+
var_dump(match (1) {
6+
1 => {
7+
goto after;
8+
42
9+
},
10+
});
11+
after:
12+
?>
13+
--EXPECTF--
14+
Fatal error: Match expression whose result is used must not contain return, break, continue or goto in %s on line %d
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
May not goto into match expression block
3+
--FILE--
4+
<?php
5+
goto in;
6+
var_dump(match (1) {
7+
1 => {
8+
in:
9+
42
10+
},
11+
});
12+
?>
13+
--EXPECTF--
14+
Fatal error: 'goto' into loop or switch statement is disallowed in %s on line %d
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Match expression block must return a value
3+
--FILE--
4+
<?php
5+
var_dump(match (1) {
6+
1 => {
7+
echo "Not returning anything\n";
8+
},
9+
});
10+
?>
11+
--EXPECTF--
12+
Fatal error: Blocks of match expression with used result must return a value. Did you mean to omit the last semicolon? in %s on line %d
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Match expression block must not use return
3+
--FILE--
4+
<?php
5+
var_dump(match ($value) {
6+
1 => { return; [] },
7+
});
8+
?>
9+
--EXPECTF--
10+
Fatal error: Match expression whose result is used must not contain return, break, continue or goto in %s on line %d
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Throwing match expression block must clean up live-vars
3+
--FILE--
4+
<?php
5+
6+
function throw_($value) {
7+
var_dump([new \stdClass] + match ($value) {
8+
1 => { throw new Exception('Exception with live var'); [] },
9+
});
10+
}
11+
12+
try {
13+
throw_(1);
14+
} catch (Exception $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
18+
?>
19+
--EXPECT--
20+
Exception with live var
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Match statement block may break out of block
3+
--FILE--
4+
<?php
5+
match (1) {
6+
1 => {
7+
echo "Before break\n";
8+
break;
9+
echo "After break\n";
10+
},
11+
};
12+
echo "After match\n";
13+
?>
14+
--EXPECT--
15+
Before break
16+
After match
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Match statement block may continue out of block, with a warning
3+
--FILE--
4+
<?php
5+
match (1) {
6+
1 => {
7+
echo "Before continue\n";
8+
continue;
9+
echo "After continue\n";
10+
},
11+
};
12+
echo "After match\n";
13+
?>
14+
--EXPECTF--
15+
Warning: "continue" targeting switch is equivalent to "break" in %s on line %d
16+
Before continue
17+
After match
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
May escape match statement block with goto
3+
--FILE--
4+
<?php
5+
match (1) {
6+
1 => {
7+
echo "Before goto\n";
8+
goto after;
9+
echo "After goto\n";
10+
},
11+
};
12+
after:
13+
echo "After match\n";
14+
?>
15+
--EXPECT--
16+
Before goto
17+
After match
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
May not goto into match statement block
3+
--FILE--
4+
<?php
5+
goto in;
6+
match (1) {
7+
1 => {
8+
in:
9+
echo "Inside match block\n";
10+
},
11+
};
12+
?>
13+
--EXPECTF--
14+
Fatal error: 'goto' into loop or switch statement is disallowed in %s on line %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Match statement block must not return a value
3+
--FILE--
4+
<?php
5+
match (1) {
6+
1 => { 42 },
7+
};
8+
?>
9+
--EXPECTF--
10+
Fatal error: Blocks of match expression with unused result must not return a value in %s on line %d

Zend/zend_ast.h

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +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,
151152
ZEND_AST_NAMED_ARG,
152153

153154
/* 3 child nodes */

0 commit comments

Comments
 (0)