Skip to content

Commit aae68be

Browse files
committed
Fix "Arrow functions on the right hand side of |> must be parenthesized"
1 parent cdac4cf commit aae68be

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848

4949
- name: Install dependencies
5050
run: >
51-
curl -sSL https://baltocdn.com/xp-framework/xp-runners/distribution/downloads/e/entrypoint/xp-run-8.8.0.sh > xp-run &&
51+
curl -sSL https://github.com/xp-runners/reference/releases/download/v9.2.0/xp-run-9.2.0.sh > xp-run &&
5252
composer install --prefer-dist &&
5353
echo "vendor/autoload.php" > composer.pth
5454

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed the error *Arrow functions on the right hand side of |> must be
7+
parenthesized* - see https://externals.io/message/128473#128554
8+
(@thekid)
9+
610
## 9.6.0 / 2025-05-22
711

812
* Merged PR #185: Implement "clone with", emitting emulating code for

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ArrayLiteral,
88
BinaryExpression,
99
Block,
10+
CallableNewExpression,
1011
Comment,
1112
Expression,
1213
InstanceExpression,
@@ -1165,7 +1166,15 @@ protected function emitNullsafeInstance($result, $instance) {
11651166
protected function emitPipe($result, $pipe) {
11661167
$this->emitOne($result, $pipe->expression);
11671168
$result->out->write('|>');
1168-
$this->emitOne($result, $pipe->target);
1169+
1170+
// `fn() => ...` on the right-hand side of pipe operator must be parenthesized
1171+
if ($pipe->target instanceof CallableNewExpression) {
1172+
$result->out->write('(');
1173+
$this->emitOne($result, $pipe->target);
1174+
$result->out->write(')');
1175+
} else {
1176+
$this->emitOne($result, $pipe->target);
1177+
}
11691178
}
11701179

11711180
protected function emitNullsafePipe($result, $pipe) {

src/test/php/lang/ast/unittest/emit/PipelinesTest.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function __construct(public string $value) { }
9393
public function pipe_to_closure() {
9494
$r= $this->run('class %T {
9595
public function run() {
96-
return "test" |> fn($x) => $x.": OK";
96+
return "test" |> (fn($x) => $x.": OK");
9797
}
9898
}');
9999

@@ -122,7 +122,7 @@ public function run() {
122122
public function missing_argument() {
123123
$this->run('class %T {
124124
public function run() {
125-
return 5 |> fn($a, $b) => $a * $b;
125+
return 5 |> (fn($a, $b) => $a * $b);
126126
}
127127
}');
128128
}
@@ -175,7 +175,7 @@ public function run() {
175175
public function addition_precedence() {
176176
$r= $this->run('class %T {
177177
public function run() {
178-
return 5 + 2 |> fn($i) => $i * 2;
178+
return 5 + 2 |> (fn($i) => $i * 2);
179179
}
180180
}');
181181

@@ -186,7 +186,7 @@ public function run() {
186186
public function comparison_precedence() {
187187
$r= $this->run('class %T {
188188
public function run() {
189-
return 5 |> fn($i) => $i * 2 === 10;
189+
return 5 |> (fn($i) => $i * 2 === 10);
190190
}
191191
}');
192192

@@ -249,8 +249,8 @@ public function run() {
249249
return "Hello World"
250250
|> "htmlentities"
251251
|> str_split(...)
252-
|> fn($x) => array_map(strtoupper(...), $x)
253-
|> fn($x) => array_filter($x, fn($v) => $v != "O")
252+
|> (fn($x) => array_map(strtoupper(...), $x))
253+
|> (fn($x) => array_filter($x, fn($v) => $v != "O"))
254254
;
255255
}
256256
}');
@@ -347,13 +347,13 @@ public function run() {
347347
Assert::equals([2, 3, 4], $r);
348348
}
349349

350-
#[Test, Ignore('See https://externals.io/message/128473')]
350+
#[Test]
351351
public function pipe_precedence_challenges() {
352352
$r= $this->run('class %T {
353353
public function run() {
354354
ob_start();
355355
356-
42 |> fn($x) => $x < 42 |> fn($x) => var_dump($x);
356+
42 |> (fn($x) => $x < 42) |> (fn($x) => var_dump($x));
357357
358358
return ob_get_clean();
359359
}

0 commit comments

Comments
 (0)