Skip to content

Commit e606ca2

Browse files
ondrejmirtesclaude
andcommitted
Merge unions of constant arrays with many keys instead of degrading
processArrayTypes() numbered every distinct constant key with a power of two and degraded the whole union to a general array once the counter left the int range - a hidden cap of 62 distinct keys. The numbering's consumer (per-array bitmask bucketing) was removed in 1cdd9ba, so all that survived was the accidental degradation, which also made large unions behave differently from small ones (the >62-key path collapsed same-key-set shapes losslessly via reduceArrays(false), while the small path preserves them as tagged unions). Keep a plain seen-key set for deduplication and let unions of constant arrays of any size take the tagged-union-preserving path; oversized unions still degrade via ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT in optimizeConstantArrays(). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
1 parent 408f6e7 commit e606ca2

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

‎src/Type/TypeCombinator.php‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
use function get_class;
4444
use function implode;
4545
use function in_array;
46-
use function is_int;
4746
use function sprintf;
4847
use function usort;
4948
use const PHP_INT_MAX;
@@ -1032,19 +1031,11 @@ private static function processArrayTypes(array $arrayTypes): array
10321031
$keyTypesForGeneralArray = [];
10331032
$valueTypesForGeneralArray = [];
10341033
$generalArrayOccurred = false;
1035-
$constantKeyTypesNumbered = [];
1036-
$filledArrays = 0;
1037-
$overflowed = false;
1038-
1039-
/** @var int|float $nextConstantKeyTypeIndex */
1040-
$nextConstantKeyTypeIndex = 1;
1034+
$seenConstantKeyTypes = [];
10411035

10421036
foreach ($arrayTypes as $arrayType) {
10431037
$constantArrays = $arrayType->getConstantArrays();
10441038
$isConstantArray = $constantArrays !== [];
1045-
if (!$isConstantArray || !$arrayType->isIterableAtLeastOnce()->no()) {
1046-
$filledArrays++;
1047-
}
10481039

10491040
if (!$isConstantArray) {
10501041
foreach ($arrayType->getArrays() as $type) {
@@ -1061,23 +1052,16 @@ private static function processArrayTypes(array $arrayTypes): array
10611052
$valueTypesForGeneralArray[] = $valueTypes[$i];
10621053

10631054
$keyTypeValue = $keyType->getValue();
1064-
if (array_key_exists($keyTypeValue, $constantKeyTypesNumbered)) {
1055+
if (array_key_exists($keyTypeValue, $seenConstantKeyTypes)) {
10651056
continue;
10661057
}
1058+
$seenConstantKeyTypes[$keyTypeValue] = true;
10671059
$keyTypesForGeneralArray[] = $keyType;
1068-
1069-
$constantKeyTypesNumbered[$keyTypeValue] = $nextConstantKeyTypeIndex;
1070-
$nextConstantKeyTypeIndex *= 2;
1071-
if (!is_int($nextConstantKeyTypeIndex)) {
1072-
$generalArrayOccurred = true;
1073-
$overflowed = true;
1074-
continue 2;
1075-
}
10761060
}
10771061
}
10781062
}
10791063

1080-
if ($generalArrayOccurred && (!$overflowed || $filledArrays > 1)) {
1064+
if ($generalArrayOccurred) {
10811065
$reducedArrayTypes = self::reduceArrays($arrayTypes, false);
10821066
if (count($reducedArrayTypes) === 1) {
10831067
return [self::intersect($reducedArrayTypes[0], ...$accessoryTypes)];

‎tests/PHPStan/Analyser/nsrt/preserve-large-constant-array.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function multiKeys(array $arr): void
1717
return;
1818
}
1919

20-
assertType('array{1: string|null, 2: int|null, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float}', $arr);
20+
assertType('array{1: null, 2: null, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float}|array{1: string, 2: int, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float}', $arr);
2121
echo 1;
2222
}
2323

@@ -65,3 +65,14 @@ function multipleOptions(array $arr): void
6565
assertType('array{1: string, 2: int, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float}', $brr);
6666
echo 1;
6767
}
68+
69+
/**
70+
* @param array{1: string, 2: int, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float} $full
71+
* @param array{2: int, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float} $withoutFirst
72+
*/
73+
function unionOfManyKeys(array $full, array $withoutFirst): void
74+
{
75+
$union = rand(0, 1) === 0 ? $full : $withoutFirst;
76+
assertType('array{1?: string, 2: int, 3: bool, 4: string, 5: int, 6: bool, 7: string, 8: int, 9: bool, 10: string, 11: int, 12: bool, 13: string, 14: int, 15: bool, 16: string, 17: int, 18: bool, 19: string, 20: int, 21: bool, 22: string, 23: int, 24: bool, 25: string, 26: int, 27: bool, 28: string, 29: int, 30: bool, 31: string, 32: int, 33: bool, 34: string, 35: int, 36: bool, 37: string, 38: int, 39: bool, 40: string, 41: int, 42: bool, 43: string, 44: int, 45: bool, 46: string, 47: int, 48: bool, 49: string, 50: int, 51: bool, 52: string, 53: int, 54: bool, 55: string, 56: int, 57: bool, 58: string, 59: int, 60: bool, 61: string, 62: int, 63: bool, 64: float}', $union);
77+
echo 1;
78+
}

0 commit comments

Comments
 (0)