diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index c71b3145..ee89eef0 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1431,14 +1431,21 @@ class CompilerBase extends \PhpAot\Core\Translator // 实际函数的返回值 $type = $this->detectExprType($v->expr); $expr = $this->parseExpr($v->expr); - // 函数定义时没有声明返回值,但函数体中有返回值,修改为实际的返回值类型 - if ($this->getReturnType() === 'void') { - $this->resetReturnType($v, $type); - } elseif ($this->isNativeType($type) and $this->getReturnType() !== self::TYPE_VAR and $this->getReturnType() !== $type) { - // 返回值类型不一致,说明存在多种类型的返回值,修改为 var 表示 any - $this->resetReturnType($v, self::TYPE_VAR); - } - $returnType = $this->getReturnType(); + + // 匿名函数的返回值一定是 var + if (!$this->inClosure) { + // 函数定义时没有声明返回值,但函数体中有返回值,修改为实际的返回值类型 + if ($this->getReturnType() === 'void') { + $this->resetReturnType($v, $type); + } elseif ($this->isNativeType($type) and $this->getReturnType() !== self::TYPE_VAR and $this->getReturnType() !== $type) { + // 返回值类型不一致,说明存在多种类型的返回值,修改为 var 表示 any + $this->resetReturnType($v, self::TYPE_VAR); + } + $returnType = $this->getReturnType(); + } else { + $returnType = self::TYPE_VAR; + } + $exprCode = $this->convertExprType($expr, $returnType, $type); // return 如果使用了 Indirect 语句,可能会导致变量提前析构,出现悬空指针 // 将 Indirect 赋值给临时变量后,使用 Ctor::Copy 解除了 Indirect,保证内存安全 diff --git a/tests/aot/func-return-type.phpt b/tests/aot/func-return-type.phpt new file mode 100644 index 00000000..55dbc561 --- /dev/null +++ b/tests/aot/func-return-type.phpt @@ -0,0 +1,19 @@ +--TEST-- +static calls +--FILE-- + +--EXPECT-- +string(4) "test" \ No newline at end of file