Skip to content

gen_stub: Add support for attributes on constants in stubs #18735

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

Merged
merged 4 commits into from
Jun 5, 2025
Merged
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
2 changes: 1 addition & 1 deletion Zend/tests/e_strict-deprecated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var_dump(E_STRICT);
--EXPECTF--
int(30719)

Deprecated: Constant E_STRICT is deprecated in %s on line %d
Deprecated: Constant E_STRICT is deprecated since 8.4, the error level was removed in %s on line %d
int(2048)
7 changes: 7 additions & 0 deletions Zend/zend_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define ZEND_ATTRIBUTES_H

#include "zend_compile.h"
#include "zend_constants.h"

#define ZEND_ATTRIBUTE_TARGET_CLASS (1<<0)
#define ZEND_ATTRIBUTE_TARGET_FUNCTION (1<<1)
Expand Down Expand Up @@ -126,6 +127,12 @@ static zend_always_inline zend_attribute *zend_add_class_constant_attribute(zend
return zend_add_attribute(&c->attributes, name, argc, flags, 0, 0);
}

static zend_always_inline zend_attribute *zend_add_global_constant_attribute(zend_constant *c, zend_string *name, uint32_t argc)
{
uint32_t flags = ZEND_CONSTANT_MODULE_NUMBER(c) == PHP_USER_CONSTANT ? 0 : ZEND_ATTRIBUTE_PERSISTENT;
return zend_add_attribute(&c->attributes, name, argc, flags, 0, 0);
}

void zend_register_attribute_ce(void);
void zend_attributes_shutdown(void);

Expand Down
3 changes: 2 additions & 1 deletion Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ static void copy_zend_constant(zval *zv)
c->filename = zend_string_copy(c->filename);
}
if (c->attributes != NULL) {
c->attributes = zend_array_dup(c->attributes);
// Use the same attributes table
GC_ADDREF(c->attributes);
}
if (Z_TYPE(c->value) == IS_STRING) {
Z_STR(c->value) = zend_string_dup(Z_STR(c->value), 1);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_constants.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
/**
* @var int
* @cvalue E_STRICT
* @deprecated
* @todo Remove in PHP 9.0
*/
#[\Deprecated(since: '8.4', message: 'the error level was removed')]
const E_STRICT = UNKNOWN;

/**
Expand Down
16 changes: 15 additions & 1 deletion Zend/zend_constants_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 58 additions & 10 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2622,6 +2622,13 @@ public function __construct(
?ExposedDocComment $exposedDocComment,
bool $isFileCacheAllowed
) {
foreach ($attributes as $attr) {
if ($attr->class === "Deprecated") {
$isDeprecated = true;
break;
}
}

$this->name = $name;
$this->value = $value;
$this->valueString = $valueString;
Expand Down Expand Up @@ -2915,17 +2922,11 @@ protected function getFlagsByPhpVersion(): array
{
$flags = parent::getFlagsByPhpVersion();

// $this->isDeprecated also accounts for any #[\Deprecated] attributes
if ($this->isDeprecated) {
$flags = $this->addFlagForVersionsAbove($flags, "ZEND_ACC_DEPRECATED", PHP_80_VERSION_ID);
}

foreach ($this->attributes as $attr) {
if ($attr->class === "Deprecated") {
$flags = $this->addFlagForVersionsAbove($flags, "ZEND_ACC_DEPRECATED", PHP_80_VERSION_ID);
break;
}
}

if ($this->flags & Modifiers::FINAL) {
$flags = $this->addFlagForVersionsAbove($flags, "ZEND_ACC_FINAL", PHP_81_VERSION_ID);
}
Expand Down Expand Up @@ -4340,7 +4341,7 @@ private function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPri
$cond,
$this->isUndocumentable,
$this->getMinimumPhpVersionIdCompatibility(),
[]
AttributeInfo::createFromGroups($stmt->attrGroups)
);
}
continue;
Expand Down Expand Up @@ -5177,7 +5178,9 @@ static function (FuncInfo $funcInfo) use ($fileInfo, &$generatedFunctionDeclarat
$php80MinimumCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility() === null || $fileInfo->getMinimumPhpVersionIdCompatibility() >= PHP_80_VERSION_ID;

if ($fileInfo->generateClassEntries) {
if ($attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null)) {
$attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null);
$attributeInitializationCode .= generateGlobalConstantAttributeInitialization($fileInfo->constInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null);
if ($attributeInitializationCode) {
if (!$php80MinimumCompatibility) {
$attributeInitializationCode = "\n#if (PHP_VERSION_ID >= " . PHP_80_VERSION_ID . ")" . $attributeInitializationCode . "#endif\n";
}
Expand Down Expand Up @@ -5289,6 +5292,51 @@ static function (FuncInfo $funcInfo) use ($allConstInfos, $phpVersionIdMinimumCo
);
}

/**
* @param iterable<ConstInfo> $constInfos
* @param array<string, ConstInfo> $allConstInfos
*/
function generateGlobalConstantAttributeInitialization(
iterable $constInfos,
array $allConstInfos,
?int $phpVersionIdMinimumCompatibility,
?string $parentCond = null
): string {
$isConditional = false;
if ($phpVersionIdMinimumCompatibility !== null && $phpVersionIdMinimumCompatibility < PHP_85_VERSION_ID) {
$isConditional = true;
$phpVersionIdMinimumCompatibility = PHP_85_VERSION_ID;
}
$code = generateCodeWithConditions(
$constInfos,
"",
static function (ConstInfo $constInfo) use ($allConstInfos, $phpVersionIdMinimumCompatibility) {
if ($constInfo->attributes === []) {
return null;
}
$constName = str_replace('\\', '\\\\', $constInfo->name->__toString());
$constVarName = 'const_' . $constName;

$code .= "\tzend_constant *$constVarName = zend_hash_str_find_ptr(EG(zend_constants), \"" . $constName . "\", sizeof(\"" . $constName . "\") - 1);\n";
foreach ($constInfo->attributes as $key => $attribute) {
$code .= $attribute->generateCode(
"zend_add_global_constant_attribute($constVarName",
$constVarName . "_$key",
$allConstInfos,
$phpVersionIdMinimumCompatibility
);
}

return $code;
},
$parentCond
);
if ($code && $isConditional) {
return "\n#if (PHP_VERSION_ID >= " . PHP_85_VERSION_ID . ")\n" . $code . "#endif\n";
}
return $code;
}

/**
* @param iterable<ConstInfo> $constInfos
* @param array<string, ConstInfo> $allConstInfos
Expand Down Expand Up @@ -6030,7 +6078,7 @@ function initPhpParser() {
}

$isInitialized = true;
$version = "5.3.1";
$version = "5.5.0";
$phpParserDir = __DIR__ . "/PHP-Parser-$version";
if (!is_dir($phpParserDir)) {
installPhpParser($version, $phpParserDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ReflectionConstant::getAttributes() with attribute (internal constant)
--FILE--
<?php

$reflectionConstant = new ReflectionConstant('E_STRICT');
var_dump($reflectionConstant->getAttributes());

?>
--EXPECTF--
array(1) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "Deprecated"
}
}
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
/** @var string */
const ZEND_CONSTANT_A = "global";

/**
* @var int
*/
#[\Deprecated(message: "use something else", since: "version 1.5")]
const ZEND_TEST_ATTRIBUTED_CONSTANT = 42;

interface _ZendTestInterface
{
/** @var int */
Expand Down
19 changes: 18 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions ext/zend_test/tests/attribute-deprecated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ $reflection = new ReflectionClassConstant('_ZendTestClass', 'ZEND_TEST_DEPRECATE
var_dump($reflection->getAttributes()[0]->newInstance());
var_dump($reflection->isDeprecated());

ZEND_TEST_ATTRIBUTED_CONSTANT;

$reflection = new ReflectionConstant('ZEND_TEST_ATTRIBUTED_CONSTANT');
var_dump($reflection->getAttributes()[0]->newInstance());
var_dump($reflection->isDeprecated());

?>
--EXPECTF--
Deprecated: Function zend_test_deprecated() is deprecated in %s on line %d
Expand All @@ -38,3 +44,12 @@ object(Deprecated)#%d (2) {
NULL
}
bool(true)

Deprecated: Constant ZEND_TEST_ATTRIBUTED_CONSTANT is deprecated since version 1.5, use something else in %s on line %d
object(Deprecated)#%d (2) {
["message"]=>
string(18) "use something else"
["since"]=>
string(11) "version 1.5"
}
bool(true)
4 changes: 3 additions & 1 deletion ext/zend_test/tests/gh11423.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ var_dump(get_defined_constants(true)["user"]);

?>
--EXPECT--
array(4) {
array(5) {
["ZEND_TEST_DEPRECATED"]=>
int(42)
["ZEND_CONSTANT_A"]=>
string(6) "global"
["ZEND_TEST_ATTRIBUTED_CONSTANT"]=>
int(42)
["ZendTestNS2\ZEND_CONSTANT_A"]=>
string(10) "namespaced"
["ZendTestNS2\ZendSubNS\ZEND_CONSTANT_A"]=>
Expand Down