diff --git a/engine/compiler.c b/engine/compiler.c index b4f7e69..2530e02 100644 --- a/engine/compiler.c +++ b/engine/compiler.c @@ -3133,8 +3133,8 @@ static sxi32 PH7_GenStateCompileFuncBody( PH7_CompileBlock(&(*pGen)); /* Fix exception jumps now the destination is resolved */ PH7_GenStateFixJumps(pGen->pCurrent, PH7_OP_THROW, PH7_VmInstrLength(pGen->pVm)); - /* Emit the final return if not yet done */ - PH7_VmEmitInstr(pGen->pVm, pGen->pIn->nLine, PH7_OP_DONE, 0, 0, 0, 0); + /* Mark the implicit function epilogue, distinct from other DONE instructions. */ + PH7_VmEmitInstr(pGen->pVm, pGen->pIn->nLine, PH7_OP_DONE, 0, 1, 0, 0); /* Fix gotos jumps now the destination is resolved */ if(SXERR_ABORT == GenStateFixGoto(&(*pGen), nGotoOfft)) { rc = SXERR_ABORT; diff --git a/engine/vm.c b/engine/vm.c index 9b4a592..4997622 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -2723,6 +2723,14 @@ Resume: */ case PH7_OP_DONE: bReturning = pInstr->p3 != 0; + /* Only normal fallthrough of a function body requires a result. + * Keep the call's NULL result unconverted, unlike explicit return. */ + if(!pInstr->iP1 && pInstr->iP2 && (pEntryFrame->iFlags & VM_FRAME_ACTIVE) && (pEntryFrame->iFlags & VM_FRAME_THROW) == 0) { + ph7_vm_func *pFunc = (ph7_vm_func *)pEntryFrame->pUserData; + if(pFunc->nType && (pFunc->nType & MEMOBJ_VOID) == 0) { + PH7_VmThrowError(&(*pVm), PH7_CTX_WARNING, "Missing return value in closure/method returning non-void"); + } + } if(bReturning && !pInstr->iP1 && (iExecFlags & VM_EXEC_INCLUDE) && pResult) { PH7_MemObjRelease(pResult); MemObjSetType(pResult, MEMOBJ_NULL); diff --git a/tests/fibonacci_sequence.aer b/tests/fibonacci_sequence.aer index b258faa..f3331ab 100644 --- a/tests/fibonacci_sequence.aer +++ b/tests/fibonacci_sequence.aer @@ -13,7 +13,7 @@ class Program { return $cur; } - public int main() { + public void main() { var_dump($this->fib(43)); } } diff --git a/tests/singleton_test.aer b/tests/singleton_test.aer index ec7fc02..f33e558 100644 --- a/tests/singleton_test.aer +++ b/tests/singleton_test.aer @@ -22,7 +22,7 @@ final class Test { return $this->value; } - public int set(int $value = 0) { + public void set(int $value = 0) { $this->value = $value; } }