Skip to content
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

Fix GH-9420: Attributes on promoted properties (PHP 8.1) #9887

Open
wants to merge 1 commit into
base: PHP-8.1
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 47 additions & 29 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6457,8 +6457,9 @@ static bool zend_is_valid_default_value(zend_type type, zval *value)
return 0;
}

static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target) /* {{{ */
{
static void zend_compile_attributes(
HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
) /* {{{ */ {
zend_attribute *attr;
zend_internal_attribute *config;

Expand All @@ -6484,8 +6485,20 @@ static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint3
}

zend_string *name = zend_resolve_class_name_ast(el->child[0]);
zend_string *lcname = zend_string_tolower_ex(name, false);
zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;

config = zend_internal_attribute_get(lcname);
zend_string_release(lcname);

/* Exclude internal attributes that do not match on promoted properties. */
if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
zend_string_release(name);
continue;
}
}

uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
attr = zend_add_attribute(
Expand Down Expand Up @@ -6530,31 +6543,33 @@ static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint3
}
}

/* Validate attributes in a secondary loop (needed to detect repeated attributes). */
ZEND_HASH_FOREACH_PTR(*attributes, attr) {
if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
continue;
}
if (*attributes != NULL) {
/* Validate attributes in a secondary loop (needed to detect repeated attributes). */
ZEND_HASH_FOREACH_PTR(*attributes, attr) {
if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
continue;
}

if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
zend_string *location = zend_get_attribute_target_names(target);
zend_string *allowed = zend_get_attribute_target_names(config->flags);
if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
zend_string *location = zend_get_attribute_target_names(target);
zend_string *allowed = zend_get_attribute_target_names(config->flags);

zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
);
}
zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
);
}

if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
if (zend_is_attribute_repeated(*attributes, attr)) {
zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
if (zend_is_attribute_repeated(*attributes, attr)) {
zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
}
}
}

if (config->validator != NULL) {
config->validator(attr, target, CG(active_class_entry));
}
} ZEND_HASH_FOREACH_END();
if (config->validator != NULL) {
config->validator(attr, target, CG(active_class_entry));
}
} ZEND_HASH_FOREACH_END();
}
}
/* }}} */

Expand Down Expand Up @@ -6689,7 +6704,10 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);

if (attributes_ast) {
zend_compile_attributes(&op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER);
zend_compile_attributes(
&op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
property_flags ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
);
}

if (type_ast) {
Expand Down Expand Up @@ -6799,7 +6817,7 @@ static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32
scope, name, &default_value, property_flags | ZEND_ACC_PROMOTED, doc_comment, type);
if (attributes_ast) {
zend_compile_attributes(
&prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
&prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
}
}
}
Expand Down Expand Up @@ -7238,7 +7256,7 @@ static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel)
target = ZEND_ATTRIBUTE_TARGET_METHOD;
}

zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target);
zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
}

/* Do not leak the class scope into free standing functions, even if they are dynamically
Expand Down Expand Up @@ -7404,7 +7422,7 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);

if (attr_ast) {
zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
}
}
}
Expand Down Expand Up @@ -7470,7 +7488,7 @@ static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_as
c = zend_declare_class_constant_ex(ce, name, &value_zv, flags, doc_comment);

if (attr_ast) {
zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
}
}
}
Expand Down Expand Up @@ -7735,7 +7753,7 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
CG(active_class_entry) = ce;

if (decl->child[3]) {
zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS);
zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
}

if (implements_ast) {
Expand Down Expand Up @@ -7941,7 +7959,7 @@ static void zend_compile_enum_case(zend_ast *ast)

zend_ast *attr_ast = ast->child[3];
if (attr_ast) {
zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
}
}

Expand Down
15 changes: 15 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static zend_class_entry *zend_test_child_class;
static zend_class_entry *zend_test_trait;
static zend_class_entry *zend_test_attribute;
static zend_class_entry *zend_test_parameter_attribute;
static zend_class_entry *zend_test_property_attribute;
static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_ns_foo_class;
Expand Down Expand Up @@ -444,6 +445,17 @@ static ZEND_METHOD(ZendTestParameterAttribute, __construct)
ZVAL_STR_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), parameter);
}

static ZEND_METHOD(ZendTestPropertyAttribute, __construct)
{
zend_string *parameter;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(parameter)
ZEND_PARSE_PARAMETERS_END();

ZVAL_STR_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), parameter);
}

static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, no_override)
{
zend_string *parameter;
Expand Down Expand Up @@ -528,6 +540,9 @@ PHP_MINIT_FUNCTION(zend_test)
ZVAL_PSTRING(&attr->args[0].value, "value1");
}

zend_test_property_attribute = register_class_ZendTestPropertyAttribute();
zend_internal_attribute_register(zend_test_property_attribute, ZEND_ATTRIBUTE_TARGET_PROPERTY);

zend_test_class_with_method_with_parameter_attribute = register_class_ZendTestClassWithMethodWithParameterAttribute();

{
Expand Down
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ final class ZendTestParameterAttribute {

public function __construct(string $parameter) {}
}

final class ZendTestPropertyAttribute {
public string $parameter;

public function __construct(string $parameter) {}
}

class ZendTestClassWithMethodWithParameterAttribute {
final public function no_override(string $parameter): int {}
Expand Down
28 changes: 27 additions & 1 deletion ext/zend_test/test_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 27df6a7b48574b5c6c9a54c618fce300c7a8bd13 */
* Stub hash: 8c96703b432ae8d2e9e88edbe278d7d5d80a867e */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -103,6 +103,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZendTestParameterAttribute___construct, 0,
ZEND_ARG_TYPE_INFO(0, parameter, IS_STRING, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_ZendTestPropertyAttribute___construct arginfo_class_ZendTestParameterAttribute___construct

#define arginfo_class_ZendTestClassWithMethodWithParameterAttribute_no_override arginfo_zend_test_parameter_with_attribute

#define arginfo_class_ZendTestClassWithMethodWithParameterAttribute_override arginfo_zend_test_parameter_with_attribute
Expand Down Expand Up @@ -144,6 +146,7 @@ static ZEND_METHOD(_ZendTestClass, returnsThrowable);
static ZEND_METHOD(_ZendTestChildClass, returnsThrowable);
static ZEND_METHOD(_ZendTestTrait, testMethod);
static ZEND_METHOD(ZendTestParameterAttribute, __construct);
static ZEND_METHOD(ZendTestPropertyAttribute, __construct);
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, no_override);
static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, override);
static ZEND_METHOD(ZendTestChildClassWithMethodWithParameterAttribute, override);
Expand Down Expand Up @@ -215,6 +218,12 @@ static const zend_function_entry class_ZendTestParameterAttribute_methods[] = {
};


static const zend_function_entry class_ZendTestPropertyAttribute_methods[] = {
ZEND_ME(ZendTestPropertyAttribute, __construct, arginfo_class_ZendTestPropertyAttribute___construct, ZEND_ACC_PUBLIC)
ZEND_FE_END
};


static const zend_function_entry class_ZendTestClassWithMethodWithParameterAttribute_methods[] = {
ZEND_ME(ZendTestClassWithMethodWithParameterAttribute, no_override, arginfo_class_ZendTestClassWithMethodWithParameterAttribute_no_override, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(ZendTestClassWithMethodWithParameterAttribute, override, arginfo_class_ZendTestClassWithMethodWithParameterAttribute_override, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -376,6 +385,23 @@ static zend_class_entry *register_class_ZendTestParameterAttribute(void)
return class_entry;
}

static zend_class_entry *register_class_ZendTestPropertyAttribute(void)
{
zend_class_entry ce, *class_entry;

INIT_CLASS_ENTRY(ce, "ZendTestPropertyAttribute", class_ZendTestPropertyAttribute_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_FINAL;

zval property_parameter_default_value;
ZVAL_UNDEF(&property_parameter_default_value);
zend_string *property_parameter_name = zend_string_init("parameter", sizeof("parameter") - 1, 1);
zend_declare_typed_property(class_entry, property_parameter_name, &property_parameter_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
zend_string_release(property_parameter_name);

return class_entry;
}

static zend_class_entry *register_class_ZendTestClassWithMethodWithParameterAttribute(void)
{
zend_class_entry ce, *class_entry;
Expand Down
31 changes: 31 additions & 0 deletions ext/zend_test/tests/attribute-promotion-parameter-only.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Attribute on promoted property may only target parameter
--EXTENSIONS--
zend_test
--FILE--
<?php

class AttrTest
{
public function __construct(
#[ZendTestParameterAttribute('foo')] public $param
) {}
}

$ref = new ReflectionClass(AttrTest::class);
$attr = $ref->getConstructor()->getParameters()[0]->getAttributes();

var_dump(count($attr));
var_dump($attr[0]->getName());
var_dump($attr[0]->newInstance()->parameter);

$attr = $ref->getProperty('param')->getAttributes();

var_dump(count($attr));

?>
--EXPECTF--
int(1)
string(26) "ZendTestParameterAttribute"
string(3) "foo"
int(0)
31 changes: 31 additions & 0 deletions ext/zend_test/tests/attribute-promotion-property-only.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Attribute on promoted property may only target property
--EXTENSIONS--
zend_test
--FILE--
<?php

class AttrTest
{
public function __construct(
#[ZendTestPropertyAttribute('foo')] public $param
) {}
}

$ref = new ReflectionClass(AttrTest::class);
$attr = $ref->getConstructor()->getParameters()[0]->getAttributes();

var_dump(count($attr));

$attr = $ref->getProperty('param')->getAttributes();

var_dump(count($attr));
var_dump($attr[0]->getName());
var_dump($attr[0]->newInstance()->parameter);

?>
--EXPECTF--
int(0)
int(1)
string(25) "ZendTestPropertyAttribute"
string(3) "foo"