-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Match expression #5371
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
Closed
[RFC] Match expression #5371
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b45c51b
Implement match expression
iluuu1994 7e2b0a4
Fix usage of incorrect var in test
iluuu1994 2ed10c4
Unnecessary EXPECTF
iluuu1994 8f75297
Improve unhandled match error message
iluuu1994 eb29cbe
Fix falling through match on undefined variable
iluuu1994 f96b1ce
Remove value in UnhandledMatchError message
iluuu1994 98bdbaf
Fix match opcache test
iluuu1994 21f356d
Fix BB successors of ZEND_MATCH
iluuu1994 2c84da2
Remove unused constant
iluuu1994 1137164
Revert unused change in should_use_jumptable for match
iluuu1994 c89fd6f
Remove vim folding
iluuu1994 909a003
Inline production match_arm_body
iluuu1994 c78173d
ZEND_CASE_STRICT may never throw
iluuu1994 ec09601
Fix taken successor for switch/match with mismatched type
iluuu1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Basic match expression functionality test | ||
--FILE-- | ||
<?php | ||
|
||
function wordify($x) { | ||
return match ($x) { | ||
0 => 'Zero', | ||
1 => 'One', | ||
2 => 'Two', | ||
3 => 'Three', | ||
4 => 'Four', | ||
5 => 'Five', | ||
6 => 'Six', | ||
7 => 'Seven', | ||
8 => 'Eight', | ||
9 => 'Nine', | ||
}; | ||
} | ||
|
||
for ($i = 0; $i <= 9; $i++) { | ||
print wordify($i) . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Zero | ||
One | ||
Two | ||
Three | ||
Four | ||
Five | ||
Six | ||
Seven | ||
Eight | ||
Nine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Match expression omit trailing comma | ||
--FILE-- | ||
<?php | ||
|
||
function print_bool($bool) { | ||
echo match ($bool) { | ||
true => "true\n", | ||
false => "false\n" | ||
}; | ||
} | ||
|
||
print_bool(true); | ||
print_bool(false); | ||
|
||
?> | ||
--EXPECT-- | ||
true | ||
false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
Match expression default case | ||
--FILE-- | ||
<?php | ||
|
||
function get_value($i) { | ||
return match ($i) { | ||
1 => 1, | ||
2 => 2, | ||
default => 'default', | ||
}; | ||
} | ||
|
||
echo get_value(0) . "\n"; | ||
echo get_value(1) . "\n"; | ||
echo get_value(2) . "\n"; | ||
echo get_value(3) . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
default | ||
1 | ||
2 | ||
default |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--TEST-- | ||
Match expression with true as expression | ||
--FILE-- | ||
<?php | ||
|
||
function get_range($i) { | ||
return match (true) { | ||
$i >= 50 => '50+', | ||
$i >= 40 => '40-50', | ||
$i >= 30 => '30-40', | ||
$i >= 20 => '20-30', | ||
$i >= 10 => '10-20', | ||
default => '0-10', | ||
}; | ||
} | ||
|
||
echo get_range(22) . "\n"; | ||
echo get_range(0) . "\n"; | ||
echo get_range(59) . "\n"; | ||
echo get_range(13) . "\n"; | ||
echo get_range(39) . "\n"; | ||
echo get_range(40) . "\n"; | ||
|
||
?> | ||
--EXPECT-- | ||
20-30 | ||
0-10 | ||
50+ | ||
10-20 | ||
30-40 | ||
40-50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
Match expression discarding result | ||
--FILE-- | ||
<?php | ||
|
||
match (1) { | ||
1 => print "Executed\n", | ||
}; | ||
|
||
?> | ||
--EXPECT-- | ||
Executed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
Match expression with no cases | ||
--FILE-- | ||
<?php | ||
|
||
$x = match (true) {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type bool in %s | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Match expression exception on unhandled case | ||
--FILE-- | ||
<?php | ||
|
||
function get_value($i) { | ||
return match ($i) { | ||
1 => 1, | ||
2 => 2, | ||
}; | ||
} | ||
|
||
echo get_value(1) . "\n"; | ||
echo get_value(2) . "\n"; | ||
echo get_value(3) . "\n"; | ||
|
||
?> | ||
--EXPECTF-- | ||
1 | ||
2 | ||
|
||
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in %s | ||
Stack trace: | ||
#0 %s: get_value(3) | ||
#1 {main} | ||
thrown in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
Match expression multiple conditions per case | ||
--FILE-- | ||
<?php | ||
|
||
function is_working_day($day) { | ||
return match ($day) { | ||
1, 7 => false, | ||
2, 3, 4, 5, 6 => true, | ||
}; | ||
} | ||
|
||
for ($i = 1; $i <= 7; $i++) { | ||
var_dump(is_working_day($i)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
Pretty printing for match expression | ||
--FILE-- | ||
<?php | ||
|
||
assert((function () { | ||
match ('foo') { | ||
'foo', 'bar' => false, | ||
'baz' => 'a', | ||
default => 'b', | ||
}; | ||
})()); | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: assert(): assert(function () { | ||
match ('foo') { | ||
'foo', 'bar' => false, | ||
'baz' => 'a', | ||
default => 'b', | ||
}; | ||
}()) failed in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Implicit break in match expression | ||
--FILE-- | ||
<?php | ||
|
||
function dump_and_return($string) { | ||
var_dump($string); | ||
return $string; | ||
} | ||
|
||
var_dump(match ('foo') { | ||
'foo' => dump_and_return('foo'), | ||
'bar' => dump_and_return('bar'), | ||
}); | ||
|
||
?> | ||
--EXPECT-- | ||
string(3) "foo" | ||
string(3) "foo" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Strict comparison in match expression | ||
--FILE-- | ||
<?php | ||
|
||
function wrong() { | ||
throw new Exception(); | ||
} | ||
|
||
var_dump(match (0) { | ||
null => wrong(), | ||
false => wrong(), | ||
0.0 => wrong(), | ||
[] => wrong(), | ||
'' => wrong(), | ||
0 => 'int', | ||
}); | ||
|
||
function get_value() { | ||
return 0; | ||
} | ||
|
||
var_dump(match (get_value()) { | ||
null => wrong(), | ||
false => wrong(), | ||
0.0 => wrong(), | ||
[] => wrong(), | ||
'' => wrong(), | ||
0 => 'int', | ||
default => 'default', | ||
}); | ||
|
||
?> | ||
--EXPECT-- | ||
string(3) "int" | ||
string(3) "int" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--TEST-- | ||
Test strict comparison with match expression jump table | ||
--FILE-- | ||
<?php | ||
|
||
function wrong() { | ||
throw new Exception(); | ||
} | ||
|
||
function test_int($char) { | ||
return match ($char) { | ||
0 => wrong(), | ||
1 => wrong(), | ||
2 => wrong(), | ||
3 => wrong(), | ||
4 => wrong(), | ||
5 => wrong(), | ||
6 => wrong(), | ||
7 => wrong(), | ||
8 => wrong(), | ||
9 => wrong(), | ||
default => 'Not matched', | ||
}; | ||
} | ||
|
||
foreach (range(0, 9) as $int) { | ||
var_dump((string) $int); | ||
var_dump(test_int((string) $int)); | ||
} | ||
|
||
function test_string($int) { | ||
return match ($int) { | ||
'0' => wrong(), | ||
'1' => wrong(), | ||
'2' => wrong(), | ||
'3' => wrong(), | ||
'4' => wrong(), | ||
'5' => wrong(), | ||
'6' => wrong(), | ||
'7' => wrong(), | ||
'8' => wrong(), | ||
'9' => wrong(), | ||
default => 'Not matched', | ||
}; | ||
} | ||
|
||
foreach (range(0, 9) as $int) { | ||
var_dump($int); | ||
var_dump(test_string($int)); | ||
} | ||
|
||
--EXPECT-- | ||
string(1) "0" | ||
string(11) "Not matched" | ||
string(1) "1" | ||
string(11) "Not matched" | ||
string(1) "2" | ||
string(11) "Not matched" | ||
string(1) "3" | ||
string(11) "Not matched" | ||
string(1) "4" | ||
string(11) "Not matched" | ||
string(1) "5" | ||
string(11) "Not matched" | ||
string(1) "6" | ||
string(11) "Not matched" | ||
string(1) "7" | ||
string(11) "Not matched" | ||
string(1) "8" | ||
string(11) "Not matched" | ||
string(1) "9" | ||
string(11) "Not matched" | ||
int(0) | ||
string(11) "Not matched" | ||
int(1) | ||
string(11) "Not matched" | ||
int(2) | ||
string(11) "Not matched" | ||
int(3) | ||
string(11) "Not matched" | ||
int(4) | ||
string(11) "Not matched" | ||
int(5) | ||
string(11) "Not matched" | ||
int(6) | ||
string(11) "Not matched" | ||
int(7) | ||
string(11) "Not matched" | ||
int(8) | ||
string(11) "Not matched" | ||
int(9) | ||
string(11) "Not matched" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Test match strict comparison with true expression | ||
--FILE-- | ||
<?php | ||
|
||
function wrong() { | ||
throw new Exception(); | ||
} | ||
|
||
echo match (true) { | ||
'truthy' => wrong(), | ||
['truthy'] => wrong(), | ||
new stdClass() => wrong(), | ||
1 => wrong(), | ||
1.0 => wrong(), | ||
true => "true\n", | ||
}; | ||
|
||
--EXPECT-- | ||
true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.