-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Implement match blocks #11933
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
Draft
iluuu1994
wants to merge
3
commits into
php:master
Choose a base branch
from
iluuu1994:match-blocks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[RFC] Implement match blocks #11933
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,21 @@ | ||
--TEST-- | ||
Coalesce block | ||
--FILE-- | ||
<?php | ||
$x = null; | ||
$y = $x ?? { | ||
echo "Executed\n"; | ||
42 | ||
}; | ||
var_dump($y); | ||
|
||
$x = 42; | ||
$y = $x ?? { | ||
echo "Never executed\n"; | ||
}; | ||
var_dump($y); | ||
?> | ||
--EXPECT-- | ||
Executed | ||
int(42) | ||
int(42) |
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,21 @@ | ||
--TEST-- | ||
Coalesce assignment block | ||
--FILE-- | ||
<?php | ||
$x = null; | ||
$x ??= { | ||
echo "Executed\n"; | ||
42 | ||
}; | ||
var_dump($x); | ||
|
||
$x = 42; | ||
$x ??= { | ||
echo "Never executed\n"; | ||
}; | ||
var_dump($x); | ||
?> | ||
--EXPECT-- | ||
Executed | ||
int(42) | ||
int(42) |
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,10 @@ | ||
--TEST-- | ||
Match expression block must not use return | ||
--FILE-- | ||
<?php | ||
foo(match (1) { | ||
1 => { return; } | ||
}); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Match expression whose result is used must not contain return, break, continue or goto 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,35 @@ | ||
--TEST-- | ||
Basic match blocks | ||
--FILE-- | ||
<?php | ||
function foo() { | ||
echo "foo()\n"; | ||
} | ||
|
||
function bar() { | ||
return 3; | ||
} | ||
|
||
function test($value) { | ||
var_dump(match ($value) { | ||
1 => { 1 }, | ||
2 => { | ||
$x = 2; | ||
$x | ||
}, | ||
3 => { | ||
foo(); | ||
bar() | ||
}, | ||
}); | ||
} | ||
|
||
test(1); | ||
test(2); | ||
test(3); | ||
?> | ||
--EXPECT-- | ||
int(1) | ||
int(2) | ||
foo() | ||
int(3) |
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,10 @@ | ||
--TEST-- | ||
Match expression block must not use break | ||
--FILE-- | ||
<?php | ||
var_dump(match ($value) { | ||
1 => { break; }, | ||
}); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Match expression whose result is used must not contain return, break, continue or goto 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,17 @@ | ||
--TEST-- | ||
Match expression block may use break if block is not escaped | ||
--FILE-- | ||
<?php | ||
var_dump(match (1) { | ||
1 => { | ||
foreach ([1, 2, 3] as $value) { | ||
echo $value, "\n"; | ||
break; | ||
} | ||
42 | ||
}, | ||
}); | ||
?> | ||
--EXPECT-- | ||
1 | ||
int(42) |
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 block must not use goto | ||
--FILE-- | ||
<?php | ||
var_dump(match (1) { | ||
1 => { | ||
goto after; | ||
}, | ||
}); | ||
after: | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Match expression whose result is used must not contain return, break, continue or goto 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,14 @@ | ||
--TEST-- | ||
May not goto into match expression block | ||
--FILE-- | ||
<?php | ||
goto in; | ||
var_dump(match (1) { | ||
1 => { | ||
in: | ||
42 | ||
}, | ||
}); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: 'goto' into loop or switch statement is disallowed in %s on line %d | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message should mention “match”. |
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-- | ||
Match expression block must return a value | ||
--FILE-- | ||
<?php | ||
function test() { | ||
var_dump(match (1) { | ||
1 => { | ||
echo "Not returning anything\n"; | ||
}, | ||
}); | ||
} | ||
try { | ||
test(); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
Not returning anything | ||
NULL |
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,10 @@ | ||
--TEST-- | ||
Match expression block must not use return | ||
--FILE-- | ||
<?php | ||
var_dump(match ($value) { | ||
1 => { return; }, | ||
}); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Match expression whose result is used must not contain return, break, continue or goto 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,20 @@ | ||
--TEST-- | ||
Throwing match expression block must clean up live-vars | ||
--FILE-- | ||
<?php | ||
|
||
function throw_($value) { | ||
var_dump([new \stdClass] + match ($value) { | ||
1 => { throw new Exception('Exception with live var'); }, | ||
}); | ||
} | ||
|
||
try { | ||
throw_(1); | ||
} catch (Exception $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Exception with live var |
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,16 @@ | ||
--TEST-- | ||
Match statement block may break out of block | ||
--FILE-- | ||
<?php | ||
match (1) { | ||
1 => { | ||
echo "Before break\n"; | ||
break; | ||
echo "After break\n"; | ||
}, | ||
}; | ||
echo "After match\n"; | ||
?> | ||
--EXPECT-- | ||
Before break | ||
After match |
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,17 @@ | ||
--TEST-- | ||
Match statement block may continue out of block, with a warning | ||
--FILE-- | ||
<?php | ||
match (1) { | ||
1 => { | ||
echo "Before continue\n"; | ||
continue; | ||
echo "After continue\n"; | ||
}, | ||
}; | ||
echo "After match\n"; | ||
?> | ||
--EXPECTF-- | ||
Warning: "continue" targeting switch is equivalent to "break" in %s on line %d | ||
Before continue | ||
After match |
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,17 @@ | ||
--TEST-- | ||
May escape match statement block with goto | ||
--FILE-- | ||
<?php | ||
match (1) { | ||
1 => { | ||
echo "Before goto\n"; | ||
goto after; | ||
echo "After goto\n"; | ||
}, | ||
}; | ||
after: | ||
echo "After match\n"; | ||
?> | ||
--EXPECT-- | ||
Before goto | ||
After match |
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,14 @@ | ||
--TEST-- | ||
May not goto into match statement block | ||
--FILE-- | ||
<?php | ||
goto in; | ||
match (1) { | ||
1 => { | ||
in: | ||
echo "Inside match block\n"; | ||
}, | ||
}; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: 'goto' into loop or switch statement is disallowed 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,11 @@ | ||
--TEST-- | ||
Match statement block must not return a value | ||
--FILE-- | ||
<?php | ||
match (1) { | ||
1 => { new stdClass }, | ||
}; | ||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
also possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, not in this PR. Please read the RFC, the rationale is explained there. https://wiki.php.net/rfc/match_blocks#why_not_language-level_blocks