Skip to content

Commit 3caf15a

Browse files
gen_stub: drop support for @refcount 0 with scalar return
Currenty, if `@refcount` is omitted, it is assumed to be 0 for scalar return types and "N" otherwise. On the other hand, if `@refcount` is provided, for a scalar return type it *must* be 0, and for a non-scalar return type it *must not* be 0. In other words, the ref count will be 0 if and only if the return type is scalar, regardless of whether the `@refcount` tag is used. In that case, adding `@refcount 0` does nothing, and since its presence suggests it does something (why would a developer add code that does nothing?) it is confusing and should be removed. As it happens, there are currently no uses of `@refcount 0` in php-src, but why should we allow future uses? Removing this support also allows simplifying the code a bit. In the process, I also renamed some of the constants for clarity.
1 parent a00c734 commit 3caf15a

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

build/gen_stub.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,12 +1155,13 @@ public function isDestructor(): bool {
11551155
}
11561156

11571157
class ReturnInfo {
1158-
const REFCOUNT_0 = "0";
1158+
// REFCOUNT_SCALAR (formerly REFCOUNT_0) is automatically applied for
1159+
// scalars, and not allowed for non-scalars
1160+
const REFCOUNT_SCALAR = "0";
11591161
const REFCOUNT_1 = "1";
11601162
const REFCOUNT_N = "N";
11611163

1162-
const REFCOUNTS = [
1163-
self::REFCOUNT_0,
1164+
const REFCOUNTS_NONSCALAR = [
11641165
self::REFCOUNT_1,
11651166
self::REFCOUNT_N,
11661167
];
@@ -1200,20 +1201,16 @@ private function setRefcount(?string $refcount): void
12001201
$isScalarType = $type !== null && $type->isScalar();
12011202

12021203
if ($refcount === null) {
1203-
$this->refcount = $isScalarType ? self::REFCOUNT_0 : self::REFCOUNT_N;
1204+
$this->refcount = $isScalarType ? self::REFCOUNT_SCALAR : self::REFCOUNT_N;
12041205
return;
12051206
}
12061207

1207-
if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) {
1208-
throw new Exception("@refcount must have one of the following values: \"0\", \"1\", \"N\", $refcount given");
1208+
if ($isScalarType) {
1209+
throw new Exception("@refcount is not permitted on functions returning scalar values");
12091210
}
12101211

1211-
if ($isScalarType && $refcount !== self::REFCOUNT_0) {
1212-
throw new Exception('A scalar return type of "' . $type->__toString() . '" must have a refcount of "' . self::REFCOUNT_0 . '"');
1213-
}
1214-
1215-
if (!$isScalarType && $refcount === self::REFCOUNT_0) {
1216-
throw new Exception('A non-scalar return type of "' . $type->__toString() . '" cannot have a refcount of "' . self::REFCOUNT_0 . '"');
1212+
if (!in_array($refcount, ReturnInfo::REFCOUNTS_NONSCALAR, true)) {
1213+
throw new Exception("@refcount must have one of the following values: \"1\", \"N\", $refcount given");
12171214
}
12181215

12191216
$this->refcount = $refcount;

0 commit comments

Comments
 (0)