From c73dedc306a611aecfb4b2587a287819c0fd5504 Mon Sep 17 00:00:00 2001 From: NathanFreeman <1056159381@qq.com> Date: Sat, 4 Jul 2026 17:09:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A6=82=E6=9E=9Celse=E5=88=86=E6=94=AF?= =?UTF-8?q?=E5=8F=AA=E6=9C=89=E6=B3=A8=E9=87=8A=EF=BC=8C=E9=82=A3=E5=B0=B1?= =?UTF-8?q?=E4=B8=8D=E6=B8=B2=E6=9F=93=E8=BF=99=E4=B8=AA=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/CompilerBase.php | 2 +- tests/core/basic/empty-else.phpt | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/core/basic/empty-else.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index e5835423..c88c484a 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) { + if (!$else || (count($else->stmts) == 1 && $else->stmts[0] instanceof Node\Stmt\Nop)) { return ''; } return $this->parseBlockStmts($else->stmts); diff --git a/tests/core/basic/empty-else.phpt b/tests/core/basic/empty-else.phpt new file mode 100644 index 00000000..bc68bacb --- /dev/null +++ b/tests/core/basic/empty-else.phpt @@ -0,0 +1,19 @@ +--TEST-- +empty else +--FILE-- + +--EXPECT-- +success From e95f996c062a2a77992290f1eed41312e25ec3af Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Jul 2026 11:55:00 +0800 Subject: [PATCH 2/2] 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--