Repository navigation
Narrow a mixed subject of a loose comparison to mixed~null - #7061
Merged
Merged
Conversation
When the values that can loosely equal the other side include objects, a mixed subject stays mixed, but it still cannot be null when null does not equal the value. Narrowing it to mixed~null again marks the offset and the variable as set, which an early return without specified types had stopped doing. Closes phpstan/phpstan#15725 Amp-Thread-ID: https://ampcode.com/threads/T-01a1d722-2ae4-761a-bc23-3426a9af3740
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.
Closes phpstan/phpstan#15725
Since 9d55d96 ("Narrow loose comparisons by the values each side can loosely equal"), the true branch of
$subject == <value>returns no specified types for amixedsubject when objects such asStringableorGMPcan loosely equal the value. That keeps the subjectmixed, as intended, but it also skips whatcreateSubjectTypes()does for the expression's parents:hasOffsetValue()on the array, the removal offalsefromarray|false, and certainty for a possibly undefined variable.The subject now still loses
nullwhennullcannot loosely equal the value: it becomesmixed~null(T of mixed~nullfor a template), so the offset and the variable count as set again. Whennullcan equal the value (== 0,== ''), nothing is narrowed, as before.The
turbo-extmirror inIdenticalNarrowingHelper.cppgets the same change.Verification:
tests/PHPStan/Analyser/nsrt/bug-15725.phpandDefinedVariableRuleTest::testBug15725()fail without the fix and pass with it, with and without the extension loaded.mixedinloose-equality-narrowing.phpandloose-equality-narrowing-php7.phpbecomemixed~null.smoke.phpandsignature-parity.php, and byte-identical self-analysis output with the extension on and off.