Skip to content

Commit 77badfd

Browse files
ondrejmirtesclaude
andcommitted
Do not expand optional-key shapes into every variant before loop widening
MutatingScope::generalizeType() flattened both sides with TypeUtils::flattenTypes(), which turns a constant array with N optional keys into its 2^N concrete variants (four lossy representatives above ten) - only for the union right after to merge them back into the same shape, paying the pairwise reduceArrays() pass for hundreds of variants on every loop iteration. A method building a shape from a dozen conditional writes spent 1.8 s there. The per-key widening reads keys, values and optionality straight from the shape, so only unions are split now. The bug-13637 fixture keeps the loop's int<0, 8> bound on the middle key (the comment had called the previous `int` a residual) and describes the innermost level as the optional-key shape it is. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DfQHLTyyoMfjfa4sXJCGa5
1 parent 36170ac commit 77badfd

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5006,6 +5006,31 @@ private function generalizeVariableTypeHolders(
50065006
return $newVariableTypeHolders;
50075007
}
50085008

5009+
/**
5010+
* TypeUtils::flattenTypes() expands a shape with optional keys into every
5011+
* concrete variant (2^N of them, four lossy representatives above ten),
5012+
* only for the union below to merge them back into the very same shape -
5013+
* quadratic in the number of variants. The per-key widening reads the
5014+
* shape's keys, values and optionality directly, so only unions are split.
5015+
*
5016+
* @return list<Type>
5017+
*/
5018+
private function flattenUnionForGeneralization(Type $type): array
5019+
{
5020+
if (!$type instanceof UnionType) {
5021+
return [$type];
5022+
}
5023+
5024+
$types = [];
5025+
foreach ($type->getTypes() as $innerType) {
5026+
foreach ($this->flattenUnionForGeneralization($innerType) as $flattenedType) {
5027+
$types[] = $flattenedType;
5028+
}
5029+
}
5030+
5031+
return $types;
5032+
}
5033+
50095034
private function generalizeType(Type $a, Type $b, int $depth): Type
50105035
{
50115036
if ($a->equals($b)) {
@@ -5033,8 +5058,8 @@ private function generalizeType(Type $a, Type $b, int $depth): Type
50335058
$otherTypes = [];
50345059

50355060
foreach ([
5036-
'a' => TypeUtils::flattenTypes($a),
5037-
'b' => TypeUtils::flattenTypes($b),
5061+
'a' => $this->flattenUnionForGeneralization($a),
5062+
'b' => $this->flattenUnionForGeneralization($b),
50385063
] as $key => $types) {
50395064
foreach ($types as $type) {
50405065
if ($type instanceof ConstantIntegerType) {

‎tests/PHPStan/Analyser/nsrt/bug-13637.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function doesNotWork(): void
1717
}
1818

1919
// The reported regression (innermost values widening to `int<0, max>`) is
20-
// fixed: they stay `int<0, 4>`. The middle key degenerates to `int` rather
21-
// than the ideal `int<0, 8>` — a minor key-precision residual in 3-level
22-
// nesting, not the value-widening bug from the issue.
23-
assertType('non-empty-array<int<0, 4>, non-empty-array<int, non-empty-array<int<1, 9>, array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}>>>', $final);
20+
// fixed: they stay `int<0, 4>`, and the middle key keeps the loop's
21+
// `int<0, 8>` bound now that widening no longer expands optional-key
22+
// shapes into every variant before merging them back.
23+
assertType('non-empty-array<int<0, 4>, non-empty-array<int<0, 8>, non-empty-array{3?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 1?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 2?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 4?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 5?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 6?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 7?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 8?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}, 9?: array{abc: int<0, 4>, def?: int<0, 4>, ghi?: int<0, 4>}}>>', $final);
2424
}
2525

2626
function thisWorks(): void

0 commit comments

Comments
 (0)