From d55f4fed2d6947ef07efddd69b819cbe9241eb4e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 25 Mar 2026 11:31:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(closure):=20=E4=BF=AE=E5=A4=8D=E7=AE=AD?= =?UTF-8?q?=E5=A4=B4=E5=87=BD=E6=95=B0=E5=B1=80=E9=83=A8=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在ClosureGenerator中调整局部变量声明生成逻辑 - 移除CompilerBase中重复的局部变量声明代码 - 确保箭头函数正确处理局部变量声明 - 添加测试用例验证箭头函数功能正常 --- src/Php/CompilerBase.php | 1 - src/Php/Generator/ClosureGenerator.php | 3 ++- tests/aot/arrow_fn/004.phpt | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/aot/arrow_fn/004.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6f2cb288..3400e537 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4345,7 +4345,6 @@ class CompilerBase extends \PhpAot\Core\Translator { $cb = function () use ($expr) { $fnCode = $this->parseStmts($expr->stmts); - $fnCode = $this->genLocalVarDecl() . $fnCode; if (!$this->isReturnStmtInLastLine($expr->stmts)) { $fnCode .= 'return ' . self::VALUE_NULL . ';' . PHP_EOL; } diff --git a/src/Php/Generator/ClosureGenerator.php b/src/Php/Generator/ClosureGenerator.php index 2aa52d54..dafb7339 100644 --- a/src/Php/Generator/ClosureGenerator.php +++ b/src/Php/Generator/ClosureGenerator.php @@ -64,7 +64,8 @@ trait ClosureGenerator $this->addArgument('this_', self::TYPE_OBJECT); } - $code .= $bodyGenCb(); + $body = $bodyGenCb(); + $code .= $this->genLocalVarDecl() . $body; $this->indentLevel--; $this->context->inClosure = false; diff --git a/tests/aot/arrow_fn/004.phpt b/tests/aot/arrow_fn/004.phpt new file mode 100644 index 00000000..2f4a17fe --- /dev/null +++ b/tests/aot/arrow_fn/004.phpt @@ -0,0 +1,18 @@ +--TEST-- +arrow function +--FILE-- + $obj->add(new DateInterval($add)); + $newDate = $fn('P10D'); + echo $newDate->format('Y-m-d') . "\n"; +} + +function main() +{ + $date = new DateTimeImmutable('2000-01-01'); + foo($date); +} +?> +--EXPECT-- +2000-01-11 \ No newline at end of file