fix(closure): 修复箭头函数局部变量声明问题

- 在ClosureGenerator中调整局部变量声明生成逻辑
- 移除CompilerBase中重复的局部变量声明代码
- 确保箭头函数正确处理局部变量声明
- 添加测试用例验证箭头函数功能正常
pull/1/head
韩天峰 5 months ago
parent 94615f939a
commit d55f4fed2d
  1. 1
      src/Php/CompilerBase.php
  2. 3
      src/Php/Generator/ClosureGenerator.php
  3. 18
      tests/aot/arrow_fn/004.phpt

@ -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;
}

@ -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;

@ -0,0 +1,18 @@
--TEST--
arrow function
--FILE--
<?php
function foo($obj) {
$fn = fn (string $add) => $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
Loading…
Cancel
Save