Repository navigation
Do not keep narrowed mixed returned from a branch as its witness type - #7304
Merged
Merged
Conversation
A ternary, a match arm or an assignment in a branch that returned a value narrowed from mixed handed the narrowed type to joinValues() as if a path produced it: `is_array($v) ? $v : $v` became mixed+array and failed a string parameter with checkMixedWitnessTypes. TernaryHandler and MatchHandler now read the type each branch value was narrowed from (MutatingScope::getNarrowedFromType()) and join through TypeCombinator::joinNarrowedValues(), where a value narrowed from mixed brings only the part of that mixed's own witness it still allows. An assignment of a narrowed value keeps its origin in the new holder (MutatingScope::assignNarrowedFrom()), so a scope merge of `$x = $v` in both branches does not keep it either. The native extension mirrors it. Amp-Thread-ID: https://ampcode.com/threads/T-01a23bcd-cba2-71f9-9bd1-dcc3a51bd5c6
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.
A ternary, a
matcharm or an assignment in a branch that returned a value narrowed frommixedhanded the narrowed type tojoinValues()as if a path produced it, sois_array($v) ? $v : $vbecamemixed+arrayand was reported when passed to astringparameter (withcheckMixedWitnessTypes).TernaryHandlerandMatchHandlerread the type each branch value was narrowed from (MutatingScope::getNarrowedFromType()) and join through the newTypeCombinator::joinNarrowedValues(): a value narrowed frommixedbrings only the part of that mixed's own witness it still allows.$x = $vkeeps$v's narrowing origin in$x's holder (MutatingScope::assignNarrowedFrom()), so the scope merge afterif (is_array($v)) { $x = $v; } else { $x = $v; }does not keep it either.$c ? $mixed : 1,$mixed ?? null.Closes phpstan/phpstan#15943
Amp thread: https://ampcode.com/threads/T-01a23bcd-cba2-71f9-9bd1-dcc3a51bd5c6