From e95f996c062a2a77992290f1eed41312e25ec3af Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Jul 2026 11:55:00 +0800 Subject: [PATCH] fix(aot): skip comment-only else branch --- src/Php/CompilerBase.php | 12 +++++++++++- .../{core/basic => aot/control_flow}/empty-else.phpt | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) rename tests/{core/basic => aot/control_flow}/empty-else.phpt (88%) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index c88c484a..8b5b36cf 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4203,7 +4203,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 || (count($else->stmts) == 1 && $else->stmts[0] instanceof Node\Stmt\Nop)) { + if (!$else || $this->isEmptyStmtList($else->stmts)) { return ''; } return $this->parseBlockStmts($else->stmts); @@ -4221,6 +4221,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/core/basic/empty-else.phpt b/tests/aot/control_flow/empty-else.phpt similarity index 88% rename from tests/core/basic/empty-else.phpt rename to tests/aot/control_flow/empty-else.phpt index bc68bacb..202fff54 100644 --- a/tests/core/basic/empty-else.phpt +++ b/tests/aot/control_flow/empty-else.phpt @@ -1,5 +1,5 @@ --TEST-- -empty else +empty else with comments --FILE--