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
Proposal: Make match expression a constant expression (when parts are constant) #5951
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,47 @@ | ||
--TEST-- | ||
Match expression can be used as a constant expression | ||
--FILE-- | ||
<?php | ||
|
||
// Here, this test uses strtolower because the locale dependence and change to the string | ||
// ensures PHP has to use a temporary value instead of a literal, to test memory management. | ||
// | ||
// It's already possible to throw from a constant expression (e.g. undeclared constants), | ||
// and match() can be seen as a much shorter way than changed ternary conditions on the same constant expressions. | ||
define('dynamic_value', strtolower('FOO')); | ||
function my_test($invalid = match(dynamic_value) {}) { | ||
return $invalid; | ||
} | ||
|
||
const X = match (dynamic_value) { | ||
default => 123, | ||
}; | ||
echo "X=",X,"\n"; | ||
const Y = match (dynamic_value) { | ||
'a', 'b' => 'default', | ||
default => dynamic_value . 'd', | ||
}; | ||
echo "Y=",Y,"\n"; | ||
const Z = match (dynamic_value) { | ||
'a', 'foo', 'b' => [dynamic_value], | ||
default => dynamic_value, | ||
}; | ||
echo "Z=",json_encode(Z),"\n"; | ||
|
||
echo json_encode(my_test([dynamic_value])), "\n"; | ||
for ($i = 0; $i < 2; $i++) { | ||
try { | ||
my_test(); | ||
} catch (UnhandledMatchError $e) { | ||
echo "Caught {$e->getMessage()} at line {$e->getLine()}\n"; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
X=123 | ||
Y=food | ||
Z=["foo"] | ||
["foo"] | ||
Caught Unhandled match value of type string at line 9 | ||
Caught Unhandled match value of type string at line 9 |
This file contains 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 can be used as a constant expression unless parts aren't constant | ||
--FILE-- | ||
<?php | ||
|
||
// This is an uncatchable fatal error caused by compiling this file. | ||
const X = match ($x) { | ||
default => 123, | ||
}; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Constant expression contains invalid operations in %s044.php on line 4 |
This file contains 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
This file contains 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
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.
zend_const_expr_to_zval
would also benefit from being updated to support ZEND_AST_MATCH, but it'd only matter if this were to be supported in constant expressions