Skip to content

Commit 71a3226

Browse files
committed
Prohibit pipe & arrow function combination that leads to confusing parse trees
See https://externals.io/message/128473 Closes GH-19533
1 parent 6497c6c commit 71a3226

15 files changed

+122
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ PHP NEWS
2727
. Various closure binding issues are now deprecated. (alexandre-daubois)
2828
. Fixed bug GH-18373 (Don't substitute self/parent with anonymous class).
2929
(ilutov)
30+
. Prohibit pipe & arrow function combination that leads to confusing parse
31+
trees. (ilutov)
3032

3133
- Filter:
3234
. Added support for configuring the URI parser for FILTER_VALIDATE_URL

Zend/tests/arrow_functions/gh7900.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ try {
2323
?>
2424
--EXPECT--
2525
Here
26-
assert(fn(): never => 42 && false)
26+
assert((fn(): never => 42) && false)

Zend/tests/pipe_operator/mixed_callable_call.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ $res1 = 1
7171
|> [StaticTest::class, 'times17']
7272
|> new Times23()
7373
|> $times29
74-
|> fn($x) => times2($x)
74+
|> (fn($x) => times2($x))
7575
;
7676

7777
var_dump($res1);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Pipe precedence 001
3+
--FILE--
4+
<?php
5+
6+
42
7+
|> fn($x) => $x < 42
8+
|> fn($x) => var_dump($x);
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Arrow functions on the right hand side of |> must be parenthesized in %s on line %d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Pipe precedence 002
3+
--FILE--
4+
<?php
5+
6+
42
7+
|> (fn($x) => $x < 42)
8+
|> (fn($x) => var_dump($x));
9+
10+
?>
11+
--EXPECT--
12+
bool(false)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Pipe precedence 003
3+
--FILE--
4+
<?php
5+
6+
null
7+
|> fn() => print (new Exception)->getTraceAsString() . "\n\n"
8+
|> fn() => print (new Exception)->getTraceAsString() . "\n\n";
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Arrow functions on the right hand side of |> must be parenthesized in %s on line %d
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Pipe precedence 004
3+
--FILE--
4+
<?php
5+
6+
null
7+
|> (fn() => print (new Exception)->getTraceAsString() . "\n\n")
8+
|> (fn() => print (new Exception)->getTraceAsString() . "\n\n");
9+
10+
?>
11+
--EXPECTF--
12+
#0 %s(%d): {closure:%s:%d}(NULL)
13+
#1 {main}
14+
15+
#0 %s(%d): {closure:%s:%d}(1)
16+
#1 {main}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Pipe precedence 005
3+
--FILE--
4+
<?php
5+
6+
try {
7+
assert(false && 1 |> (fn() => 2));
8+
} catch (AssertionError $e) {
9+
echo $e->getMessage(), "\n";
10+
}
11+
12+
?>
13+
--EXPECT--
14+
assert(false && 1 |> (fn() => 2))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Pipe precedence 006
3+
--FILE--
4+
<?php
5+
6+
$value = null;
7+
$value
8+
|> fn ($x) => $x ?? throw new Exception('Value may not be null')
9+
|> fn ($x) => var_dump($x);
10+
11+
?>
12+
--EXPECTF--
13+
Fatal error: Arrow functions on the right hand side of |> must be parenthesized in %s on line %d
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Pipe precedence 007
3+
--FILE--
4+
<?php
5+
6+
$value = 42;
7+
$value
8+
|> (fn ($x) => $x ?? throw new Exception('Value may not be null'))
9+
|> (fn ($x) => var_dump($x));
10+
11+
$value = null;
12+
$value
13+
|> (fn ($x) => $x ?? throw new Exception('Value may not be null'))
14+
|> (fn ($x) => var_dump($x));
15+
16+
?>
17+
--EXPECTF--
18+
int(42)
19+
20+
Fatal error: Uncaught Exception: Value may not be null in %s:%d
21+
Stack trace:
22+
#0 %s(%d): {closure:%s:%d}(NULL)
23+
#1 {main}
24+
thrown in %s on line %d

0 commit comments

Comments
 (0)