Repository navigation
Conversation
TypeCombinator::widen() degrades array shapes to an oversized general array in two cases. The types hold more constant array values than ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT, or a union holds more shapes than ArrayWidening::UNION_SHAPES_LIMIT. 96f80ac (phpstan#7306) added the second case. OversizedArrayWidening generalized each constant scalar value in the two cases. This is a regression in 2.4.0 from 96f80ac (phpstan#7306). A list that gets 17 conditional appends of literal strings became non-empty-list<literal-string&non-falsy-string>&oversized-array. PHPStan 2.3.7 keeps the literal values. The shape limit bounds the number of shapes. It does not need to forget the values, because ARRAY_COUNT_LIMIT bounds their number already. OversizedArrayWidening now keeps the constant scalar values when the types hold at most ARRAY_COUNT_LIMIT constant array values. Above this limit it generalizes them as before. The keys and the oversized-array accessory do not change. The list is now non-empty-list<'a'|'b'|...>&oversized-array.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Refs phpstan/phpstan#15951
This is a regression in 2.4.0 from 96f80ac (#7306). A list that code appends to in 16 or more branches loses its literal values. PHPStan 2.3.7 keeps the literal values.
Cause
TypeCombinator::widen()degrades array shapes to an oversized general array in two cases:ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT.ArrayWidening::UNION_SHAPES_LIMIT. Limit array shapes in unions and constant-array foreach unrolling in loop passes #7306 added this case. A list with optional keys counts one time for each length that it can have, so a list with 16 optional keys is over the limit.OversizedArrayWideninggeneralizes each constant scalar value in the two cases. In the second case the shapes hold only a small number of values. The shape limit bounds the number of shapes, andARRAY_COUNT_LIMITbounds the number of values already.Change
OversizedArrayWideningkeeps the constant scalar values when the types hold at mostARRAY_COUNT_LIMITconstant array values. Above this limit it generalizes the values as before.oversized-arrayaccessory do not change. The list in the example is nowarray{}|(non-empty-list<'a'|'b'|…|'q'>&oversized-array).turbo-ext.Issue 15951
The snippet of the issue no longer reports the
argument.typeerror, because$picked[0]is(1|2|…|16).The type after the
ifisarray{}|(non-empty-list<1|2|…|16>&oversized-array). The issue expectsarray{}|list{0: 1|…|16, 1?: …}. This PR does not change when the widening starts. The merge still counts the list that the narrowing splits as 17 shapes. That part of the issue stays open.Cost
#7306 widens these unions because the exact shapes have a quadratic cost. This PR keeps that widening. The kept values are one union of constants in one general array. Unconditional appends give the same form today:
$b[] = 0; … $b[] = 999;givesnon-empty-list<0|1|…|999>&oversized-array.Each later conditional append adds one constant to that union. The analysis of many conditional appends of different constants becomes more expensive.
The Benchmarks workflow of this PR, compared with the workflow of the base commit 65b9ac9 (497 benchmarks):
AnalysisBench::benchAnalyse@conditional-list-appends.php(80 appends)AnalysisBench::benchAnalyse@bug-7581.phpLocal runs of larger generated files show how the cost increases. The table shows the minimum user CPU time and the median peak RSS of the full process. The command is
bin/phpstan analyse -c tests/bench/phpstan.neon --debug <file>on PHP 8.5.13 without the turbo extension. The runs before and after the change are interleaved (8 runs for each state, 3 runs for the last row). A run takes approximately 0.5 s before the analysis of the file starts.Tests
tests/PHPStan/Analyser/nsrt/widened-shapes-keep-values.phphas three cases: a list with one append in each of 17 branches, a list with several appends in each of 6 branches, and 17 shapes with different keys.tests/PHPStan/Analyser/nsrt/bug-15951.phpis the snippet of the issue.CallToFunctionParametersRuleTest::testWidenedShapesKeepValuesandtestBug15951analyse the two files and expect no error.OversizedArrayWidening.phpand pass with it.make phpstanand the coding standard pass locally on PHP 8.5 without the turbo extension.CI
2.4.x. The Larastan, phpstan-dba, phpstan-laravel, Rector and Symplify test jobs fail on other PRs of the same day, for example Check unreachable return statements when fixing a missing return type #7323.Integration - rectorphp/rector-srcpasses with this PR. On the other PRs it fails with the error of the issue atRectorConfigBuilder.php:599.— Claude