fix(aot): skip comment-only else branch

pull/11/head
韩天峰 2 months ago
parent c73dedc306
commit e95f996c06
  1. 12
      src/Php/CompilerBase.php
  2. 2
      tests/aot/control_flow/empty-else.phpt

@ -4203,7 +4203,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
protected function parseIfChain(array $arms, ?Node\Stmt\Else_ $else, int $index): string protected function parseIfChain(array $arms, ?Node\Stmt\Else_ $else, int $index): string
{ {
if (!isset($arms[$index])) { 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 '';
} }
return $this->parseBlockStmts($else->stmts); return $this->parseBlockStmts($else->stmts);
@ -4221,6 +4221,16 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
return $code; return $code;
} }
protected function isEmptyStmtList(array $stmts): bool
{
foreach ($stmts as $stmt) {
if (!$stmt instanceof Node\Stmt\Nop) {
return false;
}
}
return true;
}
/** /**
* 逻辑比较的运算,必须返回 bool 类型. * 逻辑比较的运算,必须返回 bool 类型.
*/ */

@ -1,5 +1,5 @@
--TEST-- --TEST--
empty else empty else with comments
--FILE-- --FILE--
<?php <?php
function main() { function main() {
Loading…
Cancel
Save