Skip to content

Commit f62737f

Browse files
ondrejmirtesclaude
andcommitted
Keep a list's growth range at PHP_INT_MAX in IntersectionType::setOffsetValueType()
When setting an offset on a list turns the result into a non-list, IntersectionType::setOffsetValueType() keeps the list accessory if the offset lies in int<0, N + 1> for a known offset N of the list. With N = PHP_INT_MAX, `N + 1` became a float and IntegerRangeType::fromInterval() threw a TypeError; the native port computed it in signed arithmetic, which overflowed. No offset exists past PHP_INT_MAX, so the range is now open-ended there (int<0, max>) in the twin and the port alike. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 4dea335 commit f62737f

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/Type/IntersectionType.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
use function strlen;
7272
use function substr;
7373
use function usort;
74+
use const PHP_INT_MAX;
7475

7576
/** @api */
7677
#[InstanceofDeprecated]
@@ -1169,7 +1170,9 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
11691170
if (!is_int($constantScalarValue)) {
11701171
continue;
11711172
}
1172-
if (IntegerRangeType::fromInterval(0, $constantScalarValue + 1)->isSuperTypeOf($offsetType)->yes()) {
1173+
// no offset exists past PHP_INT_MAX: the list may grow up to it
1174+
$maxListOffset = $constantScalarValue < PHP_INT_MAX ? $constantScalarValue + 1 : null;
1175+
if (IntegerRangeType::fromInterval(0, $maxListOffset)->isSuperTypeOf($offsetType)->yes()) {
11731176
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
11741177
break 2;
11751178
}

‎tests/PHPStan/Type/IntersectionTypeTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Traversable;
3333
use function count;
3434
use function sprintf;
35+
use const PHP_INT_MAX;
3536
use const PHP_VERSION_ID;
3637

3738
class IntersectionTypeTest extends PHPStanTestCase
@@ -1016,4 +1017,17 @@ public function testConstantOfIntersectionWithTemplateMixed(IntersectionType $ty
10161017
$this->assertSame(DateTimeInterface::class, $constant->getDeclaringClass()->getName());
10171018
}
10181019

1020+
public function testSetOffsetValueTypeOnListWithOffsetAtIntMax(): void
1021+
{
1022+
$type = new IntersectionType([
1023+
new ArrayType(new IntegerType(), new MixedType()),
1024+
new AccessoryArrayListType(),
1025+
new HasOffsetType(new ConstantIntegerType(PHP_INT_MAX)),
1026+
]);
1027+
1028+
$result = $type->setOffsetValueType(new ConstantIntegerType(3), new ConstantIntegerType(1));
1029+
$this->assertTrue($result->isList()->yes());
1030+
$this->assertSame('non-empty-list&hasOffset(9223372036854775807)&hasOffsetValue(3, 1)', $result->describe(VerbosityLevel::precise()));
1031+
}
1032+
10191033
}

‎turbo-ext/src/IntersectionType.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,9 @@ class IntersectionType
16341634
for (zv::ArrayEntry valueEntry : zv::ArrRef(values.raw())) {
16351635
zv::Ref value = valueEntry.value().deref();
16361636
if (!value.isLong()) continue;
1637-
zv::Val range = integerRange(NullableLong::of(0), NullableLong::of(value.asLong() + 1));
1637+
/* no offset exists past PHP_INT_MAX: the list may grow up to it */
1638+
NullableLong maxListOffset = value.asLong() < ZEND_LONG_MAX ? NullableLong::of(value.asLong() + 1) : NullableLong::null();
1639+
zv::Val range = integerRange(NullableLong::of(0), maxListOffset);
16381640
if (UNEXPECTED(range.isUndef())) return zv::Val();
16391641
zend_long covered = pt_type_call_result_trinary(Z_OBJ_P(range.raw()), PT_LC("issupertypeof"), 1, offsetType);
16401642
if (UNEXPECTED(covered < 0)) return zv::Val();

‎turbo-ext/tests/type-family.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8420,6 +8420,12 @@ public function getTypes(): array
84208420
$cacheEnabledProperty->setValue(null, $cacheEnabledBefore);
84218421
}
84228422

8423+
// a list with an offset at PHP_INT_MAX: an offset past it cannot exist
8424+
$intMaxList = new \PHPStan\Type\IntersectionType([new \PHPStan\Type\ArrayType(new \PHPStan\Type\IntegerType(), new \PHPStan\Type\MixedType()), new \PHPStan\Type\Accessory\AccessoryArrayListType(), new \PHPStan\Type\Accessory\HasOffsetType(new \PHPStan\Type\Constant\ConstantIntegerType(PHP_INT_MAX))]);
8425+
foreach ([3, PHP_INT_MAX, -1] as $offset) {
8426+
$r["intersection list at PHP_INT_MAX setOffsetValueType $offset"] = $misuse(static fn () => $intMaxList->setOffsetValueType(new \PHPStan\Type\Constant\ConstantIntegerType($offset), new \PHPStan\Type\Constant\ConstantIntegerType(1)), true);
8427+
}
8428+
84238429
// countConstantArrayValueTypes() hands each element to TypeTraverser::map(Type $type, ...)
84248430
foreach (['string' => 'x', 'object' => new \stdClass()] as $elementName => $element) {
84258431
$r["combinator countConstantArrayValueTypes $elementName"] = $misuse(static fn () => \PHPStan\Type\TypeCombinator::countConstantArrayValueTypes([new \PHPStan\Type\IntegerType(), $element]));

0 commit comments

Comments
 (0)