test(closures): 添加闭包词法变量测试用例并修复引用转换逻辑

- 添加 closure_003.phpt 测试本地作用域词法变量的闭包功能
- 添加 closure_004.phpt 测试作用域生命周期词法变量的闭包功能
- 修改 convertToRef 方法修复原生类型变量引用转换检查逻辑
- 更新编译器基类中的引用转换实现以正确处理变量类型检查
pull/1/head
韩天峰 7 months ago
parent b3dc15a55f
commit 51939af82f
  1. 8
      src/Php/CompilerBase.php
  2. 34
      tests/zend/closures/closure_003.phpt
  3. 37
      tests/zend/closures/closure_004.phpt

@ -2963,10 +2963,12 @@ class CompilerBase extends \PhpAot\Core\Translator
return $tmpVar;
}
protected function convertToRef(NodeAbstract $expr): string {
protected function convertToRef(NodeAbstract $expr): string
{
$this->checkLeftValue($expr);
if ($this->isVarExpr($expr) and $this->isNativeType($this->parseIdentifier($expr))) {
$this->fatalError($expr, 'Cannot convert variable of native type to reference');
$var = $this->parseIdentifier($expr);
if ($this->isVarExpr($expr) and $this->isNativeType($var)) {
$this->localVars[$var] = self::TYPE_VAR;
}
return $this->parseIdentifier($expr) . '.toReference()';
}

@ -0,0 +1,34 @@
--TEST--
Closure 003: Lambda with lexical variables (local scope)
--FILE--
<?php
function run () {
$x = 4;
$lambda1 = function () use ($x) {
echo "$x\n";
};
$lambda2 = function () use (&$x) {
echo "$x\n";
};
$lambda1();
$lambda2();
$x++;
$lambda1();
$lambda2();
}
function main() {
run();
echo "Done\n";
}
?>
--EXPECT--
4
4
4
5
Done

@ -0,0 +1,37 @@
--TEST--
Closure 004: Lambda with lexical variables (scope lifetime)
--FILE--
<?php
function run () {
$x = 4;
$lambda1 = function () use ($x) {
echo "$x\n";
};
$lambda2 = function () use (&$x) {
echo "$x\n";
$x++;
};
return array($lambda1, $lambda2);
}
function main() {
list ($lambda1, $lambda2) = run();
$lambda1();
$lambda2();
$lambda1();
$lambda2();
echo "Done\n";
}
?>
--EXPECT--
4
4
4
5
Done
Loading…
Cancel
Save