Skip to content

Commit 4272247

Browse files
committed
Prohibit pipe & arrow function combination that leads to confusing parse trees
1 parent 9c754ba commit 4272247

File tree

14 files changed

+120
-3
lines changed

14 files changed

+120
-3
lines changed

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

Zend/zend_ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
20702070
case ZEND_AST_ARROW_FUNC:
20712071
case ZEND_AST_METHOD:
20722072
decl = (const zend_ast_decl *) ast;
2073+
if (decl->kind == ZEND_AST_ARROW_FUNC && (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
2074+
smart_str_appendc(str, '(');
2075+
}
20732076
if (decl->child[4]) {
20742077
bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
20752078
zend_ast_export_attributes(str, decl->child[4], indent, newlines);
@@ -2113,6 +2116,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
21132116
}
21142117
smart_str_appends(str, " => ");
21152118
zend_ast_export_ex(str, body, 0, indent);
2119+
if (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC) {
2120+
smart_str_appendc(str, ')');
2121+
}
21162122
break;
21172123
}
21182124

0 commit comments

Comments
 (0)