From 6b65d23ffeb75f7eedf4d3bb807353a2a746dbd0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 4 Jun 2026 11:20:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E9=97=AD?= =?UTF-8?q?=E5=8C=85=E4=B8=AD=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在返回类型检查条件中添加闭包上下文判断 - 避免在闭包内部执行不必要的返回类型验证 - 确保闭包环境下的代码生成正确性 - 修复三处相同的返回类型检查逻辑以保持一致性 --- src/Php/CompilerBase.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 16584c1b..1dc90cac 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2308,7 +2308,7 @@ class CompilerBase extends \PhpAot\Core\Translator if ($v->expr === null) { if ($this->functionDef->returnType === self::TYPE_VOID and !$this->context->inClosure) { return 'return;'; - } elseif ($this->functionDef->returnTypeCheck) { + } elseif ($this->functionDef->returnTypeCheck && !$this->context->inClosure) { $tmpVar = $this->genTmpVarName(); $this->addLocalVar($tmpVar, self::TYPE_VAR); $code = $tmpVar . ' = ' . self::VALUE_NULL . ';' . PHP_EOL; @@ -2350,7 +2350,7 @@ class CompilerBase extends \PhpAot\Core\Translator $exprCode = $this->convertExprType($expr, $returnType, $type); // Union/nullable return type: always use tmpVar for runtime check - if ($this->functionDef->returnTypeCheck) { + if ($this->functionDef->returnTypeCheck && !$this->context->inClosure) { $tmpVar = $this->genTmpVarName(); $this->addLocalVar($tmpVar, self::TYPE_VAR); $code = $tmpVar . ' = ' . $exprCode . ';' . PHP_EOL; @@ -6505,7 +6505,7 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->functionDef->returnType === self::TYPE_VOID) { return ''; } - if ($this->functionDef->returnTypeCheck) { + if ($this->functionDef->returnTypeCheck && !$this->context->inClosure) { $tmpVar = $this->genTmpVarName(); $this->addLocalVar($tmpVar, self::TYPE_VAR); $code = $tmpVar . ' = ' . self::VALUE_NULL . ';' . PHP_EOL;