From f226382d52e9c22bf106274515017f5608aefb57 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 12:58:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(static):=20=E4=BF=AE=E5=A4=8D=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=8F=98=E9=87=8F=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=B8=B8=E9=87=8F=E5=BC=95=E7=94=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了新的测试用例验证 Bug #23384 的修复 - 实现了 genLambdaCall 方法用于处理静态变量赋值 - 修改 parseBeforeStmtLines 和 parseAfterStmtLines 方法以支持后置语句处理 - 在静态变量初始化时使用 lambda 表达式确保常量正确解析 - 修复了静态变量默认值解析中对类常量的引用问题 --- src/Php/CompilerBase.php | 15 ++++++++++-- src/Php/Generator/ClosureGenerator.php | 20 ++++++++++++++++ tests/core/lang/bug23384.phpt | 33 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/core/lang/bug23384.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 2aa627e1..ed5df308 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1112,9 +1112,18 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseBeforeStmtLines(): string { if ($this->context->beforeStmtLines) { - $code = implode(PHP_EOL, $this->context->beforeStmtLines); + $code = implode(PHP_EOL, $this->context->beforeStmtLines); $this->context->beforeStmtLines = []; + return $code . PHP_EOL; + } + return ''; + } + protected function parseAfterStmtLines(): string + { + if ($this->context->afterStmtLines) { + $code = implode(PHP_EOL, $this->context->afterStmtLines); + $this->context->afterStmtLines = []; return $code . PHP_EOL; } return ''; @@ -3526,7 +3535,9 @@ class CompilerBase extends \PhpAot\Core\Translator $initCode .= $this->getIndent() . "if (!{$initState}) { \n"; $this->indentLevel++; $initCode .= $this->getIndent() . "{$initState} = true;\n"; - $initCode .= $this->getIndent() . $this->getStaticVarName($varName) . ' = ' . $this->parseIdentifier($var->default) . ';'; + $initCode .= $this->genLambdaCall(function () use ($var, $varName) { + return $this->getIndent() . $this->getStaticVarName($varName) . ' = ' . $this->parseExpr($var->default) . ';'; + }); $this->indentLevel--; $initCode .= $this->getIndent() . '}'; $list[] = $initCode; diff --git a/src/Php/Generator/ClosureGenerator.php b/src/Php/Generator/ClosureGenerator.php index c9c69628..518da8ed 100644 --- a/src/Php/Generator/ClosureGenerator.php +++ b/src/Php/Generator/ClosureGenerator.php @@ -93,4 +93,24 @@ trait ClosureGenerator return 'php::newClosure(' . $tmpVar . ', { ' . implode(', ', $useVars) . ' })'; } } + + protected function genLambdaCall(callable $cb): string + { + $code = ''; + $oriCtx = $this->context; + + // 使用 lambda 函数来对 static 变量进行赋值 + $this->context = new FunctionContext(); + $code .= '([&](){' . PHP_EOL; + $body = $cb(); + $code .= $this->genLocalVarDecl(); + $code .= $this->parseBeforeStmtLines(); + $code .= $body; + $code .= $this->parseAfterStmtLines(); + $code .= '})();' . PHP_EOL; + + $this->context = $oriCtx; + + return $code; + } } diff --git a/tests/core/lang/bug23384.phpt b/tests/core/lang/bug23384.phpt new file mode 100644 index 00000000..f1246c86 --- /dev/null +++ b/tests/core/lang/bug23384.phpt @@ -0,0 +1,33 @@ +--TEST-- +Bug #23384 (use of class constants in statics) +--FILE-- + 'ten'); + static $arr = array(Foo::HUN => 'ten'); + + print_r($arr); + print_r($arr2); + print_r($x); + } +} + +function main() { + define('TEN', 10); + Foo::test(); + echo Foo::HUN . "\n"; +} +?> +--EXPECT-- +Array +( + [100] => ten +) +Array +( + [10] => ten +) +100100