Repository navigation
Keep the literal key/value union when generalizing constant array shapes of unknown sealedness in loops #7095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VincentLanglet
merged 3 commits into
phpstan:2.3.x
from
phpstan-bot:create-pull-request/patch-cf8hs97
Nov 17, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Analyser; | ||
|
|
||
| use PHPStan\Testing\TypeInferenceTestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| /** | ||
| * Runs without bleeding edge, where constant array shapes have unknown sealedness. | ||
| */ | ||
| class Bug15749Test extends TypeInferenceTestCase | ||
| { | ||
|
|
||
| public static function dataAsserts(): iterable | ||
| { | ||
| yield from self::gatherAssertTypes(__DIR__ . '/data/bug-15749.php'); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed ...$args | ||
| */ | ||
| #[DataProvider('dataAsserts')] | ||
| public function testAsserts( | ||
| string $assertType, | ||
| string $file, | ||
| ...$args, | ||
| ): void | ||
| { | ||
| $this->assertFileAsserts($assertType, $file, ...$args); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15749; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @return array<int<0, 3>, int> */ | ||
| function foo(): array { | ||
| $a = [ ]; | ||
| for ($i = 0; $i <= 3; $i++) { | ||
| $a[$i] = random_int(0, 100); | ||
| } | ||
| assertType('non-empty-array<int<0, 3>, int<0, 100>>', $a); | ||
| return $a; | ||
| } | ||
|
|
||
| function lessThan(): void { | ||
| $arr = []; | ||
| for ($i = 0; $i < 5; $i++) { | ||
| $arr[$i] = 'v'; | ||
| } | ||
| assertType("non-empty-array<int<0, 4>, 'v'>", $arr); | ||
| } | ||
|
|
||
| function withCondition(): void { | ||
| $arr = []; | ||
| for ($i = 0; $i < 5; $i++) { | ||
| if (rand()) { | ||
| $arr[$i] = 'v'; | ||
| } | ||
| } | ||
| assertType("array<int<0, 4>, '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<int<0, 3>, 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<int<0, 3>, 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<int<0, 2>, non-empty-array<int<0, 2>, 1>>', $a); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can one of the constant arrays be "maybe unsealed" at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:$this->unsealed === null). That is the legacy shape, and the only kind that exists without bleeding edge....<K, V>.The old check,
!isSealed()->yes(), also bailed on maybe, becauseisSealed()is justisUnsealed()->negate(). So every legacy shape was widened toint<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 inturbo-ext/src/MutatingScope.cpp. Only comments changed, no behaviour.make tests(23,868 tests, 116 skipped) andmake phpstanboth pass. The change is in its own commit, 4dc2c6f, and has not been pushed.