Preserve callable, Closure, class-string and float-literal template bounds instead of widening them to mixed - #6483
Open
phpstan-bot wants to merge 1 commit into
Conversation
…late bounds instead of widening them to `mixed` * Add `TemplateCallableType`, `TemplateClosureType`, `TemplateClassStringType`, `TemplateGenericClassStringType` and `TemplateConstantFloatType`, each rebuilding its parent type from the bound so the template behaves like the bound it declares. * Register the new classes in `TemplateTypeFactory::create()`, which previously fell through to `TemplateMixedType` with a `mixed` bound for these types. * Derive the `generics.notSupportedBound` check in `TemplateTypeCheck` from `TemplateTypeFactory` instead of a hand-maintained list of bound classes. The two lists had drifted apart, so `int<0, 10>`, `true`/`false` and enum-case bounds were reported as unsupported even though the factory already preserved them. * Adjacent bounds probed and left unsupported on purpose: `void`, `never` (a template bounded by them can hold no value) and `resource` (PHPStan cannot tell resources apart, so refining one adds nothing). They are now covered by the rule test.
Member
|
Personally I wouldn't tackle this feature request now, seems like a crazy edge case. |
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
Callable signature types could not be used as
@templatebounds.@template F of Closure(int): int,@template F of callableand@template F of callable(int): intwere reported asgenerics.notSupportedBound, the template silently degraded tomixed(which then triggeredparameter.phpDocType/return.phpDocTypefollow-up errors against the nativeClosure/callabletypehints), and the concrete closure type passed at the call site was lost.The same silent widening affected
class-string,class-string<T>and float-literal bounds, and the rule that reports unsupported bounds had drifted out of sync with the factory that implements them.Changes
New
Template*Typeclasses, each reconstructing its parent type from the bound (src/Type/Generic/):TemplateClosureType—@template T of Closure(int): stringTemplateCallableType—@template T of callable,@template T of callable(int): stringTemplateClassStringType—@template T of class-stringTemplateGenericClassStringType—@template T of class-string<Exception>TemplateConstantFloatType—@template T of 1.5(completes the constant-scalar family next toTemplateConstantIntegerType/TemplateConstantStringType)src/Type/Generic/TemplateTypeFactory.php: register the five new classes so the bound is preserved instead of falling through toTemplateMixedTypewith amixedbound.src/Rules/Generics/TemplateTypeCheck.php: replace the hand-maintained list of supported bound classes with a question toTemplateTypeFactory— reportgenerics.notSupportedBoundexactly when the factory did not keep the declared bound.phpstan-baseline.neon: regenerated for the newinstanceof/toPhpDocNodeentries that mirror the existingTemplate*Typeones, minus the entry that theTemplateTypeCheckrewrite made obsolete.Analogous cases fixed by the rule/factory sync (the factory already preserved these bounds, only the rule falsely reported them):
@template T of int<0, 10>(IntegerRangeType)@template T of true/@template T of false(ConstantBooleanType)@template T of Suit::Hearts(EnumCaseObjectTypeand any otherObjectTypesubclass)Probed and deliberately left unsupported:
void,never(a template bounded by either can hold no value) andresource(PHPStan does not distinguish resources, so the refinement carries no information).@template T of statickeeps failing earlier withclass.notFound, which is a separate concern.Root cause
TemplateTypeFactory::create()dispatches on the bound's class to pick aTemplate*Typeimplementation, and ends with a catch-all that returnsnew TemplateMixedType(..., new MixedType(true)). Any bound without a dedicatedTemplate*class therefore lost its meaning entirely: inference against the bound could no longer succeed, so the template resolved to nothing and the caller fell back to the native return typehint.TemplateTypeCheckwarned about this with a second, hand-written list of accepted bound classes. Because the two lists were maintained independently, they disagreed in both directions: bounds the factory handled fine (int<0, 10>,true, enum cases) were reported as unsupported, while the list gave no protection against the factory gaining or losing support. The check now derives its answer from the factory, so the rule and the implementation cannot drift again.Test
tests/PHPStan/Analyser/nsrt/bug-15273.php— the issue's playground sample verbatim; all four calls must inferstatic-Closure(int): int. Fails before the fix on three of the four assertions (Closure,callable(): mixed,callable(): mixed).tests/PHPStan/Analyser/nsrt/template-bound-types.php— inference throughclass-string,class-string<Exception>,int<0, 10>and1.5bounds. Fails before the fix on three of the four assertions.tests/PHPStan/Rules/PhpDoc/data/bug-15273.php+IncompatiblePhpDocTypeRuleTest::testBug15273()— the@param/@return"is not subtype of native type" follow-up errors. Eight errors before the fix, none after.tests/PHPStan/Rules/Generics/data/function-template.php+FunctionTemplateTypeRuleTest::testRule()— every newly supported bound plus the ones that stay unsupported. Ninegenerics.notSupportedBoundfalse positives before the fix; onlyvoidandneverremain.make tests,make phpstanandmake csare green.make name-collisionfails on the pre-existing unparseable PHP 8.5 data files (*-pipe.php) both with and without this change; running the detector with those excluded reports no collisions.Fixes phpstan/phpstan#15273