Bail to MixedType in deep dim fetch writes on oversized arrays#5681
Open
phpstan-bot wants to merge 1 commit into
Open
Bail to MixedType in deep dim fetch writes on oversized arrays#5681phpstan-bot wants to merge 1 commit into
phpstan-bot wants to merge 1 commit into
Conversation
When produceArrayDimFetchAssignValueToWrite walks down through nested ArrayDimFetch nodes, each level calls getOffsetValueType on the current type. For deeply nested structures (6+ levels) where the type has already been marked as an oversized array (precision intentionally lost), these cascading operations produce exponential cost without useful precision gain. Stop drilling deeper when the dim fetch depth exceeds 5 AND the current offsetValueType is already an oversized array, substituting MixedType instead. This prevents expensive cascading intersectTypes/union operations at levels where precision cannot be maintained anyway. Closes phpstan/phpstan#14624
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.
Summary
isset()checks in while loopsMixedTypeinproduceArrayDimFetchAssignValueToWritewhen dim fetch depth exceeds 5 and the type is already an oversized array (precision intentionally lost)Detail
The root cause is in the DOWN walk of
produceArrayDimFetchAssignValueToWrite. For code that builds deeply nested arrays (6+ levels of$out[$a][$b][$c][$d][$e][$f]), each level callsgetOffsetValueTypeon the current type. After generalization marks the type as oversized (viaOversizedArrayTypein an intersection), continued drilling produces cascadingintersectTypesandTypeCombinator::unionoperations that grow exponentially — without any precision gain, since oversized arrays already returnMixedTypefromgetOffsetValueType.The fix recognizes this futility: when depth > 5 AND the current type is already oversized, substitute
MixedTypeimmediately instead of performing expensive type operations that will ultimately yieldMixedTypeanyway.Test plan
tests/bench/data/bug-14624.phpmake phpstanpasses with no errorsFixes phpstan/phpstan#14624
🤖 Generated with Claude Code