Improve BC support of arginfo files fenerated by gen_stub.php (#13705)

- Declared compatibility expectations of stub files are now enforced by a ZEND_STATIC_ASSERT call at the top of arginfo files
- Property registration for PHP 7 is fixed: function zend_declare_property_ex() is used again instead of zend_declare_typed_property(). This has been a regression since I added support for exposing doc comments.
- As a defensive measure, deep cloning is performed before newer features (type declarations, attributes etc.) are discarded before generating legacy arginfo files. Until now, some of the objects were forgotten to be taken care of. These omissions may have resulted in some weird bugs in theory (but probably they didn't have much impact in practice).
- PHP version related conditions inside *non-legacy arginfo files* used to possibly check for the 70000 version iD until now if compatibility with PHP 7.0 was declared in a stub. This was not 100% correct, since non-legacy arginfo files are only for PHP 8.0+. Now, I made sure that at least PHP version ID 80000 is used in the preprocessor conditions. The solution was a bit tricky though...
This commit is contained in:
Máté Kocsis 2024-03-18 22:06:17 +01:00 committed by GitHub
parent 4e934d789e
commit 5992a29724
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 119 additions and 28 deletions

View File

@ -122,17 +122,19 @@ function processStubFile(string $stubFile, Context $context, bool $includeOnly =
echo "Saved $arginfoFile\n";
}
if ($fileInfo->generateLegacyArginfoForPhpVersionId !== null && $fileInfo->generateLegacyArginfoForPhpVersionId < PHP_80_VERSION_ID) {
if ($fileInfo->shouldGenerateLegacyArginfo()) {
$legacyFileInfo = clone $fileInfo;
$legacyFileInfo->legacyArginfoGeneration = true;
$phpVersionIdMinimumCompatibility = $legacyFileInfo->getMinimumPhpVersionIdCompatibility();
foreach ($legacyFileInfo->getAllFuncInfos() as $funcInfo) {
$funcInfo->discardInfoForOldPhpVersions();
$funcInfo->discardInfoForOldPhpVersions($phpVersionIdMinimumCompatibility);
}
foreach ($legacyFileInfo->getAllClassInfos() as $classInfo) {
$classInfo->discardInfoForOldPhpVersions($phpVersionIdMinimumCompatibility);
}
foreach ($legacyFileInfo->getAllConstInfos() as $constInfo) {
$constInfo->discardInfoForOldPhpVersions();
}
foreach ($legacyFileInfo->getAllPropertyInfos() as $propertyInfo) {
$propertyInfo->discardInfoForOldPhpVersions();
$constInfo->discardInfoForOldPhpVersions($phpVersionIdMinimumCompatibility);
}
$arginfoCode = generateArgInfoCode(
@ -1532,14 +1534,18 @@ class FuncInfo {
return "\tF" . $this->return->refcount . '("' . addslashes($this->name->__toString()) . '", ' . $type->toOptimizerTypeMask() . "),\n";
}
public function discardInfoForOldPhpVersions(): void {
public function discardInfoForOldPhpVersions(?int $minimumPhpVersionIdCompatibility): void {
$this->attributes = [];
$this->return->type = null;
$this->framelessFunctionInfos = [];
$this->exposedDocComment = null;
$this->supportsCompileTimeEval = false;
foreach ($this->args as $arg) {
$arg->type = null;
$arg->defaultValue = null;
$arg->attributes = [];
}
$this->minimumPhpVersionIdCompatibility = $minimumPhpVersionIdCompatibility;
}
/** @return array<int, string[]> */
@ -2127,6 +2133,15 @@ OUPUT_EXAMPLE
$this->args[$key] = clone $argInfo;
}
$this->return = clone $this->return;
foreach ($this->attributes as $key => $attribute) {
$this->attributes[$key] = clone $attribute;
}
foreach ($this->framelessFunctionInfos as $key => $framelessFunctionInfo) {
$this->framelessFunctionInfos[$key] = clone $framelessFunctionInfo;
}
if ($this->exposedDocComment) {
$this->exposedDocComment = clone $this->exposedDocComment;
}
}
}
@ -2355,7 +2370,7 @@ abstract class VariableLike
/** @param array<string, ConstInfo> $allConstInfos */
abstract protected function getFieldSynopsisValueString(array $allConstInfos): ?string;
abstract public function discardInfoForOldPhpVersions(): void;
abstract public function discardInfoForOldPhpVersions(?int $minimumPhpVersionIdCompatibility): void;
protected function addTypeToFieldSynopsis(DOMDocument $doc, DOMElement $fieldsynopsisElement): void
{
@ -2628,17 +2643,19 @@ class ConstInfo extends VariableLike
return $entryElement;
}
public function discardInfoForOldPhpVersions(): void {
public function discardInfoForOldPhpVersions(?int $phpVersionIdMinimumCompatibility): void {
$this->type = null;
$this->flags &= ~Modifiers::FINAL;
$this->isDeprecated = false;
$this->attributes = [];
$this->phpVersionIdMinimumCompatibility = $phpVersionIdMinimumCompatibility;
}
/** @param array<string, ConstInfo> $allConstInfos */
public function getDeclaration(array $allConstInfos): string
{
$simpleType = ($this->phpDocType ?? $this->type)->tryToSimpleType();
$type = $this->phpDocType ?? $this->type;
$simpleType = $type ? $type->tryToSimpleType() : null;
if ($simpleType && $simpleType->name === "mixed") {
$simpleType = null;
}
@ -2909,10 +2926,11 @@ class PropertyInfo extends VariableLike
return $this->defaultValueString;
}
public function discardInfoForOldPhpVersions(): void {
public function discardInfoForOldPhpVersions(?int $phpVersionIdMinimumCompatibility): void {
$this->type = null;
$this->flags &= ~Modifiers::READONLY;
$this->attributes = [];
$this->phpVersionIdMinimumCompatibility = $phpVersionIdMinimumCompatibility;
}
/** @param array<string, ConstInfo> $allConstInfos */
@ -2940,7 +2958,6 @@ class PropertyInfo extends VariableLike
$code .= "\tzend_string *property_{$propertyName}_name = zend_string_init(\"$propertyName\", sizeof(\"$propertyName\") - 1, 1);\n";
$nameCode = "property_{$propertyName}_name";
$typeCode = $this->getTypeCode($propertyName, $code);
if ($this->exposedDocComment) {
$commentCode = "property_{$propertyName}_comment";
@ -2956,7 +2973,14 @@ class PropertyInfo extends VariableLike
} else {
$template = "\t";
}
$template .= "zend_declare_typed_property(class_entry, $nameCode, &$zvalName, %s, $commentCode, $typeCode);\n";
if ($this->phpVersionIdMinimumCompatibility === null || $this->phpVersionIdMinimumCompatibility >= PHP_80_VERSION_ID) {
$typeCode = $this->getTypeCode($propertyName, $code);
$template .= "zend_declare_typed_property(class_entry, $nameCode, &$zvalName, %s, $commentCode, $typeCode);\n";
} else {
$template .= "zend_declare_property_ex(class_entry, $nameCode, &$zvalName, %s, $commentCode);\n";
}
$flagsCode = generateVersionDependentFlagCode(
$template,
$this->getFlagsByPhpVersion(),
@ -3007,6 +3031,12 @@ class PropertyInfo extends VariableLike
if ($this->type) {
$this->type = clone $this->type;
}
foreach ($this->attributes as $key => $attribute) {
$this->attributes[$key] = clone $attribute;
}
if ($this->exposedDocComment) {
$this->exposedDocComment = clone $this->exposedDocComment;
}
}
}
@ -3383,6 +3413,19 @@ class ClassInfo {
];
}
public function discardInfoForOldPhpVersions(?int $phpVersionIdMinimumCompatibility): void {
$this->attributes = [];
$this->flags &= ~Modifiers::READONLY;
$this->exposedDocComment = null;
$this->isStrictProperties = false;
$this->isNotSerializable = false;
foreach ($this->propertyInfos as $propertyInfo) {
$propertyInfo->discardInfoForOldPhpVersions($phpVersionIdMinimumCompatibility);
}
$this->phpVersionIdMinimumCompatibility = $phpVersionIdMinimumCompatibility;
}
/**
* @param array<string, ClassInfo> $classMap
* @param array<string, ConstInfo> $allConstInfos
@ -3801,6 +3844,10 @@ class ClassInfo {
public function __clone()
{
foreach ($this->constInfos as $key => $constInfo) {
$this->constInfos[$key] = clone $constInfo;
}
foreach ($this->propertyInfos as $key => $propertyInfo) {
$this->propertyInfos[$key] = clone $propertyInfo;
}
@ -3808,6 +3855,14 @@ class ClassInfo {
foreach ($this->funcInfos as $key => $funcInfo) {
$this->funcInfos[$key] = clone $funcInfo;
}
foreach ($this->attributes as $key => $attribute) {
$this->attributes[$key] = clone $attribute;
}
if ($this->exposedDocComment) {
$this->exposedDocComment = clone $this->exposedDocComment;
}
}
/**
@ -3848,9 +3903,10 @@ class FileInfo {
public array $classInfos = [];
public bool $generateFunctionEntries = false;
public string $declarationPrefix = "";
public ?int $generateLegacyArginfoForPhpVersionId = null;
public bool $generateClassEntries = false;
public bool $isUndocumentable = false;
public bool $legacyArginfoGeneration = false;
private ?int $minimumPhpVersionIdCompatibility = null;
/**
* @return iterable<FuncInfo>
@ -3880,16 +3936,20 @@ class FileInfo {
}
/**
* @return iterable<PropertyInfo>
* @return iterable<ClassInfo>
*/
public function getAllPropertyInfos(): iterable {
public function getAllClassInfos(): iterable {
foreach ($this->classInfos as $classInfo) {
yield from $classInfo->propertyInfos;
yield $classInfo;
}
}
public function __clone()
{
foreach ($this->constInfos as $key => $constInfo) {
$this->constInfos[$key] = clone $constInfo;
}
foreach ($this->funcInfos as $key => $funcInfo) {
$this->funcInfos[$key] = clone $funcInfo;
}
@ -3898,6 +3958,26 @@ class FileInfo {
$this->classInfos[$key] = clone $classInfo;
}
}
public function setMinimumPhpVersionIdCompatibility(?int $minimumPhpVersionIdCompatibility) {
$this->minimumPhpVersionIdCompatibility = $minimumPhpVersionIdCompatibility;
}
public function getMinimumPhpVersionIdCompatibility(): ?int {
// Non-legacy arginfo files are always PHP 8.0+ compatible
if (!$this->legacyArginfoGeneration &&
$this->minimumPhpVersionIdCompatibility !== null &&
$this->minimumPhpVersionIdCompatibility < PHP_80_VERSION_ID
) {
return PHP_80_VERSION_ID;
}
return $this->minimumPhpVersionIdCompatibility;
}
public function shouldGenerateLegacyArginfo(): bool {
return $this->minimumPhpVersionIdCompatibility !== null && $this->minimumPhpVersionIdCompatibility < PHP_80_VERSION_ID;
}
}
class DocCommentTag {
@ -4541,7 +4621,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$stmt->getComments(),
$cond,
$fileInfo->isUndocumentable,
$fileInfo->generateLegacyArginfoForPhpVersionId,
$fileInfo->getMinimumPhpVersionIdCompatibility(),
[]
);
}
@ -4557,7 +4637,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$stmt,
$cond,
$fileInfo->isUndocumentable,
$fileInfo->generateLegacyArginfoForPhpVersionId
$fileInfo->getMinimumPhpVersionIdCompatibility()
);
continue;
}
@ -4588,7 +4668,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$classStmt->getComments(),
$cond,
$fileInfo->isUndocumentable,
$fileInfo->generateLegacyArginfoForPhpVersionId,
$fileInfo->getMinimumPhpVersionIdCompatibility(),
createAttributes($classStmt->attrGroups)
);
}
@ -4604,7 +4684,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$classStmt->type,
$classStmt->getComments(),
$prettyPrinter,
$fileInfo->generateLegacyArginfoForPhpVersionId,
$fileInfo->getMinimumPhpVersionIdCompatibility(),
createAttributes($classStmt->attrGroups)
);
}
@ -4620,7 +4700,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$classStmt,
$cond,
$fileInfo->isUndocumentable,
$fileInfo->generateLegacyArginfoForPhpVersionId
$fileInfo->getMinimumPhpVersionIdCompatibility()
);
} else if ($classStmt instanceof Stmt\EnumCase) {
$enumCaseInfos[] = new EnumCaseInfo(
@ -4631,7 +4711,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
}
$fileInfo->classInfos[] = parseClass(
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos, $cond, $fileInfo->generateLegacyArginfoForPhpVersionId, $fileInfo->isUndocumentable
$className, $stmt, $constInfos, $propertyInfos, $methodInfos, $enumCaseInfos, $cond, $fileInfo->getMinimumPhpVersionIdCompatibility(), $fileInfo->isUndocumentable
);
continue;
}
@ -4678,11 +4758,11 @@ function parseStubFile(string $code): FileInfo {
throw new Exception(
"Legacy PHP version must be one of: \"" . PHP_70_VERSION_ID . "\" (PHP 7.0), \"" . PHP_80_VERSION_ID . "\" (PHP 8.0), " .
"\"" . PHP_81_VERSION_ID . "\" (PHP 8.1), \"" . PHP_82_VERSION_ID . "\" (PHP 8.2), \"" . PHP_83_VERSION_ID . "\" (PHP 8.3), " .
"\"" . $tag->value . "\" provided"
"\"" . PHP_84_VERSION_ID . "\" (PHP 8.4), \"" . $tag->value . "\" provided"
);
}
$fileInfo->generateLegacyArginfoForPhpVersionId = $tag->value ? (int) $tag->value : PHP_70_VERSION_ID;
$fileInfo->setMinimumPhpVersionIdCompatibility($tag->value ? (int) $tag->value : PHP_70_VERSION_ID);
} else if ($tag->name === 'generate-class-entries') {
$fileInfo->generateClassEntries = true;
$fileInfo->declarationPrefix = $tag->value ? $tag->value . " " : "";
@ -4705,7 +4785,7 @@ function funcInfoToCode(FileInfo $fileInfo, FuncInfo $funcInfo): string {
$code = '';
$returnType = $funcInfo->return->type;
$isTentativeReturnType = $funcInfo->return->tentativeReturnType;
$php81MinimumCompatibility = $fileInfo->generateLegacyArginfoForPhpVersionId === null || $fileInfo->generateLegacyArginfoForPhpVersionId >= PHP_81_VERSION_ID;
$php81MinimumCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility() === null || $fileInfo->getMinimumPhpVersionIdCompatibility() >= PHP_81_VERSION_ID;
if ($returnType !== null) {
if ($isTentativeReturnType && !$php81MinimumCompatibility) {
@ -4864,6 +4944,14 @@ function generateArgInfoCode(
$code = "/* This is a generated file, edit the .stub.php file instead.\n"
. " * Stub hash: $stubHash */\n";
$minimumPhpVersionIdCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility();
if ($minimumPhpVersionIdCompatibility !== null) {
$code .= "\nZEND_STATIC_ASSERT(PHP_VERSION_ID >= $minimumPhpVersionIdCompatibility, ";
$code .= "\"{$stubFilenameWithoutExtension}_arginfo.h only supports ";
$code .= "PHP version ID $minimumPhpVersionIdCompatibility or newer, \"\n";
$code .= "\t\"but it is included on an older PHP version\");\n";
}
$generatedFuncInfos = [];
$argInfoCode = generateCodeWithConditions(
@ -4924,10 +5012,10 @@ function generateArgInfoCode(
}
}
$php80MinimumCompatibility = $fileInfo->generateLegacyArginfoForPhpVersionId === null || $fileInfo->generateLegacyArginfoForPhpVersionId >= PHP_80_VERSION_ID;
$php80MinimumCompatibility = $fileInfo->getMinimumPhpVersionIdCompatibility() === null || $fileInfo->getMinimumPhpVersionIdCompatibility() >= PHP_80_VERSION_ID;
if ($fileInfo->generateClassEntries) {
if ($attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->generateLegacyArginfoForPhpVersionId, null)) {
if ($attributeInitializationCode = generateFunctionAttributeInitialization($fileInfo->funcInfos, $allConstInfos, $fileInfo->getMinimumPhpVersionIdCompatibility(), null)) {
if (!$php80MinimumCompatibility) {
$attributeInitializationCode = "\n#if (PHP_VERSION_ID >= " . PHP_80_VERSION_ID . ")" . $attributeInitializationCode . "#endif\n";
}

View File

@ -1,6 +1,9 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: dd3f852ea9f8e3a356ed226380edf5cc336f8a4e */
ZEND_STATIC_ASSERT(PHP_VERSION_ID >= 80000, "test_arginfo.h only supports PHP version ID 80000 or newer, "
"but it is included on an older PHP version");
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()