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

Polymorphic anonymous functions #10685

Open
klimick opened this issue Mar 7, 2024 · 4 comments
Open

Polymorphic anonymous functions #10685

klimick opened this issue Mar 7, 2024 · 4 comments
Labels
Milestone

Comments

@klimick
Copy link

klimick commented Mar 7, 2024

Bug report

The new feature for callable type from 1.10.60 release does not work well.

Most easiest case of usage:
https://phpstan.org/r/870d442a-918b-4ea8-8181-afe38d8faae2

Type representation dumps incorrectly:
https://phpstan.org/r/294502a4-683d-4dc7-a7f5-24b07291ec22

Other (can be mind blowing):
https://phpstan.org/r/0efe72eb-b394-4de7-b2d5-a04f59fe6667
https://phpstan.org/r/15a337f8-fdcc-484c-9db0-766ed55740b0

Code snippet that reproduces the problem

No response

Expected output

I tried to describe it in examples

Did PHPStan help you today? Did it make you happy in any way?

No response

@mad-briller
Copy link
Contributor

mad-briller commented Mar 8, 2024

in your flip-zip example, are you sure that the expected output is list<array{1, 2}> and not list<array{2, 1}> ?

it's hard to tell without the code bodies, but assuming the parameters become flipped, the result should be backwards right?
https://3v4l.org/KtRie

@klimick
Copy link
Author

klimick commented Mar 8, 2024

in your flip-zip example, are you sure that the expected output is list<array{1, 2}> and not list<array{2, 1}> ?

You right. Expected output should be list<array{2, 1}>.
It seems I was in a hurry with the issue writing. Sorry.
I checked it with TypeScript. Link to playground.

it's hard to tell without the code bodies, but assuming the parameters become flipped, the result should be backwards right?

Right. You guessed what the functions do.
It's classic from functional programming.

@phpstan-bot
Copy link
Contributor

@klimick After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1 – 8.3 (1 error)
+PHP 8.1 – 8.3 (2 errors)
 ==========
 
-26: Dumped type: Closure(A): A
+26: Dumped type: Closure<A of mixed>(A): A
+28: Call to function acceptsPoly() on a separate line has no effect.
 
-PHP 7.2 – 8.0 (2 errors)
+PHP 7.2 – 8.0 (3 errors)
 ==========
 
 22: First-class callables are supported only on PHP 8.1 and later.
-26: Dumped type: Closure(A): A
+26: Dumped type: Closure<A of mixed>(A): A
+28: Call to function acceptsPoly() on a separate line has no effect.
Full report

PHP 8.1 – 8.3 (2 errors)

Line Error
26 Dumped type: Closure<A of mixed>(A): A
28 Call to function acceptsPoly() on a separate line has no effect.

PHP 7.2 – 8.0 (3 errors)

Line Error
22 First-class callables are supported only on PHP 8.1 and later.
26 Dumped type: Closure<A of mixed>(A): A
28 Call to function acceptsPoly() on a separate line has no effect.

@phpstan-bot
Copy link
Contributor

@klimick After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
 
 32: Dumped type: callable(list<B>, list<A>): list<array{A, B}>
 36: Dumped type: list<array{2, 2}>
-37: Parameter #1 $ of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.
+37: Parameter #1 of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.
 
 PHP 7.2 – 8.0 (4 errors)
 ==========
@@ @@
 29: First-class callables are supported only on PHP 8.1 and later.
 32: Dumped type: callable(list<B>, list<A>): list<array{A, B}>
 36: Dumped type: list<array{2, 2}>
-37: Parameter #1 $ of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.
+37: Parameter #1 of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.
Full report

PHP 8.1 – 8.3 (3 errors)

Line Error
32 Dumped type: callable(list<B>, list<A>): list<array{A, B}>
36 Dumped type: list<array{2, 2}>
37 Parameter #1 of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.

PHP 7.2 – 8.0 (4 errors)

Line Error
29 First-class callables are supported only on PHP 8.1 and later.
32 Dumped type: callable(list<B>, list<A>): list<array{A, B}>
36 Dumped type: list<array{2, 2}>
37 Parameter #1 of callable callable(list<B>, list<A>): list<array{A, B}> expects list<2>, array{1} given.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants