From b30a9281a34015b8683495267041a8a66196f871 Mon Sep 17 00:00:00 2001 From: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:51:52 +0000 Subject: [PATCH 1/3] Keep the literal key/value union when generalizing constant array shapes of unknown sealedness in loops - MutatingScope::generalizeType(): when two constant array shapes with different key sets are merged into a general ArrayType, keep the union of their keys and values unless a shape is definitely unsealed (`isUnsealed()->yes()`). Previously this required `isSealed()->yes()`, which only holds with bleeding edge. Without it, every shape has unknown sealedness, so loop-built arrays like `for ($i = 0; $i <= 3; $i++) { $a[$i] = ...; }` widened their keys to `int<0, max>`. Since 2.2 `$a[0|1] = ...` produces the shape `array{0?: ..., 1?: ...}` instead of `array<0|1, ...>`, so the constant-array path is taken where the general-array path used to keep the bound. - Port the same condition to the native mirror in turbo-ext/src/MutatingScope.cpp. - ScopeTest::testGeneralize: legacy shapes now generalize like sealed ones (`non-empty-array<'a'|'b', 1|2>` instead of widening to `literal-string` / `int<1, max>`). - New LegacyArrayShapeTypeInferenceTest runs without bleeding edge and covers for (<=, <, decrementing, conditional assignment, pre-existing string key, nested) and while loops. foreach over constant arrays was already precise. do-while stays at int<0, max> with and without bleeding edge: its body never sees the narrowed counter, so that is a separate limitation. --- src/Analyser/MutatingScope.php | 26 +++--- .../LegacyArrayShapeTypeInferenceTest.php | 32 +++++++ tests/PHPStan/Analyser/ScopeTest.php | 8 +- tests/PHPStan/Analyser/data/bug-15749.php | 85 +++++++++++++++++++ turbo-ext/src/MutatingScope.cpp | 22 ++--- 5 files changed, 145 insertions(+), 28 deletions(-) create mode 100644 tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php create mode 100644 tests/PHPStan/Analyser/data/bug-15749.php diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 36da011e02a..e5b9911ffe9 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -5286,25 +5286,25 @@ private function generalizeType(Type $a, Type $b, int $depth): Type $resultTypes[] = $resultArrayBuilder->getArray(); } else { - // Both inputs are sealed constant array shapes — their key - // sets are finite by construction. On the fall-through - // ArrayType path, recursing into `generalizeType` would - // widen e.g. `0|1` to `int<0, max>` — for both the keys and - // the values — losing the loop's per-iteration precision. - // Keep the literal union instead so the loop's bounds stay - // visible. (Scoped to sealed shapes so the general - // `generalize()` widening contract for legacy arrays — see - // ScopeTest::testGeneralize — is unaffected.) - $bothSealed = true; + // Neither input has unsealed extra keys, so their key sets + // are finite — the constant keys of the shapes. That holds + // for sealed shapes as well as for shapes of unknown + // sealedness (the only kind without bleeding edge). On the + // fall-through ArrayType path, recursing into + // `generalizeType` would widen e.g. `0|1` to `int<0, max>` + // — for both the keys and the values — losing the loop's + // per-iteration precision. Keep the literal union instead + // so the loop's bounds stay visible. + $noneUnsealed = true; foreach ([...$constantArrays['a'], ...$constantArrays['b']] as $constantArrayCheck) { foreach ($constantArrayCheck->getConstantArrays() as $constantArrayInstance) { - if (!$constantArrayInstance->isSealed()->yes()) { - $bothSealed = false; + if ($constantArrayInstance->isUnsealed()->yes()) { + $noneUnsealed = false; break 2; } } } - if ($bothSealed) { + if ($noneUnsealed) { $resultKeyType = TypeCombinator::union($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType()); $resultValueType = TypeCombinator::union($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType()); if ($resultValueType->isOversizedArray()->yes()) { diff --git a/tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php b/tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php new file mode 100644 index 00000000000..548778eeb9d --- /dev/null +++ b/tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php @@ -0,0 +1,32 @@ +assertFileAsserts($assertType, $file, ...$args); + } + +} diff --git a/tests/PHPStan/Analyser/ScopeTest.php b/tests/PHPStan/Analyser/ScopeTest.php index 1912d549581..59c54ff0f65 100644 --- a/tests/PHPStan/Analyser/ScopeTest.php +++ b/tests/PHPStan/Analyser/ScopeTest.php @@ -26,8 +26,8 @@ public static function dataGeneralize(): array { // A directly constructed shape is a legacy (unsealed) one only while bleeding // edge is off - the toggle is process-global, and PHPUnit 9 evaluates the - // provider after earlier test classes may have left it on. The widening - // contract below is the legacy one, so the inputs are pinned to it. + // provider after earlier test classes may have left it on. The inputs are + // pinned to legacy shapes, so the expectations below are for those. return BleedingEdgeToggle::withBleedingEdge(false, static fn (): array => [ [ new ConstantStringType('a'), @@ -147,7 +147,7 @@ public static function dataGeneralize(): array new ConstantIntegerType(1), new ConstantIntegerType(1), ]), - 'non-empty-array', + 'non-empty-array<\'a\'|\'b\', 1>', ], [ new ConstantArrayType([ @@ -162,7 +162,7 @@ public static function dataGeneralize(): array new ConstantIntegerType(1), new ConstantIntegerType(2), ]), - 'non-empty-array>', + 'non-empty-array<\'a\'|\'b\', 1|2>', ], [ new UnionType([ diff --git a/tests/PHPStan/Analyser/data/bug-15749.php b/tests/PHPStan/Analyser/data/bug-15749.php new file mode 100644 index 00000000000..a5f28d34546 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-15749.php @@ -0,0 +1,85 @@ +, int> */ +function foo(): array { + $a = [ ]; + for ($i = 0; $i <= 3; $i++) { + $a[$i] = random_int(0, 100); + } + assertType('non-empty-array, int<0, 100>>', $a); + return $a; +} + +function lessThan(): void { + $arr = []; + for ($i = 0; $i < 5; $i++) { + $arr[$i] = 'v'; + } + assertType("non-empty-array, 'v'>", $arr); +} + +function withCondition(): void { + $arr = []; + for ($i = 0; $i < 5; $i++) { + if (rand()) { + $arr[$i] = 'v'; + } + } + assertType("array, 'v'>", $arr); +} + +function existingKey(): void { + $arr = ['x' => 0]; + for ($i = 0; $i < 5; $i++) { + $arr[$i] = $i; + } + assertType("non-empty-array<'x'|int<0, 4>, int<0, 4>>", $arr); +} + +function whileLoop(): void { + $a = []; + $i = 0; + while ($i <= 3) { + $a[$i] = 1; + $i++; + } + assertType('non-empty-array, 1>', $a); +} + +function foreachRange(): void { + $a = []; + foreach (range(0, 3) as $i) { + $a[$i] = 1; + } + assertType('array{1, 1, 1, 1}', $a); +} + +function stringKeys(): void { + $a = []; + foreach (['a', 'b', 'c'] as $k) { + $a[$k] = 1; + } + assertType("array{a: 1, b: 1, c: 1}", $a); +} + +function decrementing(): void { + $a = []; + for ($i = 3; $i >= 0; $i--) { + $a[$i] = 1; + } + assertType('non-empty-array, 1>', $a); +} + +function nested(): void { + $a = []; + for ($i = 0; $i <= 2; $i++) { + for ($j = 0; $j <= 2; $j++) { + $a[$i][$j] = 1; + } + } + assertType('non-empty-array, non-empty-array, 1>>', $a); +} diff --git a/turbo-ext/src/MutatingScope.cpp b/turbo-ext/src/MutatingScope.cpp index 79179d1be27..706a7ce8f08 100644 --- a/turbo-ext/src/MutatingScope.cpp +++ b/turbo-ext/src/MutatingScope.cpp @@ -9674,23 +9674,23 @@ class MutatingScope if (UNEXPECTED(resultArray.isUndef())) return zv::Val(); resultTypes.push(std::move(resultArray)); } else { - /* Both inputs are sealed constant array shapes — see the - * twin's comment: keep the literal union instead of - * widening the keys and values */ - bool bothSealed = true; - for (uint32_t side = 0; side < 2 && bothSealed; side++) { + /* Neither input has unsealed extra keys — see the twin's + * comment: keep the literal union instead of widening the + * keys and values */ + bool noneUnsealed = true; + for (uint32_t side = 0; side < 2 && noneUnsealed; side++) { for (auto checkEntry : zv::TableRef(constantArrays.table((int) side))) { zv::Val constantArrayInstances = pt_type_op(checkEntry.value().deref().asObject(), PT_OP_GET_CONSTANT_ARRAYS, 0, NULL); if (UNEXPECTED(constantArrayInstances.isUndef() || Z_TYPE_P(constantArrayInstances.raw()) != IS_ARRAY)) return zv::Val(); for (auto instance : zv::TableRef(Z_ARRVAL_P(constantArrayInstances.raw()))) { - zend_long isSealed = pt_type_call_trinary(instance.value().deref().asObject(), PT_LC("issealed"), 0, NULL); - if (UNEXPECTED(isSealed < 0)) return zv::Val(); - if (isSealed != PT_TRI_YES) { - bothSealed = false; + zend_long isUnsealed = pt_type_call_trinary(instance.value().deref().asObject(), PT_LC("isunsealed"), 0, NULL); + if (UNEXPECTED(isUnsealed < 0)) return zv::Val(); + if (isUnsealed == PT_TRI_YES) { + noneUnsealed = false; break; } } - if (!bothSealed) break; + if (!noneUnsealed) break; } } @@ -9699,7 +9699,7 @@ class MutatingScope zv::Val valueTypeB = pt_type_op(Z_OBJ_P(constantArraysB.raw()), PT_OP_GET_ITERABLE_VALUE_TYPE, 0, NULL); if (UNEXPECTED(valueTypeB.isUndef())) return zv::Val(); zv::Val resultKeyType, resultValueType; - if (bothSealed) { + if (noneUnsealed) { resultKeyType = unionTypes(keyTypeA.raw(), keyTypeB.raw()); if (UNEXPECTED(resultKeyType.isUndef())) return zv::Val(); resultValueType = unionTypes(valueTypeA.raw(), valueTypeB.raw()); From 4dc2c6ffa52c10531f4ba4d1f73250b96c9ec0a7 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Tue, 17 Nov 2026 09:49:51 +0000 Subject: [PATCH 2/3] Explain why shapes of unknown sealedness pass the generalizeType() unsealed check Co-Authored-By: Claude Opus 5.5 --- src/Analyser/MutatingScope.php | 5 ++++- turbo-ext/src/MutatingScope.cpp | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index e5b9911ffe9..cd74cbd215b 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -5289,7 +5289,10 @@ private function generalizeType(Type $a, Type $b, int $depth): Type // Neither input has unsealed extra keys, so their key sets // are finite — the constant keys of the shapes. That holds // for sealed shapes as well as for shapes of unknown - // sealedness (the only kind without bleeding edge). On the + // sealedness (the only kind without bleeding edge). The + // latter report `isUnsealed()` as maybe and are deliberately + // let through: they carry no extra key type, so only + // `isUnsealed()->yes()` (an explicit `...`) bails. On the // fall-through ArrayType path, recursing into // `generalizeType` would widen e.g. `0|1` to `int<0, max>` // — for both the keys and the values — losing the loop's diff --git a/turbo-ext/src/MutatingScope.cpp b/turbo-ext/src/MutatingScope.cpp index 706a7ce8f08..103c63505bb 100644 --- a/turbo-ext/src/MutatingScope.cpp +++ b/turbo-ext/src/MutatingScope.cpp @@ -9676,7 +9676,8 @@ class MutatingScope } else { /* Neither input has unsealed extra keys — see the twin's * comment: keep the literal union instead of widening the - * keys and values */ + * keys and values. Shapes of unknown sealedness (isUnsealed + * maybe) are deliberately let through; only yes bails */ bool noneUnsealed = true; for (uint32_t side = 0; side < 2 && noneUnsealed; side++) { for (auto checkEntry : zv::TableRef(constantArrays.table((int) side))) { From c110df0d68be309e4173753a0b9a4fc3421b0f30 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Tue, 17 Nov 2026 10:07:49 +0000 Subject: [PATCH 3/3] Rename LegacyArrayShapeTypeInferenceTest to Bug15749Test Co-Authored-By: Claude Opus 5.5 --- .../{LegacyArrayShapeTypeInferenceTest.php => Bug15749Test.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/PHPStan/Analyser/{LegacyArrayShapeTypeInferenceTest.php => Bug15749Test.php} (89%) diff --git a/tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php b/tests/PHPStan/Analyser/Bug15749Test.php similarity index 89% rename from tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php rename to tests/PHPStan/Analyser/Bug15749Test.php index 548778eeb9d..8c8421e76f6 100644 --- a/tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.php +++ b/tests/PHPStan/Analyser/Bug15749Test.php @@ -8,7 +8,7 @@ /** * Runs without bleeding edge, where constant array shapes have unknown sealedness. */ -class LegacyArrayShapeTypeInferenceTest extends TypeInferenceTestCase +class Bug15749Test extends TypeInferenceTestCase { public static function dataAsserts(): iterable