Skip to content

GH-19153: Validate #[\Attribute] targets #19154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.5.0beta1

- Core:
. Fixed bug GH-19153 (#[\Attribute] validation should error on
trait/interface/enum/abstract class). (DanielEScherzer)

31 Jul 2025, PHP 8.5.0alpha4

Expand Down
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_abstract.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an abstract class
--FILE--
<?php

#[Attribute]
abstract class Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[\Attribute] to abstract class Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an enum
--FILE--
<?php

#[Attribute]
enum Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[\Attribute] to enum Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_interface.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on an interface
--FILE--
<?php

#[Attribute]
interface Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[\Attribute] to interface Demo in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/attributes/Attribute/Attribute_on_trait.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
#[Attribute] on a trait
--FILE--
<?php

#[Attribute]
trait Demo {}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Cannot apply #[\Attribute] to trait Demo in %s on line %d
19 changes: 19 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ static void validate_allow_dynamic_properties(
scope->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES;
}

static void validate_attribute(
zend_attribute *attr, uint32_t target, zend_class_entry *scope)
{
const char *msg = NULL;
if (scope->ce_flags & ZEND_ACC_TRAIT) {
msg = "Cannot apply #[\\Attribute] to trait %s";
} else if (scope->ce_flags & ZEND_ACC_INTERFACE) {
msg = "Cannot apply #[\\Attribute] to interface %s";
} else if (scope->ce_flags & ZEND_ACC_ENUM) {
msg = "Cannot apply #[\\Attribute] to enum %s";
} else if (scope->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
msg = "Cannot apply #[\\Attribute] to abstract class %s";
}
if (msg != NULL) {
zend_error_noreturn(E_ERROR, msg, ZSTR_VAL(scope->name));
}
}

ZEND_METHOD(Attribute, __construct)
{
zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL;
Expand Down Expand Up @@ -522,6 +540,7 @@ void zend_register_attribute_ce(void)

zend_ce_attribute = register_class_Attribute();
attr = zend_mark_internal_attribute(zend_ce_attribute);
attr->validator = validate_attribute;

zend_ce_return_type_will_change_attribute = register_class_ReturnTypeWillChange();
zend_mark_internal_attribute(zend_ce_return_type_will_change_attribute);
Expand Down