Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inferring template types on intersection types #1122

Merged

Conversation

rvanvelzen
Copy link
Contributor

@rvanvelzen rvanvelzen commented Mar 26, 2022

c877ec1 was incorrect in hindsight

Closes phpstan/phpstan#6904 and phpstan/phpstan#6917

@staabm
Copy link
Contributor

staabm commented Mar 27, 2022

as I noticed by coincidence - it doesn't need to be part of/fixed by this PR, but maybe its a related issue: phpstan/phpstan#5592 (comment)

just want to make you aware

@VincentLanglet
Copy link
Contributor

Thanks for your quick reaction

@rvanvelzen rvanvelzen force-pushed the fix-intersection-template-inference branch from b3fa91b to 5feb496 Compare March 27, 2022 16:03
c877ec1 was incorrect in hindsight. I'm not really happy about this solution.

The problem is that types like non-empty-string are represented by an intersection of string&non-empty-string, and when the inference is looking at these types it gets confused because non-empty-string on its own doesn't make much sense.

Closes phpstan/phpstan#6904 and phpstan/phpstan#6917
@ondrejmirtes ondrejmirtes force-pushed the fix-intersection-template-inference branch from 1e02708 to 247279f Compare March 28, 2022 08:52
@ondrejmirtes
Copy link
Member

The failures in CI are in Rector and Composer:

Composer (https://github.com/composer/composer/blob/3ae111140facdba8ae82adcd1085e4adfc7d715c/src/Composer/Command/RemoveCommand.php#L181-L189)

 ------ ----------------------------------------------------------------------- 
  Line   src/Composer/Command/RemoveCommand.php                                 
 ------ ----------------------------------------------------------------------- 
  181    Only booleans are allowed in &&, array<string|Stringable> given on     
         the right side.                                                        
  181    Parameter #2 $array of static method Composer\Pcre\Preg::grep()        
         expects array<string|Stringable>, array<int, int|string> given.        
  181    Unable to resolve the template type T in call to method static method  
         Composer\Pcre\Preg::grep()                                             
         💡 See:                                                                
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-temp  
         late-type                                                              
  189    Only booleans are allowed in &&, array<string|Stringable> given on     
         the right side.                                                        
  189    Parameter #2 $array of static method Composer\Pcre\Preg::grep()        
         expects array<string|Stringable>, array<int, int|string> given.        
  189    Unable to resolve the template type T in call to method static method  
         Composer\Pcre\Preg::grep()                                             
         💡 See:                                                                
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-temp  
         late-type                                                              
 ------ ----------------------------------------------------------------------- 

Rector (https://github.com/rectorphp/rector-src/blob/303ecc342da1484619c4f875ad67850feeb4b52b/rules/Php80/Rector/ClassMethod/OptionalParametersAfterRequiredRector.php#L146)


 ------ ----------------------------------------------------------------------- 
  Line   rules/Php80/Rector/ClassMethod/OptionalParametersAfterRequiredRector.  
         php                                                                    
 ------ ----------------------------------------------------------------------- 
  146    Property PhpParser\Node\Expr\New_::$args                               
         (array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>) does    
         not accept array<PhpParser\Node\Arg|PhpParser\Node\Param>.             
  146    Unable to resolve the template type T in call to method                
         Rector\Php80\NodeResolver\ArgumentSorter::sortArgsByExpectedParamOrde  
         r()                                                                    
         💡 See:                                                                
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-temp  
         late-type                                                              
  164    Unable to resolve the template type T in call to method                
         Rector\Php80\NodeResolver\ArgumentSorter::sortArgsByExpectedParamOrde  
         r()                                                                    
         💡 See:                                                                
         https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-temp  
         late-type                                                              
  173    Property PhpParser\Node\Expr\MethodCall::$args                         
         (array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>) does    
         not accept array<PhpParser\Node\Arg|PhpParser\Node\Param>.             
 ------ ----------------------------------------------------------------------- 

@ondrejmirtes
Copy link
Member

Can you please check what's going on in these errors and whether they are legit or whether they should be fixed in this PR? Feel free to reproduce them in phpstan-src test suite.

@rvanvelzen
Copy link
Contributor Author

The composer failure seems legit to me: https://phpstan.org/r/33fa6126-1af3-49b2-b505-922f22367f11

@ondrejmirtes ondrejmirtes merged commit 367a316 into phpstan:1.5.x Mar 28, 2022
@ondrejmirtes
Copy link
Member

Thank you, awesome 😊

@rvanvelzen
Copy link
Contributor Author

I was just about to say that the Rector one is a regression. Will send a PR once I've got a solution

@ondrejmirtes
Copy link
Member

It's fine, I'm not releasing it right away 😊 Thank you.

@rvanvelzen
Copy link
Contributor Author

Perhaps I was wrong. This seems to be the Rector case: https://phpstan.org/r/ca5584c3-6a43-4046-b1d2-b7514b9e699a

Before array<First|Second> would be resolved as array<Second> when passed to T of array<Second|Third>, but that no longer happens and you now get the error about incompatible types.

So if you agree I'll leave this as-is, and we'll hear about actual regressions when this is released.

@ondrejmirtes
Copy link
Member

Alright, I'm gonna tag 1.5.1 :)

@rvanvelzen rvanvelzen deleted the fix-intersection-template-inference branch April 25, 2022 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants