Repository navigation
Tie variables destructured from a union of array shapes to each other - #7260
Merged
Merged
Conversation
['kind' => $kind, 'payload' => $payload] = $message reads both variables from the same member of an array-shape union, so narrowing $kind to one member's value now narrows $payload to that member's type, and the other way round. A variable guards the others only when its types in the members are equal or share no value. The native AssignHandler gets the same change. Amp-Thread-ID: https://ampcode.com/threads/T-01a22b9b-2b0a-7521-a915-75eebb3a519c
merge() returns array{Type, null}|array{null, Type}, so once $a is null the
destructured $b is a Type. Also regenerates the AssignHandler declarations
for the new helper and adds the issue-bot reproducer of #12487, which the
destructuring narrowing fixes.
Amp-Thread-ID: https://ampcode.com/threads/T-01a22b9b-2b0a-7521-a915-75eebb3a519c
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.
Destructuring a union of array shapes now ties the destructured variables to each other by the member they came from, so checking the discriminant narrows the other variables:
The same works for positional tuples (
[$tag, $value] = $pair) and in reverse (is_int($value)narrows$tag). A variable guards the others only when its types in the members are equal or share no value, so overlapping discriminants narrow nothing. Members and variables are capped byDESTRUCTURED_MEMBERS_LIMIT/DESTRUCTURED_VARIABLES_LIMIT. The nativeAssignHandlerin turbo-ext gets the same change; walk-trace is identical over the default corpus and the full suite passes with and without the extension.Closes phpstan/phpstan#15893
Closes phpstan/phpstan#12487
Amp thread: https://ampcode.com/threads/T-01a22b9b-2b0a-7521-a915-75eebb3a519c