From 288021d16c7d71fb75395b8818babf87d836053b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sun, 8 Feb 2026 18:06:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=AD=A4=E7=AB=8B=E4=BB=A3=E7=A0=81=E7=9A=84=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase 中添加 foundStrayCode 方法用于报告孤立代码错误 - 在 Preprocessor 中为 Stmt_Expression 类型添加孤立代码检测 - 防止在扩展模式下生成名为 main 的函数 - 添加 empty-2.phpt 测试用例验证空值合并操作符功能 --- examples/autoload_class.php | 2 ++ src/Php/CompilerBase.php | 5 +++++ src/Php/Preprocessor.php | 7 ++++++- src/template/extension.cc.php | 3 +++ tests/aot/empty-2.phpt | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/aot/empty-2.phpt diff --git a/examples/autoload_class.php b/examples/autoload_class.php index c5585be2..cddcc72f 100644 --- a/examples/autoload_class.php +++ b/examples/autoload_class.php @@ -11,3 +11,5 @@ function main() $o = new CpuCoreCounter(); var_dump($o->getCount()); } + +test(); \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 7884f069..8ef6cf45 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1799,6 +1799,11 @@ class CompilerBase extends \PhpAot\Core\Translator return false; } + protected function foundStrayCode(): never + { + $this->error("All execution code must be within a function; there is no allowance for stray code."); + } + protected function parseFuncCall(Node\Expr\FuncCall $expr, bool $silent = false): string { $call = ''; diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index a5172481..29428ef6 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -105,9 +105,10 @@ class Preprocessor extends CompilerBase case 'Stmt_Const': $this->parseConstDef($v); break; + case 'Stmt_Expression': + $this->foundStrayCode(); default: $this->fatalError($v, 'Unsupported statement: ' . $type); - break; } } @@ -142,6 +143,8 @@ class Preprocessor extends CompilerBase case 'Stmt_Use': case 'Stmt_Const': break; + case 'Stmt_Expression': + $this->foundStrayCode(); default: abort($v2); } @@ -174,6 +177,8 @@ class Preprocessor extends CompilerBase case 'Stmt_ClassMethod': $code .= $this->prepareFunction($v) . PHP_EOL; break; + case 'Stmt_Expression': + $this->foundStrayCode(); default: abort($v); } diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index 606973a6..90b6b650 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -72,6 +72,9 @@ endforeach; static const zend_function_entry ext_functions[] = { functions as $functionDef): + if ($this->buildMode === 'ext' and $functionDef->name === 'main') { + continue; + } ?> ZEND_FE(name?>, arginfo_name?>) diff --git a/tests/aot/empty-2.phpt b/tests/aot/empty-2.phpt new file mode 100644 index 00000000..a8e5f2df --- /dev/null +++ b/tests/aot/empty-2.phpt @@ -0,0 +1,15 @@ +--TEST-- +empty 2 +--FILE-- + +--EXPECT-- +int(1) +int(3)