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

Callable parameter types aren't correctly inferred #3818

Open
nreynis opened this issue Sep 2, 2020 · 11 comments
Open

Callable parameter types aren't correctly inferred #3818

nreynis opened this issue Sep 2, 2020 · 11 comments

Comments

@nreynis
Copy link

nreynis commented Sep 2, 2020

Bug report

Types applied to callable doesn't seems to be correctly inferred.

Code snippet that reproduces the problem

https://phpstan.org/r/24105e02-8d5f-4fd9-a237-01ebec9938a9

Expected output

Both lines 29 and 34 should raise Else branch is unreachable because ternary operator condition is always true., only line 29 does.

Line 34 error reveals that the callable is inferred to callable(mixed): DateTimeImmutable generic should have been propagated to the callable and it should expect callable(T of DateTime): DateTimeImmutable

@ondrejmirtes ondrejmirtes added this to the Generics milestone Sep 2, 2020
@nreynis
Copy link
Author

nreynis commented Sep 2, 2020

Return types seems to have the same limitation:
https://phpstan.org/r/477de7ac-8952-4ab4-9aec-3c31e05027e5

@phpstan-bot
Copy link
Contributor

@nreynis PHPStan now reports different result with your code snippet:

@@ @@
 ==========
 
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

@phpstan-bot
Copy link
Contributor

@nreynis After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
 ==========
 
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(T): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(T): DateTimeImmutable, Closure(mixed): DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

@phpstan-bot
Copy link
Contributor

@nreynis After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (2 errors)
+PHP 7.4 – 8.0 (3 errors)
 ==========
 
+15: Property Test::$generator is never read, only written.
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (3 errors)

Line Error
15 Property Test::$generator is never read, only written.
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

@phpstan-bot
Copy link
Contributor

@nreynis After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (2 errors)
 ==========
 
+16: Property Test::$generator is never read, only written.
 30: Else branch is unreachable because ternary operator condition is always true.
 
 PHP 7.1 – 7.3 (4 errors)
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
16 Property Test::$generator is never read, only written.
30 Else branch is unreachable because ternary operator condition is always true.

PHP 7.1 – 7.3 (4 errors)

Line Error
30 Syntax error, unexpected T_VARIABLE, expecting ')' on line 30
31 Syntax error, unexpected ')' on line 31
36 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 36
37 Syntax error, unexpected ')' on line 37

@phpstan-bot
Copy link
Contributor

@nreynis After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (2 errors)
+PHP 7.4 – 8.0 (3 errors)
 ==========
 
+15: Property Test::$generator is never read, only written.
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (3 errors)

Line Error
15 Property Test::$generator is never read, only written.
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

@phpstan-bot
Copy link
Contributor

@nreynis After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 7.4 – 8.0 (1 error)
+PHP 7.4 – 8.0 (2 errors)
 ==========
 
+16: Property Test::$generator is never read, only written.
 30: Else branch is unreachable because ternary operator condition is always true.
 
 PHP 7.1 – 7.3 (4 errors)
Full report

PHP 7.4 – 8.0 (2 errors)

Line Error
16 Property Test::$generator is never read, only written.
30 Else branch is unreachable because ternary operator condition is always true.

PHP 7.1 – 7.3 (4 errors)

Line Error
30 Syntax error, unexpected T_VARIABLE, expecting ')' on line 30
31 Syntax error, unexpected ')' on line 31
36 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 36
37 Syntax error, unexpected ')' on line 37

@Einenlum
Copy link

Not sure if it's the exact same issue, but I found something that looks similar.

https://phpstan.org/r/525c36eb-c861-45bf-85ca-0315555e923f

Not sure how to fix this.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 7.4 – 8.0 (2 errors)
+PHP 7.4 – 8.0 (3 errors)
 ==========
 
+15: Property Test::$generator is never read, only written.
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): (DateTimeImmutable|null) given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (3 errors)

Line Error
15 Property Test::$generator is never read, only written.
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): (DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.1 – 8.2 (1 error)
+PHP 8.1 – 8.2
 ==========
 
-17: Parameter #1 $a of callable (Closure(A): void)|(Closure(B): void) expects A, A|B given.
+No errors
 
-PHP 8.0 (3 errors)
+PHP 8.0 (2 errors)
 ==========
 
 15: First-class callables are supported only on PHP 8.1 and later.
 15: First-class callables are supported only on PHP 8.1 and later.
-17: Parameter #1 $a of callable (Closure(A): void)|(Closure(B): void) expects A, A|B given.
 
-PHP 7.2 – 7.4 (4 errors)
+PHP 7.2 – 7.4 (3 errors)
 ==========
 
 13: Method Foo::handle() uses native union types but they're supported only on PHP 8.0 and later.
 15: First-class callables are supported only on PHP 8.1 and later.
-15: First-class callables are supported only on PHP 8.1 and later.
-17: Parameter #1 $a of callable (Closure(A): void)|(Closure(B): void) expects A, A|B given.
+15: First-class callables are supported only on PHP 8.1 and later.
Full report

PHP 8.1 – 8.2

No errors

PHP 8.0 (2 errors)

Line Error
15 First-class callables are supported only on PHP 8.1 and later.
15 First-class callables are supported only on PHP 8.1 and later.

PHP 7.2 – 7.4 (3 errors)

Line Error
13 Method Foo::handle() uses native union types but they're supported only on PHP 8.0 and later.
15 First-class callables are supported only on PHP 8.1 and later.
15 First-class callables are supported only on PHP 8.1 and later.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 7.4 – 8.0 (2 errors)
+PHP 7.4 – 8.0 (3 errors)
 ==========
 
+15: Property Test::$generator is never read, only written.
 29: Else branch is unreachable because ternary operator condition is always true.
-34: Parameter #1 $immutableGenerator of class Test constructor expects callable(mixed): DateTimeImmutable, Closure(mixed): DateTimeImmutable|null given.
+35: Parameter #1 $immutableGenerator of class Test constructor expects callable(T): DateTimeImmutable, Closure(T): (DateTimeImmutable|null) given.
 
 PHP 7.1 – 7.3 (4 errors)
 ==========
Full report

PHP 7.4 – 8.0 (3 errors)

Line Error
15 Property Test::$generator is never read, only written.
29 Else branch is unreachable because ternary operator condition is always true.
35 `Parameter #1 $immutableGenerator of class Test constructor expects callable(T): DateTimeImmutable, Closure(T): (DateTimeImmutable

PHP 7.1 – 7.3 (4 errors)

Line Error
29 Syntax error, unexpected T_VARIABLE, expecting ')' on line 29
30 Syntax error, unexpected ')' on line 30
35 Syntax error, unexpected T_DOUBLE_ARROW, expecting ')' on line 35
36 Syntax error, unexpected ')' on line 36

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

No branches or pull requests

4 participants