From be69262137825d3559093df289b36b1da5f8ea31 Mon Sep 17 00:00:00 2001 From: DanielEScherzer Date: Sat, 30 Nov 2024 11:47:56 -0800 Subject: [PATCH] gen_stub: drop support for `@refcount 0` with scalar return (#16505) * 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. --- build/gen_stub.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/build/gen_stub.php b/build/gen_stub.php index 1468a0b68b1..800262f27e5 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -1159,8 +1159,7 @@ class ReturnInfo { const REFCOUNT_1 = "1"; const REFCOUNT_N = "N"; - const REFCOUNTS = [ - self::REFCOUNT_0, + const REFCOUNTS_NONSCALAR = [ self::REFCOUNT_1, self::REFCOUNT_N, ]; @@ -1204,16 +1203,14 @@ class ReturnInfo { return; } - if (!in_array($refcount, ReturnInfo::REFCOUNTS, true)) { - throw new Exception("@refcount must have one of the following values: \"0\", \"1\", \"N\", $refcount given"); + if ($isScalarType) { + throw new Exception( + "@refcount on functions returning scalar values is redundant and not permitted" + ); } - if ($isScalarType && $refcount !== self::REFCOUNT_0) { - throw new Exception('A scalar return type of "' . $type->__toString() . '" must have a refcount of "' . self::REFCOUNT_0 . '"'); - } - - if (!$isScalarType && $refcount === self::REFCOUNT_0) { - throw new Exception('A non-scalar return type of "' . $type->__toString() . '" cannot have a refcount of "' . self::REFCOUNT_0 . '"'); + if (!in_array($refcount, ReturnInfo::REFCOUNTS_NONSCALAR, true)) { + throw new Exception("@refcount must have one of the following values: \"1\", \"N\", $refcount given"); } $this->refcount = $refcount;