From aa51b09fa98c9a3b3ff0018dbffa2c3a7d48003e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 11 May 2026 18:03:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(aot):=20=E4=BF=AE=E5=A4=8D=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=95=B0=E7=BB=84=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=B9=B6=E8=B0=83=E6=95=B4=E7=BC=96=E8=AF=91=E5=99=A8=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=8F=AF=E8=A7=81=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 CompilerBase.php 中的 parseAssignOpBitwiseOr 方法从 private 改为 protected - 将 CompilerBase.php 中的 error 方法从 protected 改为 public - 在 gen_stub.php 中添加对属性数组参数的错误检查和提示 - 修复 Translator.php 中构建目录文件名使用 targetName 而非 getModuleName() --- src/Php/CompilerBase.php | 4 +- src/Php/Translator.php | 2 +- src/gen_stub.php | 3 ++ tests/aot/attribute/001.phpt | 72 ++++++++++++++++++++++++++++++++++++ tests/aot/attribute/002.phpt | 29 +++++++++++++++ tests/aot/attribute/003.phpt | 34 +++++++++++++++++ 6 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 tests/aot/attribute/001.phpt create mode 100644 tests/aot/attribute/002.phpt create mode 100644 tests/aot/attribute/003.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index e2dc837a..ad9855cc 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2672,7 +2672,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseAssignOp($expr, '&='); } - private function parseAssignOpBitwiseOr(Expr\AssignOp\BitwiseOr $expr): string + protected function parseAssignOpBitwiseOr(Expr\AssignOp\BitwiseOr $expr): string { return $this->parseAssignOp($expr, '|='); } @@ -2682,7 +2682,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseAssignOp($expr, '**='); } - protected function error(string $msg): never + public function error(string $msg): never { if ($this->forTest) { throw new TestError($msg); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 720ecbe7..17a3e95e 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -426,7 +426,7 @@ class Translator extends Preprocessor exit(1); } } - $file = $this->getBuildDir() . '/extension-' . $this->getModuleName() . '.cc'; + $file = $this->getBuildDir() . '/extension-' . $this->targetName . '.cc'; $this->localHeaders = $this->argInfoHeaderFiles; $this->genClassCeList(); $this->indentLevel++; diff --git a/src/gen_stub.php b/src/gen_stub.php index bf18b7aa..66cb054b 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -3397,6 +3397,9 @@ class AttributeInfo { } } if ($initValue === '') { + if ($arg->value instanceof Expr\Array_ && count($arg->value->items) > 0) { + getTranslator()->error("Array arguments to attributes are not supported"); + } $value = EvaluatedValue::createFromExpression($arg->value, null, null, $allConstInfos); $code .= $value->initializeZval( "attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value", diff --git a/tests/aot/attribute/001.phpt b/tests/aot/attribute/001.phpt new file mode 100644 index 00000000..c3d32c52 --- /dev/null +++ b/tests/aot/attribute/001.phpt @@ -0,0 +1,72 @@ +--TEST-- +Attribute: 001 +--FILE-- +value = $value; + } +} + +#[MyAttribute] +#[MyAttribute(1234)] +#[MyAttribute(value: 1234)] +#[MyAttribute(MyAttribute::VALUE)] +#[MyAttribute([])] +#[MyAttribute(100 + 200)] +class Thing +{ +} + +#[MyAttribute(1234), MyAttribute(5678)] +class AnotherThing +{ +} + +function main() { + $reflection = (new ReflectionClass(Thing::class)); + $attributes = $reflection->getAttributes(); + + foreach ($attributes as $attribute) { + var_dump($attribute->getName()); + var_dump($attribute->getArguments()); + } +} +?> +--EXPECT-- +string(11) "MyAttribute" +array(0) { +} +string(11) "MyAttribute" +array(1) { + [0]=> + int(1234) +} +string(11) "MyAttribute" +array(1) { + ["value"]=> + int(1234) +} +string(11) "MyAttribute" +array(1) { + [0]=> + string(5) "value" +} +string(11) "MyAttribute" +array(1) { + [0]=> + array(0) { + } +} +string(11) "MyAttribute" +array(1) { + [0]=> + int(300) +} \ No newline at end of file diff --git a/tests/aot/attribute/002.phpt b/tests/aot/attribute/002.phpt new file mode 100644 index 00000000..f88008bc --- /dev/null +++ b/tests/aot/attribute/002.phpt @@ -0,0 +1,29 @@ +--TEST-- +Attribute: 002 +--FILE-- +getAttributes(MyAttribute::class); + var_dump($attributes); +} +?> +--EXPECT-- +object(Thing)#1 (0) { +} +array(1) { + [0]=> + object(ReflectionAttribute)#3 (1) { + ["name"]=> + string(11) "MyAttribute" + } +} \ No newline at end of file diff --git a/tests/aot/attribute/003.phpt b/tests/aot/attribute/003.phpt new file mode 100644 index 00000000..49ab0988 --- /dev/null +++ b/tests/aot/attribute/003.phpt @@ -0,0 +1,34 @@ +--TEST-- +Attribute: 003 +--SKIPIF-- + +--FILE-- +getAttributes(MyAttribute::class); + foreach ($attributes as $attribute) { + var_dump($attribute->getName()); + var_dump($attribute->getArguments()); + } +} +?> +--EXPECT-- +object(Thing)#1 (0) { +} +array(1) { + [0]=> + object(ReflectionAttribute)#3 (1) { + ["name"]=> + string(11) "MyAttribute" + } +} \ No newline at end of file