Repository navigation
Keep the literal key/value union when generalizing constant array shapes of unknown sealedness in loops - #7095
Conversation
…pes 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.
| foreach ($constantArrayCheck->getConstantArrays() as $constantArrayInstance) { | ||
| if (!$constantArrayInstance->isSealed()->yes()) { | ||
| $bothSealed = false; | ||
| if ($constantArrayInstance->isUnsealed()->yes()) { |
There was a problem hiding this comment.
can one of the constant arrays be "maybe unsealed" at this point?
There was a problem hiding this comment.
Yes, a shape can be "maybe unsealed" at this point, and that is the case this fix is meant to let through.
ConstantArrayType::isUnsealed() (src/Type/Constant/ConstantArrayType.php:226) gives three results:
- maybe when the shape has unknown sealedness (
$this->unsealed === null). That is the legacy shape, and the only kind that exists without bleeding edge. - yes when the shape is definitely unsealed: it has extra keys, such as an explicit
...<K, V>. - no when the shape is sealed.
The old check, !isSealed()->yes(), also bailed on maybe, because isSealed() is just isUnsealed()->negate(). So every legacy shape was widened to int<0, max>, which is the bug reported in phpstan/phpstan#15749.
A maybe-unsealed shape has no extra key type, so its keys are only its constant keys and the set is finite, just like a sealed shape. Keeping the literal key union is safe for it. Only a definitely unsealed shape can hold keys outside that union, which is why the check now bails only on isUnsealed()->yes().
I added this explanation to the comment in MutatingScope::generalizeType() and to the same spot in the C++ mirror in turbo-ext/src/MutatingScope.cpp. Only comments changed, no behaviour. make tests (23,868 tests, 116 skipped) and make phpstan both pass. The change is in its own commit, 4dc2c6f, and has not been pushed.
…sealed check Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| /** | ||
| * Runs without bleeding edge, where constant array shapes have unknown sealedness. | ||
| */ | ||
| class LegacyArrayShapeTypeInferenceTest extends TypeInferenceTestCase |
There was a problem hiding this comment.
rename this class to Bug15749Test
There was a problem hiding this comment.
I renamed the test class from LegacyArrayShapeTypeInferenceTest to Bug15749Test, and the file to tests/PHPStan/Analyser/Bug15749Test.php. Nothing else in the repo referred to the old name. The class still runs without bleeding edge and uses the same data file, tests/PHPStan/Analyser/data/bug-15749.php.
make tests passes (23,868 tests, 116 skipped), and make phpstan reports no errors. The rename is in its own commit, c110df0, and has not been pushed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Without bleeding edge, an array filled in a bounded loop lost its key bound:
With bleeding edge the result was already precise. This change gives the same result without it.
Changes
src/Analyser/MutatingScope.php(generalizeType()): when two constant array shapes with different key sets are merged into a generalArrayType, keep the literal key/value union unless a shape is definitely unsealed (isUnsealed()->yes()). Before, this neededisSealed()->yes().turbo-ext/src/MutatingScope.cpp: same change in the native mirror. It was checked by reverting only the PHP side and running the new test with the extension loaded (it passes), andtests/smoke.phpprints ALL OK.tests/PHPStan/Analyser/ScopeTest.php: the legacy-shape rows oftestGeneralizenow give the same result as sealed shapes. For example,array{a: 1} + array{a: 1, b: 2}givesnon-empty-array<'a'|'b', 1|2>instead ofnon-empty-array<literal-string&lowercase-string&non-falsy-string, int<1, max>>.Analogous cases probed (all run without bleeding edge):
forwith<, decrementingfor, conditional assignment in the body, an array that starts with a string key, nested loops, andwhileloops.foreachoverrange()and over constant string lists.do-whilegivesint<0, max>both with and without bleeding edge, and also in 2.1.x. The body runs before the condition narrows the counter, so this is a separate limitation. It is not covered here.Root cause
Since 2.2,
$a[0|1] = …onarray{}|array{0: …}produces the shapearray{0?: …, 1?: …}instead of the generalarray<0|1, …>. The loop fixed point therefore takes the constant-array branch ofgeneralizeType(). That branch keeps the literal key union only for sealed shapes and widened0+0|1toint<0, max>for anything else. Without bleeding edge every shape has unknown sealedness, so the bound was always lost. A shape of unknown sealedness has no extra key type, so its key set is finite (its constant keys), the same as a sealed shape. Only definitely unsealed shapes (...<K, V>) need the widening. Termination still holds: it relies on the counter's own generalization and the shape size limit, as on the sealed path. The issue says "since 2.3.6", but the regression actually appears in released PHARs from 2.2.0 on; 2.1.40 was precise.Test
tests/PHPStan/Analyser/LegacyArrayShapeTypeInferenceTest.phpwithtests/PHPStan/Analyser/data/bug-15749.php. It is aTypeInferenceTestCasewithout bleeding edge, because the nsrt tests always enable it and so could not catch this. It has the issue's reproducer verbatim plus the probes listed above: 7 of its 9 assertions fail without the fix.ScopeTest::testGeneralizeexpectations updated as described above.make phpstan, andmake cs-fixpass.make name-collisionaborts on a parse error in an unrelated existing file (readonly-property-assign-clone-with.php, PHP 8.5 syntax); the new namespaceBug15749is unique.tests/bench/datafiles analysed without bleeding edge produce identical output and timing before and after the change.Fixes phpstan/phpstan#15749