php-src/Zend/tests/attributes/032_attribute_validation_scope.phpt
Ilija Tovilo f8d1864bbb
Delay #[Attribute] arg validation until runtime
Fixes GH-13970
Closes GH-14105

We cannot validate at compile-time for multiple reasons:

* Evaluating the argument naively with zend_get_attribute_value can lead to code
  execution at compile time through the new expression, leading to possible
  reentrance of the compiler.
* Even if the evaluation was possible, it would need to be restricted to the
  current file, because constant values coming from other files can change
  without affecting the current compilation unit. For this reason, validation
  would need to be repeated at runtime anyway.
* Enums cannot be instantiated at compile-time (the actual bug report). This
  could be allowed here, because the value is immediately destroyed. But given
  the other issues, this won't be needed.

Instead, we just move it to runtime entirely. It's only needed for
ReflectionAttribute::newInstance(), which is not particularly a hot path. The
checks are also simple.
2024-05-06 12:38:56 +02:00

19 lines
339 B
PHP

--TEST--
Validation for "Attribute" uses the class scope when evaluating constant ASTs
--FILE--
<?php
#[Attribute(parent::x)]
class x extends y {}
class y {
protected const x = Attribute::TARGET_CLASS;
}
#[x]
class z {}
var_dump((new ReflectionClass(z::class))->getAttributes()[0]->newInstance());
?>
--EXPECT--
object(x)#1 (0) {
}