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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 LegacyArrayShapeTypeInferenceTest extends TypeInferenceTestCase | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename this class to
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed the test class from
|
||
| { | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| } | ||
| 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); | ||
| } |
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.