Skip to content

Commit 02af2ea

Browse files
committed
Fix callable new syntax when using a variable or expression
See also php/php-src#12336
1 parent 4b3514e commit 02af2ea

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

ChangeLog.md

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ XP Compiler ChangeLog
33

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

6+
## 9.3.3 / 2025-03-02
7+
8+
* Fixed callable new syntax when using a variable or expression, e.g.
9+
`new $class(...)`. See also https://github.com/php/php-src/issues/12336
10+
(@thekid)
11+
612
## 9.3.2 / 2024-11-02
713

814
* Fixed empty match expressions and match expressions with default

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1096,13 +1096,11 @@ protected function emitCallable($result, $callable) {
10961096

10971097
protected function emitCallableNew($result, $callable) {
10981098
$t= $result->temp();
1099-
$result->out->write("function(...{$t}) { return ");
1099+
$result->out->write("fn(...{$t}) => ");
11001100

11011101
$callable->type->arguments= [new UnpackExpression(new Variable(substr($t, 1)), $callable->line)];
11021102
$this->emitOne($result, $callable->type);
11031103
$callable->type->arguments= null;
1104-
1105-
$result->out->write("; }");
11061104
}
11071105

11081106
protected function emitInvoke($result, $invoke) {

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

+22
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ public function run() {
171171
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
172172
}
173173

174+
#[Test]
175+
public function variable_instantiation() {
176+
$r= $this->run('use lang\ast\unittest\emit\Handle; class %T {
177+
public function run() {
178+
$class= Handle::class;
179+
return array_map(new $class(...), [0, 1, 2]);
180+
}
181+
}');
182+
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
183+
}
184+
185+
#[Test]
186+
public function expression_instantiation() {
187+
$r= $this->run('use lang\ast\unittest\emit\Handle; class %T {
188+
public function run() {
189+
$version= "";
190+
return array_map(new (Handle::class.$version)(...), [0, 1, 2]);
191+
}
192+
}');
193+
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
194+
}
195+
174196
#[Test]
175197
public function anonymous_instantiation() {
176198
$f= $this->run('class %T {

0 commit comments

Comments
 (0)