Skip to content

Commit b0ddbb6

Browse files
committed
Support for PFA in constant expressions
1 parent a6e220e commit b0ddbb6

18 files changed

Lines changed: 492 additions & 24 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
PFA in constexpr 001
3+
--FILE--
4+
<?php
5+
6+
function f($a = g("foo", ?)) {
7+
return $a(1);
8+
}
9+
10+
function g(string $a, int $b) {
11+
return [$a, $b];
12+
}
13+
14+
var_dump(f());
15+
var_dump(f());
16+
17+
?>
18+
--EXPECT--
19+
array(2) {
20+
[0]=>
21+
string(3) "foo"
22+
[1]=>
23+
int(1)
24+
}
25+
array(2) {
26+
[0]=>
27+
string(3) "foo"
28+
[1]=>
29+
int(1)
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
PFA in constexpr: non-constexpr arg
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(new stdClass, $x, ?)) {
7+
return $a(1);
8+
}
9+
10+
function g(string $a, int $b) {
11+
return [$a, $b];
12+
}
13+
14+
try {
15+
var_dump(f());
16+
} catch (Error $e) {
17+
echo $e::class, ": ", $e->getMessage(), " on line ", $e->getLine();
18+
}
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: Constant expression contains invalid operations in %s on line %d
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
PFA in constexpr: named args
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(c: ?, ...)) {
7+
return $a(1.5, b: 3);
8+
}
9+
10+
function g(string $a = 'hello', int $b = 2, float $c = 0) {
11+
return [$a, $b, $c];
12+
}
13+
14+
var_dump(f());
15+
16+
?>
17+
--EXPECT--
18+
array(3) {
19+
[0]=>
20+
string(5) "hello"
21+
[1]=>
22+
int(3)
23+
[2]=>
24+
float(1.5)
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
PFA in constexpr: extra named args
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(?, foo: 'bar', ...)) {
7+
return $a(1, bar: 'baz');
8+
}
9+
10+
function g(...$args) {
11+
return [$args];
12+
}
13+
14+
var_dump(f());
15+
16+
?>
17+
--EXPECT--
18+
array(1) {
19+
[0]=>
20+
array(3) {
21+
[0]=>
22+
int(1)
23+
["foo"]=>
24+
string(3) "bar"
25+
["bar"]=>
26+
string(3) "baz"
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
PFA in constexpr: variadic placeholder must be after positional params
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(..., 1)) {
7+
}
8+
9+
f();
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Variadic placeholder must be last in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
PFA in constexpr: variadic placeholder must be after named params
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(..., foo: 1)) {
7+
}
8+
9+
f();
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Variadic placeholder must be last in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
PFA in constexpr: variadic placeholder may only appear once
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(..., ...)) {
7+
}
8+
9+
f();
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Variadic placeholder may only appear once in %s on line %d
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
PFA in constexpr: variadic placeholder not allowed to be named
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(foo: ...)) {
7+
}
8+
9+
f();
10+
11+
?>
12+
--EXPECTF--
13+
Parse error: syntax error, unexpected token "..." in %s on line %d
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
PFA in constexpr: error during partial creation
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(?)) {
7+
}
8+
9+
function g($a, $b) {
10+
}
11+
12+
try {
13+
f();
14+
} catch (Error $e) {
15+
echo $e::class, ": ", $e->getMessage(), "\n";
16+
}
17+
18+
?>
19+
--EXPECT--
20+
ArgumentCountError: Partial application of g() expects exactly 2 arguments, 1 given
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
PFA in constexpr: error during arg list evaluation
3+
--FILE--
4+
<?php
5+
6+
function f($a = g(strlen(?), e: strlen(?), a: strlen(?), ...)) {
7+
}
8+
9+
function g($a, $b, $c, $d, $e) {
10+
}
11+
12+
function h($a = i(strlen(?), a: strlen(?), b: invalid(?), ...)) {
13+
}
14+
15+
function i(...$args) {
16+
}
17+
18+
try {
19+
f();
20+
} catch (Error $e) {
21+
echo $e::class, ": ", $e->getMessage(), "\n";
22+
}
23+
24+
try {
25+
h();
26+
} catch (Error $e) {
27+
echo $e::class, ": ", $e->getMessage(), "\n";
28+
}
29+
30+
?>
31+
--EXPECT--
32+
Error: Named parameter $a overwrites previous argument
33+
Error: Call to undefined function invalid()

0 commit comments

Comments
 (0)