Fix phpstan/phpstan#13453: Templated return type error message is self-contradictory#5352
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#13453: Templated return type error message is self-contradictory#5352phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…iptions in error messages - When two different template types produce the same description at the chosen verbosity level, escalate to precise verbosity to include scope information - This fixes self-contradictory messages like "should return T of ResultA but returns T of ResultA" by showing "T of ResultA (function run(), argument) but returns T of ResultA (class I, parameter)" - New regression test in tests/PHPStan/Rules/Functions/data/bug-13453.php
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 two different template types have the same name and bound but belong to different scopes (e.g., a function template
T of ResultAand an interface templateT of ResultA), error messages like "should return T of ResultA but returns T of ResultA" were self-contradictory and confusing to users. The fix escalates the verbosity level toprecisewhen both types produce identical descriptions, adding scope information to disambiguate them.Changes
VerbosityLevel::getRecommendedLevelByType()insrc/Type/VerbosityLevel.phpto detect when accepting and accepted types have identical descriptions at the chosen level, and escalate topreciseverbosity in that casetests/PHPStan/Rules/Functions/data/bug-13453.phpand corresponding test method intests/PHPStan/Rules/Functions/ReturnTypeRuleTest.phpRoot cause
VerbosityLevel::getRecommendedLevelByType()chose verbosity based on type complexity (constant values, callables, accessory types, etc.) but never considered whether the two types being compared would produce identical string descriptions. Template types from different scopes with the same name and bound are semantically different but described identically attypeOnlyandvalueverbosity levels. Only atpreciselevel does the scope information (e.g., "function run(), argument" vs "class I, parameter") get included.Test
The regression test reproduces the exact scenario from the issue: a function with
@template T of ResultAreturning a value whose type isT of ResultAfrom an interface template. The test verifies the error message now includes scope disambiguation.Fixes phpstan/phpstan#13453