From 548f7ce4962b3864c77215b8d50f5bf10851c811 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 5 Feb 2026 18:05:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E8=B5=8B=E5=80=BC=E8=A1=A8=E8=BE=BE=E5=BC=8F=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E9=87=8F=E6=A3=80=E6=B5=8B=E5=92=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复了数组项解析时的变量作用域问题 - 添加了赋值表达式状态的临时保存和恢复机制 - 改进了变量表达式的类型检查逻辑 - 修复了void类型函数的返回值处理 - 确保非void类型的函数返回正确的空值表示 --- src/Php/CompilerBase.php | 11 +++++++++-- tests/misc/1.phpt | 2 +- tests/misc/2.phpt | 2 +- tests/misc/3.phpt | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/misc/3.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index e0a5023a..5fae5e63 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1036,8 +1036,11 @@ class CompilerBase extends \PhpAot\Core\Translator continue; } if ($item instanceof Node\Expr\ArrayItem) { + $oriInAssignExpr = $this->inAssignExpr; + $this->inAssignExpr = true; $var = $this->parseIdentifier($item->value); - if (!$this->hasVar($var)) { + $this->inAssignExpr = $oriInAssignExpr; + if ($this->isVarExpr($item->value) and !$this->hasVar($var)) { $this->addLocalVar($var, self::TYPE_VAR); } $code .= "{$var} = {$tmpVar}.item({$k}); "; @@ -1199,7 +1202,11 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseReturn(mixed $v): string { if ($v->expr === null) { - return 'return;'; + if ($this->functionDef->returnType === self::TYPE_VOID) { + return 'return;'; + } else { + return 'return php::null;'; + } } // 实际函数的返回值 $type = $this->detectExprType($v->expr); diff --git a/tests/misc/1.phpt b/tests/misc/1.phpt index 26401810..d9552ae4 100644 --- a/tests/misc/1.phpt +++ b/tests/misc/1.phpt @@ -1,5 +1,5 @@ --TEST-- -mixed/1.phpt +mixed: 1 --FILE-- = 0; $i = $i - 2) { + // 奇数位需要调换的偶数 + if ($A[$i] % 2 !== $i % 2) { + // 偶数位不需要调换的偶数 + while ($A[$j] % 2 === $j % 2) { + $j = $j - 2; + } + [$A[$i], $A[$j]] = [$A[$j], $A[$i]]; + } + } + return $A; +} +?> +--EXPECT-- +Array +( + [0] => 4 + [1] => 5 + [2] => 2 + [3] => 7 +) +