Repository navigation
Keep the narrowing origin when generalizing loop variable holders - #7305
Merged
Merged
Conversation
generalizeVariableTypeHolders() built the widened holder without narrowedFrom, so the merge after a loop took a value the loop only narrowed, like a mixed iterated with foreach, for a value of its own and kept it as the witness type. The holder now keeps the origin both passes were narrowed from, by the same rule ExpressionTypeHolder::and() uses. Amp-Thread-ID: https://ampcode.com/threads/T-01a23bcd-b5ed-748d-aae2-43defeba4aba
ondrejmirtes
force-pushed
the
keep-narrowed-from-when-generalizing
branch
from
December 3, 2026 13:38
04d59b3 to
accd486
Compare
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.
MutatingScope::generalizeVariableTypeHolders()built the widened holder withoutnarrowedFrom. The merge after a loop then took a value the loop only narrowed (amixediterated withforeachwhose body assigns something) for a value of its own and kept it as the witness type,mixed+(array{}|object), socount($items)after the loop was reported.The widened holder now comes from a new
ExpressionTypeHolder::generalize(), which keeps the origin both passes were narrowed from, by the same ruleExpressionTypeHolder::and()uses. The turbo extension gets the same change (pt_holder_generalize(), used by the nativegeneralizeVariableTypeHolders()and the nativegeneralize()), with differential coverage insmoke.php.Closes phpstan/phpstan#15942
Amp thread: https://ampcode.com/threads/T-01a23bcd-b5ed-748d-aae2-43defeba4aba