Fix #9634: Cannot use "T is never" in conditional return#5082
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #9634: Cannot use "T is never" in conditional return#5082phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added TemplateType handling in NonAcceptingNeverType::isSuperTypeOf() and NeverType::isSuperTypeOf() to return Maybe instead of No, since a template type can be instantiated to never - New regression tests in tests/PHPStan/Rules/PhpDoc/data/bug-9634.php and tests/PHPStan/Analyser/nsrt/bug-9634.php Closes phpstan/phpstan#9634
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.
Summary
When using
(T is never ? false : bool)as a conditional return type on a generic interface method, PHPStan incorrectly reported "Condition 'T is never' in conditional return type is always false." The fix allowsneverto correctly recognize that a template typeTmight be instantiated tonever.Changes
TemplateTypehandling insrc/Type/NonAcceptingNeverType.php—isSuperTypeOf()now returnsMaybefor template types instead ofNoTemplateTypehandling insrc/Type/NeverType.phpfor consistencytests/PHPStan/Rules/PhpDoc/data/bug-9634.phpand test method intests/PHPStan/Rules/PhpDoc/MethodConditionalReturnTypeRuleTest.phptests/PHPStan/Analyser/nsrt/bug-9634.phpverifying thatOption<never>::isSome()resolves tofalseandOption<int>::isSome()resolves toboolRoot cause
NonAcceptingNeverType::isSuperTypeOf()(the type used for explicitly writtenneverin PHPDoc) returnedNofor all types exceptNeverTypeandNonAcceptingNeverType. WhenConditionalReturnTypeRuleHelperchecked whether the conditionT is nevercould ever be true by calling$targetType->isSuperTypeOf($subjectType), thenevertarget returnedNofor the template typeT, causing the rule to report the condition as "always false". However, a template typeTcan be instantiated tonever(sinceneveris a subtype of any bound), so the correct answer isMaybe.Test
(T is never ? false : bool)no longer produces a false positive errorTis bound tonever, the conditional resolves tofalse, and whenTis bound toint, it resolves toboolFixes phpstan/phpstan#9634