diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index a07a0f5e..89077f0a 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4255,7 +4255,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont protected function parseIfChain(array $arms, ?Node\Stmt\Else_ $else, int $index): string { if (!isset($arms[$index])) { - if (!$else) { + if (!$else || $this->isEmptyStmtList($else->stmts)) { return ''; } return $this->parseBlockStmts($else->stmts); @@ -4273,6 +4273,16 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont return $code; } + protected function isEmptyStmtList(array $stmts): bool + { + foreach ($stmts as $stmt) { + if (!$stmt instanceof Node\Stmt\Nop) { + return false; + } + } + return true; + } + /** * 逻辑比较的运算,必须返回 bool 类型. */ diff --git a/tests/aot/control_flow/empty-else.phpt b/tests/aot/control_flow/empty-else.phpt new file mode 100644 index 00000000..202fff54 --- /dev/null +++ b/tests/aot/control_flow/empty-else.phpt @@ -0,0 +1,19 @@ +--TEST-- +empty else with comments +--FILE-- + +--EXPECT-- +success