Skip to content

Commit

Permalink
Fix callable new syntax when using a variable or expression
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 2, 2025
1 parent 4b3514e commit 02af2ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ XP Compiler ChangeLog

## ?.?.? / ????-??-??

## 9.3.3 / 2025-03-02

* Fixed callable new syntax when using a variable or expression, e.g.
`new $class(...)`. See also https://github.com/php/php-src/issues/12336
(@thekid)

## 9.3.2 / 2024-11-02

* Fixed empty match expressions and match expressions with default
Expand Down
4 changes: 1 addition & 3 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,13 +1096,11 @@ protected function emitCallable($result, $callable) {

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

$callable->type->arguments= [new UnpackExpression(new Variable(substr($t, 1)), $callable->line)];
$this->emitOne($result, $callable->type);
$callable->type->arguments= null;

$result->out->write("; }");
}

protected function emitInvoke($result, $invoke) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/php/lang/ast/unittest/emit/CallableSyntaxTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ public function run() {
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
}

#[Test]
public function variable_instantiation() {
$r= $this->run('use lang\ast\unittest\emit\Handle; class %T {
public function run() {
$class= Handle::class;
return array_map(new $class(...), [0, 1, 2]);
}
}');
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
}

#[Test]
public function expression_instantiation() {
$r= $this->run('use lang\ast\unittest\emit\Handle; class %T {
public function run() {
$version= "";
return array_map(new (Handle::class.$version)(...), [0, 1, 2]);
}
}');
Assert::equals([new Handle(0), new Handle(1), new Handle(2)], $r);
}

#[Test]
public function anonymous_instantiation() {
$f= $this->run('class %T {
Expand Down

0 comments on commit 02af2ea

Please sign in to comment.