Skip to content

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
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Zend/Optimizer/optimize_func_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ static void zend_try_inline_call(zend_op_array *op_array, zend_op *fcall, zend_o
} while (i < func->op_array.num_args);
}

for (zend_op *op = fcall + 1; op != opline; ++op) {
// Don't inline calls with explicit default arguments.
if (op->opcode == ZEND_FETCH_DEFAULT_ARG) {
return;
}
}
Copy link
Member

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 and opline), not the entire function. It would be nice to also skip nested functions, but that will require listing all init/do opcodes.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (zend_op *op = fcall + 1; op != opline; op++) {
	// Don't inline calls with explicit default arguments.
	if (op->opcode == ZEND_FETCH_DEFAULT_ARG) {
		return;
	}
}

Copy link
Author

@Bilge Bilge Aug 28, 2024

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.


if (RETURN_VALUE_USED(opline)) {
zval zv;

Expand Down
18 changes: 18 additions & 0 deletions Zend/tests/default_expression/anonymous_class.phpt
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"
13 changes: 13 additions & 0 deletions Zend/tests/default_expression/callable.phpt
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"
20 changes: 20 additions & 0 deletions Zend/tests/default_expression/closure.phpt
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"
247 changes: 247 additions & 0 deletions Zend/tests/default_expression/default_expressions.phpt
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)
12 changes: 12 additions & 0 deletions Zend/tests/default_expression/global_user_function.phpt
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"
10 changes: 10 additions & 0 deletions Zend/tests/default_expression/internal_function.phpt
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)
9 changes: 9 additions & 0 deletions Zend/tests/default_expression/invalid_context.phpt
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
10 changes: 10 additions & 0 deletions Zend/tests/default_expression/invalid_context2.phpt
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
15 changes: 15 additions & 0 deletions Zend/tests/default_expression/match_default_expr.phpt
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)
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
Loading
Loading