fix(php): 修复闭包中的返回类型检查逻辑

- 在返回类型检查条件中添加闭包上下文判断
- 避免在闭包内部执行不必要的返回类型验证
- 确保闭包环境下的代码生成正确性
- 修复三处相同的返回类型检查逻辑以保持一致性
pull/1/head
韩天峰 3 months ago
parent 3b5345d68e
commit 6b65d23ffe
  1. 6
      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;

Loading…
Cancel
Save