From 5ee74b2d9d6611d4e991bde39543b9bba7e94d98 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Jul 2026 16:40:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(parser):=20=E4=BF=AE=E5=A4=8D=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E8=B5=8B=E5=80=BC=E6=97=B6=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8E=A8=E6=96=AD=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/Parser/AssignOpTrait.php | 16 ++++++--- tests/aot/ref/basic.phpt | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tests/aot/ref/basic.phpt diff --git a/src/Php/Parser/AssignOpTrait.php b/src/Php/Parser/AssignOpTrait.php index 39603a2d..4f7f2317 100644 --- a/src/Php/Parser/AssignOpTrait.php +++ b/src/Php/Parser/AssignOpTrait.php @@ -145,9 +145,10 @@ trait AssignOpTrait } $propertyWriteTarget = $this->preparePropertyWriteTarget($left); - $finalVarType = $type = $this->detectTypeOfExpr($right); + $type = $this->detectTypeOfExpr($right); + $finalVarType = $this->getNormalAssignType($type); if ($type === self::TYPE_VOID) { - $finalVarType = $type = self::TYPE_VAR; + $type = self::TYPE_VAR; } if ($this->isVarExpr($left)) { @@ -239,6 +240,7 @@ trait AssignOpTrait } elseif ($this->isVarExpr($right)) { $rightVar = $this->parseIdentifier($right); $type = $this->isStdContainer($rightVar) ? self::TYPE_ARRAY : $this->getVarType($rightVar); + $finalVarType = $this->getNormalAssignType($type); if ($this->isTypedObject($rightVar) and $this->isTypedObject($var)) { $leftClass = $this->getObjectType($var); $rightClass = $this->getObjectType($rightVar); @@ -247,7 +249,8 @@ trait AssignOpTrait } // 变量第一次被赋值,确定其类型,由于 PHP 的变量作用域是 function 级的,在 for/while 块中声明的变量,可以在块外使用 if (!$this->hasVar($var)) { - $finalVarType = $this->isNativeType($type) ? $this->getNativeType($type) : $type; + $finalVarType = $this->getNormalAssignType($type); + $finalVarType = $this->isNativeType($finalVarType) ? $this->getNativeType($finalVarType) : $finalVarType; $this->addLocalVar($var, $finalVarType); } else { $finalVarType = $this->getVarType($var); @@ -597,9 +600,14 @@ trait AssignOpTrait $this->errorUndefinedVariable($expr->expr); } if ($this->isVarExpr($expr->var) and !$this->hasVar($var)) { - $this->addLocalVar($var, $this->detectTypeOfExpr($expr->expr)); + $this->addLocalVar($var, $this->getNormalAssignType($this->detectTypeOfExpr($expr->expr))); } return '(' . $isset . '?' . $var . ':(' . $var . ' = ' . $right . '))'; } + protected function getNormalAssignType(string $type): string + { + return $type === self::TYPE_REF || $type === self::TYPE_VOID ? self::TYPE_VAR : $type; + } + } diff --git a/tests/aot/ref/basic.phpt b/tests/aot/ref/basic.phpt new file mode 100644 index 00000000..32457804 --- /dev/null +++ b/tests/aot/ref/basic.phpt @@ -0,0 +1,59 @@ +--TEST-- +ref: basic +--FILE-- + +--EXPECT-- +array(3) { + [0]=> + int(1) + [1]=> + int(2026) + [2]=> + int(3) +} +array(3) { + [0]=> + int(1) + [1]=> + int(2026) + [2]=> + int(1999) +} +array(3) { + [0]=> + int(1) + [1]=> + int(2026) + [2]=> + int(3) +} +string(4) "done"