From 51939af82f3d004a817792eb4a71714efba2f183 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 10 Feb 2026 15:23:55 +0800 Subject: [PATCH] =?UTF-8?q?test(closures):=20=E6=B7=BB=E5=8A=A0=E9=97=AD?= =?UTF-8?q?=E5=8C=85=E8=AF=8D=E6=B3=95=E5=8F=98=E9=87=8F=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 closure_003.phpt 测试本地作用域词法变量的闭包功能 - 添加 closure_004.phpt 测试作用域生命周期词法变量的闭包功能 - 修改 convertToRef 方法修复原生类型变量引用转换检查逻辑 - 更新编译器基类中的引用转换实现以正确处理变量类型检查 --- src/Php/CompilerBase.php | 8 +++--- tests/zend/closures/closure_003.phpt | 34 +++++++++++++++++++++++++ tests/zend/closures/closure_004.phpt | 37 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tests/zend/closures/closure_003.phpt create mode 100644 tests/zend/closures/closure_004.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 6349e17f..d08b7f23 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -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()'; } diff --git a/tests/zend/closures/closure_003.phpt b/tests/zend/closures/closure_003.phpt new file mode 100644 index 00000000..b224a50c --- /dev/null +++ b/tests/zend/closures/closure_003.phpt @@ -0,0 +1,34 @@ +--TEST-- +Closure 003: Lambda with lexical variables (local scope) +--FILE-- + +--EXPECT-- +4 +4 +4 +5 +Done diff --git a/tests/zend/closures/closure_004.phpt b/tests/zend/closures/closure_004.phpt new file mode 100644 index 00000000..236e54b1 --- /dev/null +++ b/tests/zend/closures/closure_004.phpt @@ -0,0 +1,37 @@ +--TEST-- +Closure 004: Lambda with lexical variables (scope lifetime) +--FILE-- + +--EXPECT-- +4 +4 +4 +5 +Done