-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added default
as an expression in argument lists
#15437
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
Open
Bilge
wants to merge
15
commits into
php:master
Choose a base branch
from
Bilge:default-expr
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.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fd7ac5f
Added `default` as an expression in argument lists.
Bilge d33f2cb
Added tests and fixed memory leaks.
Bilge 66b77d6
Applied @bwoebi's grammar patch to enable T_DEFAULT in expressions.
Bilge 4ddca1d
Changed ZEND_FETCH_DEFAULT_ARG OP to call internal reflection method.
Bilge d2f35a5
Added support for passing `new class` as parameter defaults.
Bilge 80d268e
Added support for default in nested calls.
Bilge bdafc3f
Fixed inlining calls with explicit default arguments.
Bilge f88eb41
Renamed public reflection struct and method.
Bilge f0bdbfd
Added compile-time check for default passed beyond parameter limit.
Bilge 6acbffb
Fixed crash when invoking trampoline functions with default.
Bilge defe31a
Fixed default invalid context tracking.
Bilge f401390
Added support for passing default using named arguments to closures.
Bilge 04ccf4c
Fixed match allowing multiple default conditions.
Bilge 082dd45
Changed match rules to prohibit sharing conditions with the default arm.
Bilge 588a482
Optimized optimizer.
Bilge 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
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,18 @@ | ||
--TEST-- | ||
Tests passing default to anonymous class methods | ||
--FILE-- | ||
<?php | ||
|
||
$C = new class { | ||
function __invoke($V = 'Alfa') {} | ||
function F($V = 'Bravo') {} | ||
}; | ||
$C($D = default); | ||
var_dump($D); | ||
|
||
$C->F($D = default); | ||
var_dump($D); | ||
?> | ||
--EXPECT-- | ||
string(4) "Alfa" | ||
string(5) "Bravo" |
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-- | ||
Tests passing default to a callable object | ||
--FILE-- | ||
<?php | ||
|
||
class C { | ||
function __invoke($V = 'Alfa') {} | ||
} | ||
new C()($D = default); | ||
var_dump($D); | ||
?> | ||
--EXPECT-- | ||
string(4) "Alfa" |
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-- | ||
Tests passing default to closures | ||
--FILE-- | ||
<?php | ||
|
||
$F = function ($X = 1, $Y = 2) { | ||
return $X + $Y; | ||
}; | ||
var_dump($F(default, $V = default)); | ||
var_dump($V); | ||
|
||
$F2 = fn ($P = 'Alfa') => $P; | ||
var_dump($F2($V = default)); | ||
var_dump($V); | ||
?> | ||
--EXPECT-- | ||
int(3) | ||
int(2) | ||
string(4) "Alfa" | ||
string(4) "Alfa" |
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,247 @@ | ||
--TEST-- | ||
Tests an exhaustive list of valid expressions containing the default keyword | ||
--FILE-- | ||
<?php | ||
|
||
class C {} | ||
function F($V = 2) { return $V; } | ||
function FA($V = ['Alfa', 'Bravo']) { return $V; } | ||
function FE($V = new Exception) { return $V; } | ||
function FO($V = new C) { return $V; } | ||
|
||
echo "Numeric binary operators (LHS)\n"; | ||
var_dump(F(default + 1)); | ||
var_dump(F(default - 1)); | ||
var_dump(F(default * 2)); | ||
var_dump(F(default / 2)); | ||
var_dump(F(default % 2)); | ||
var_dump(F(default & 1)); | ||
var_dump(F(default | 1)); | ||
var_dump(F(default ^ 2)); | ||
var_dump(F(default << 1)); | ||
var_dump(F(default >> 1)); | ||
var_dump(F(default ** 2)); | ||
var_dump(F(default <=> 2)); | ||
|
||
echo "\nNumeric binary operators (RHS)\n"; | ||
var_dump(F(1 + default)); | ||
var_dump(F(1 - default)); | ||
var_dump(F(2 * default)); | ||
var_dump(F(2 / default)); | ||
var_dump(F(2 % default)); | ||
var_dump(F(1 & default)); | ||
var_dump(F(1 | default)); | ||
var_dump(F(2 ^ default)); | ||
var_dump(F(1 << default)); | ||
var_dump(F(1 >> default)); | ||
var_dump(F(2 ** default)); | ||
var_dump(F(2 <=> default)); | ||
|
||
echo "\nBoolean binary operators\n"; | ||
var_dump(F(default === 2)); | ||
var_dump(F(default !== 2)); | ||
var_dump(F(default == '2')); | ||
var_dump(F(default != '2')); | ||
var_dump(F(default >= 1)); | ||
var_dump(F(default <= 1)); | ||
var_dump(F(default > 1)); | ||
var_dump(F(default < 1)); | ||
var_dump(F(default && 0)); | ||
var_dump(F(default || 0)); | ||
var_dump(F(default and 0)); | ||
var_dump(F(default or 0)); | ||
var_dump(F(default xor 0)); | ||
|
||
echo "\nUnary operators\n"; | ||
var_dump(F(+default)); | ||
var_dump(F(-default)); | ||
var_dump(F(!default)); | ||
var_dump(F(~default)); | ||
|
||
echo "\nConditional expressions\n"; | ||
var_dump(F(default ? 1 : 0)); | ||
var_dump(F(1 ? default : 0)); | ||
var_dump(F(1 ? 1 : default)); | ||
var_dump(F(default ?: 0)); | ||
var_dump(F(0 ?: default)); | ||
var_dump(F(default ?? 0)); | ||
var_dump(F(null ?? default)); | ||
|
||
echo "\nVariable assignment\n"; | ||
F($V = default); var_dump($V); | ||
F($V += default); var_dump($V); | ||
F($V -= default); var_dump($V); | ||
F($V *= default); var_dump($V); | ||
F($V **= default); var_dump($V); | ||
F($V /= default); var_dump($V); | ||
F($V <<= default); var_dump($V); | ||
F($V >>= default); var_dump($V); | ||
F($V %= default); var_dump($V); | ||
F($V &= default); var_dump($V); | ||
F($V |= default); var_dump($V); | ||
F($V ^= default); var_dump($V); | ||
F($V .= default); var_dump($V); | ||
F($U ??= default); var_dump($U); | ||
FA(list($V) = default); var_dump($V); | ||
FA([, $V] = default); var_dump($V); | ||
// TODO: Variable expressions? | ||
|
||
echo "\nCasts\n"; | ||
var_dump(F((int)default)); | ||
var_dump(F((double)default)); | ||
var_dump(F((string)default)); | ||
var_dump(F((array)default)); | ||
var_dump(F((object)default)); | ||
var_dump(F((bool)default)); | ||
|
||
echo "\nParens\n"; | ||
var_dump(F((((default))))); | ||
|
||
echo "\nInternal functions\n"; | ||
var_dump(F(empty(default))); | ||
// eval() makes no sense. | ||
// exit() makes no sense. | ||
// TODO? | ||
// include | ||
// include_once | ||
// require | ||
// require_once | ||
|
||
echo "\nMatch tier\n"; | ||
var_dump(F(match(default) { default => default })); | ||
var_dump(F(match(default) { 2 => 3 })); | ||
|
||
echo "\nInstanceof tier\n"; | ||
var_dump(FO(default instanceof C)); | ||
var_dump(FO(default instanceof D)); | ||
|
||
echo "\nClone tier\n"; | ||
var_dump(FO(clone default)); | ||
|
||
echo "\nThrow tier\n"; | ||
try { | ||
FE(throw default); | ||
} catch (Exception $E) { | ||
var_dump($E::class); | ||
} | ||
|
||
echo "\nPrint tier\n"; | ||
var_dump(F(print default)); | ||
|
||
// TODO: Silence operator? | ||
|
||
?> | ||
--EXPECT-- | ||
Numeric binary operators (LHS) | ||
int(3) | ||
int(1) | ||
int(4) | ||
int(1) | ||
int(0) | ||
int(0) | ||
int(3) | ||
int(0) | ||
int(4) | ||
int(1) | ||
int(4) | ||
int(0) | ||
|
||
Numeric binary operators (RHS) | ||
int(3) | ||
int(-1) | ||
int(4) | ||
int(1) | ||
int(0) | ||
int(0) | ||
int(3) | ||
int(0) | ||
int(4) | ||
int(0) | ||
int(4) | ||
int(0) | ||
|
||
Boolean binary operators | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
bool(true) | ||
|
||
Unary operators | ||
int(2) | ||
int(-2) | ||
bool(false) | ||
int(-3) | ||
|
||
Conditional expressions | ||
int(1) | ||
int(2) | ||
int(1) | ||
int(2) | ||
int(2) | ||
int(2) | ||
int(2) | ||
|
||
Variable assignment | ||
int(2) | ||
int(4) | ||
int(2) | ||
int(4) | ||
int(16) | ||
int(8) | ||
int(32) | ||
int(8) | ||
int(0) | ||
int(0) | ||
int(2) | ||
int(0) | ||
string(2) "02" | ||
int(2) | ||
string(4) "Alfa" | ||
string(5) "Bravo" | ||
|
||
Casts | ||
int(2) | ||
float(2) | ||
string(1) "2" | ||
array(1) { | ||
[0]=> | ||
int(2) | ||
} | ||
object(stdClass)#1 (1) { | ||
["scalar"]=> | ||
int(2) | ||
} | ||
bool(true) | ||
|
||
Parens | ||
int(2) | ||
|
||
Internal functions | ||
bool(false) | ||
|
||
Match tier | ||
int(2) | ||
int(3) | ||
|
||
Instanceof tier | ||
bool(true) | ||
bool(false) | ||
|
||
Clone tier | ||
object(C)#2 (0) { | ||
} | ||
|
||
Throw tier | ||
string(9) "Exception" | ||
|
||
Print tier | ||
2int(1) |
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-- | ||
Tests passing default to a global user function parameter | ||
--FILE-- | ||
<?php | ||
|
||
function F($V = 'Alfa') { | ||
var_dump($V); | ||
} | ||
F(default); | ||
?> | ||
--EXPECT-- | ||
string(4) "Alfa" |
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-- | ||
Tests passing default to an internal function parameter | ||
--FILE-- | ||
<?php | ||
|
||
json_encode([], 0, $D = default); | ||
var_dump($D); | ||
?> | ||
--EXPECT-- | ||
int(512) |
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,9 @@ | ||
--TEST-- | ||
Tests specifying the default keyword in an invalid context throws a compile error | ||
--FILE-- | ||
<?php | ||
|
||
default; | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use default in non-argument context. 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,10 @@ | ||
--TEST-- | ||
Tests specifying the default keyword in an invalid context throws a compile error (2) | ||
--FILE-- | ||
<?php | ||
|
||
function F($V = 2) { return $V; } | ||
F(fn () => default)(); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use default in non-argument context. 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,15 @@ | ||
--TEST-- | ||
Tests default as an expression in a match condition is not treated as a default match arm | ||
--FILE-- | ||
<?php | ||
|
||
$F = fn ($V = 1) => $V; | ||
var_dump($F( | ||
match (default) { | ||
default + 1 => 2, | ||
default => default, | ||
} | ||
)); | ||
?> | ||
--EXPECT-- | ||
int(1) |
15 changes: 15 additions & 0 deletions
15
Zend/tests/default_expression/match_default_in_shared_condition.phpt
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,15 @@ | ||
--TEST-- | ||
Tests declaring a default match arm adjacent to another condition is still prohibited in an argument context | ||
--FILE-- | ||
<?php | ||
|
||
function F($V = 1) {} | ||
F( | ||
match (0) { | ||
0, | ||
default => 1, | ||
} | ||
); | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Match arms may not share conditions with the default arm in %s on line %d |
Oops, something went wrong.
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.
As mentioned off-list, this should check only for opcodes within the current call (between
fcall
andopline
), not the entire function. It would be nice to also skip nested functions, but that will require listing all init/do opcodes.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.
Thanks for the note. I am still trying to figure out how to do this.
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.
Store fcall in a local var, loop, and bump until reaching opline.
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.
There is code for that in PHP already somewhere, and now also in Xdebug: xdebug/xdebug@551b203#diff-846d4897469701b1d805f3574922dc47b632fd189087a2589b2e01e848585a8bR378 — that should probably become a PHP API function.
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.
I'll be honest, I still have no idea what to do here, and no idea how the Xdebug code relates to this. Anyway, I'll deal with this if the feature gets accepted.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks. Clearly I need to understand more about what oparrays, oplines and opcodes really are, and how they relate to each other. I tend to imagine that an oparray contains oplines which contain opcodes, but I still don't really know why oparray is even a thing, and when you would bundle oplines together.