From 5d150a2b02e67f64e9da3bf52cdf761d9898a4ee Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 25 May 2026 15:22:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E8=B5=8B=E5=80=BC=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除数组类型重分配检查的特殊处理 - 添加全局翻译器变量用于测试环境 - 引入通用变量赋值表达式检查方法 - 改进错误消息中的变量名显示格式 - 统一变量类型转换错误处理流程 --- phpunit/bootstrap.php | 3 +++ src/Php/CompilerBase.php | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index 7006852b..09b91d61 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -5,13 +5,16 @@ use PhpAot\Php\CompilerTest; use PhpAot\Php\Exception\TestError; require __DIR__ . '/../bin/bootstrap.php'; +require __DIR__ . '/../src/gen_stub.php'; class BaseTest extends TestCase { protected function exec(string $expected, string $file): void { try { + global $translator; $compiler = CompilerTest::create(ROOT_PATH); + $translator = $compiler; $testFile = __DIR__ . '/code/' . $file; $compiler->addFiles([$testFile]); $compiler->prepareFile($testFile); diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index fe513ac9..1c145a1e 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1551,10 +1551,7 @@ class CompilerBase extends \PhpAot\Core\Translator $this->fatalError($left, "Cannot re-assign typed object `\${$var}` from `{$leftClass}` to `{$rightClass}`"); } } else { - if ($this->getVarType($var) === self::TYPE_ARRAY) { - $this->fatalError($left, "Cannot re-assign `\${$var}` from object to array"); - } - // TODO 右值是一个类型对象,但左值是一个 var ,许可,但无法标记对象类型 + $this->checkVarAssignExpr($left, $this->getVarType($var), self::TYPE_OBJECT); } } else { if ($this->isFuncCallExpr($right) and $this->isNameExpr($right->name)) { @@ -5448,7 +5445,11 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isNativeType($toType) and $this->isNativeType($fromType)) { return true; } - $this->fatalError($left, "Cannot re-assign variable from `{$fromType}` to `{$toType}`"); + $varName = 'variable'; + if ($this->isVarExpr($left)) { + $varName = '`$' . $this->parseIdentifier($left) . '`'; + } + $this->fatalError($left, "Cannot re-assign $varName from `{$fromType}` to `{$toType}`"); } protected function mustNoCall(NodeAbstract $node): void