Fix false positive match.alwaysTrue with consecutive match(true) expressions#5296
Closed
gnutix wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix false positive match.alwaysTrue with consecutive match(true) expressions#5296gnutix wants to merge 1 commit intophpstan:2.1.xfrom
gnutix wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…cutive match(true) expressions After an exhaustive match(true) with a throwing default arm, the post-match scope carried conditional expressions encoding relationships between arm-narrowed types (e.g. "if equals(A) is false, then equals(B) is true"). When a subsequent match(true) filtered by falsey for the first arm, these conditional expressions activated and incorrectly narrowed the second arm's condition to true. The fix restores the pre-match conditional expressions after the arm body scope merge, preventing match-internal type relationships from leaking into the post-match scope.
Member
|
No fix is needed, this is expected behaviour. |
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.
Fixes phpstan/phpstan#14368
Summary
match(true)with a throwing default arm, the post-match scope carried conditional expressions encoding relationships between arm-narrowed types (e.g. "ifequals(A)is false, thenequals(B)is true")match(true)filtered by falsey for the first arm, these conditional expressions activated and incorrectly narrowed the second arm's condition totrue, causing a false positivematch.alwaysTrueRoot cause
PR #5066 (fix #9601) changed
MatchHandlerto exclude always-terminating arms (e.g.default => throw ...) from the arm body scope merge. This is correct for type resolution, but themergeWithcall also createsconditionalExpressionsthat encode relationships between the narrowed types of different arms. These conditional expressions survived into the post-match scope and were activated by subsequent code that narrowed the same expressions.