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

Wrong resolving of generics inside try/catch block #6353

Closed
honzatrtik opened this issue Jan 12, 2022 · 6 comments
Closed

Wrong resolving of generics inside try/catch block #6353

honzatrtik opened this issue Jan 12, 2022 · 6 comments

Comments

@honzatrtik
Copy link

honzatrtik commented Jan 12, 2022

Code snippet that reproduces the problem

https://phpstan.org/r/ecf86117-177b-439c-b8e3-4e3040a8b6e2

Ends with
Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>.

Additionally, if we set return values to variable as follows:

/**
 * @template T
 * @param callable():T $fn
 * @return Option<T>
 */
function fromCallbackThatCanThrow(callable $fn) {
	try {
		$a = some($fn());
	} catch (Throwable $failure) {
		$a = none();
	}
	return $a;
};

https://phpstan.org/r/dca44c7f-7e85-4f99-a66c-0b4629a25901

then the reported error is even more confusing:
Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>|Option<T>.

If never is subtype of T (its universal subtype - bottom type, so it should be) and T is covariant, then Option<never> should be also subtype of Option<T>. So Option<never>| Option<T> should be Option<T>

Expected output

No errors should be reported

@ondrejmirtes ondrejmirtes added this to the Generics milestone Feb 1, 2022
@ondrejmirtes
Copy link
Member

I guess that never could be accepted as T.

@ZebulanStanphill
Copy link
Contributor

ZebulanStanphill commented Feb 1, 2022

Yeah, that's the case in TypeScript. T | never is equal to T.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1
 ==========
 
-58: Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>.
+No errors
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (1 error)
 ==========
 
-22: Promoted properties are supported only on PHP 8.0 and later.
-58: Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>.
+22: Promoted properties are supported only on PHP 8.0 and later.
Full report

PHP 8.0 – 8.1

No errors

PHP 7.1 – 7.4 (1 error)

Line Error
22 Promoted properties are supported only on PHP 8.0 and later.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1
 ==========
 
-60: Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>|Option<T>.
+No errors
 
-PHP 7.1 – 7.4 (2 errors)
+PHP 7.1 – 7.4 (1 error)
 ==========
 
-22: Promoted properties are supported only on PHP 8.0 and later.
-60: Function fromCallbackThatCanThrow() should return Option<T> but returns Option<*NEVER*>|Option<T>.
+22: Promoted properties are supported only on PHP 8.0 and later.
Full report

PHP 8.0 – 8.1

No errors

PHP 7.1 – 7.4 (1 error)

Line Error
22 Promoted properties are supported only on PHP 8.0 and later.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@c2b9e71

@github-actions
Copy link

github-actions bot commented Apr 5, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants