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

Incorrect inferred return type 'never' #8862

Closed
jack-worman opened this issue Dec 8, 2022 · 14 comments · Fixed by #9026
Closed

Incorrect inferred return type 'never' #8862

jack-worman opened this issue Dec 8, 2022 · 14 comments · Fixed by #9026

Comments

@jack-worman
Copy link
Contributor

jack-worman commented Dec 8, 2022

https://psalm.dev/r/4d5cfb806a

void|never is being incorrectly reduced to never instead of void.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/4d5cfb806a
<?php

function foo(): void
{
    if (random_int(0, 1) === 1) {
        throw new \Exception();
    }
}
Psalm output (using commit 4dc969b):

INFO: LessSpecificReturnType - 3:17 - The inferred return type 'never' for foo is more specific than the declared return type 'void'

@weirdan
Copy link
Collaborator

weirdan commented Dec 8, 2022

/cc: @kkmuffme

@kkmuffme
Copy link
Contributor

I'm unable to reproduce this locally with psalm 5.1.0 - I get "No errors found!" no matter if I try with phpdoc or with : void native type.

Maybe something in #8828 broke this? (unlikely, that was the last related thing modified which is why I'm asking)

What version is running on psalm.dev? @weirdan

@weirdan
Copy link
Collaborator

weirdan commented Dec 11, 2022

Currently it's ef02ded - a couple of commits behind the HEAD of master.

@kkmuffme
Copy link
Contributor

@jack-worman which version do you get this error on? 5.1.0?

@jack-worman
Copy link
Contributor Author

@kkmuffme I am on 5.1.0

@kkmuffme
Copy link
Contributor

For some reason I cannot get this error on 5.1.0 with this code.
Will wait for 5.2.0 and see if I can reproduce and fix it then, as I don't have much time atm.

@kkmuffme
Copy link
Contributor

I still cannot reproduce this on psalm 5.2.0

Can you confirm that you still get this error on psalm 5.2.0?

If you still get this error, can you try with my psalm config:

<psalm
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://getpsalm.org/schema/config"
	xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
	name="Debug"
	errorLevel="1"
	resolveFromConfigFile="false"
	phpVersion="8.1"
	cacheDirectory=".cache/psalm/dev-tests"
	findUnusedVariablesAndParams="false"
>
</psalm>

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2022

@kkmuffme it requires (undocumented) restrictReturnTypes="true" attribute (Force return types to be as tight as possible on psalm.dev).

@kkmuffme
Copy link
Contributor

Could this setting get documented in https://psalm.dev/docs/running_psalm/configuration/ ?
It didn't exist when I created this never check and I wasn't aware it exists until you told me.

Anyway the bug is bc of ReturnTypeCollector::getReturnTypes does not return implicit void return like in the above example, therefore creating this error (since it just returns "never" here bc of the throw, the implicit void isn't reported in $inferred_return_type_parts):
https://github.com/vimeo/psalm/blob/master/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php#L145

I won't be able to fix this atm, but I guess it's a good first issue to modify it to check if function returns void implicitly.

@kkmuffme
Copy link
Contributor

btw this is not specific to never, this happens with all other implicit void returns too:
https://psalm.dev/r/b0e397c14e
https://psalm.dev/r/6ffb302b9e

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b0e397c14e
<?php

function foo(): void
{
    if (random_int(0, 1) === 1) {
        return false;
    }
}
Psalm output (using commit ef02ded):

ERROR: InvalidReturnStatement - 6:16 - No return values are expected for foo

INFO: LessSpecificReturnType - 3:17 - The inferred return type 'false' for foo is more specific than the declared return type 'void'

ERROR: InvalidFalsableReturnType - 3:17 - The declared return type 'void' for foo does not allow false, but 'false' contains false
https://psalm.dev/r/6ffb302b9e
<?php

function foo(): void
{
    if (random_int(0, 1) === 1) {
        return 10;
    }
}
Psalm output (using commit ef02ded):

ERROR: InvalidReturnStatement - 6:16 - No return values are expected for foo

ERROR: InvalidReturnType - 3:17 - The declared return type 'void' for foo is incorrect, got '10'

@kkmuffme
Copy link
Contributor

It actually has the same issue with void + null when it's explicit too: https://psalm.dev/r/6f227ae9b9

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6f227ae9b9
<?php

function foo(): void
{
    if (random_int(0, 1) === 1) {
        throw new \Exception();
    }
    
    return;
}
Psalm output (using commit d338b00):

INFO: LessSpecificReturnType - 3:17 - The inferred return type 'null' for foo is more specific than the declared return type 'void'

kkmuffme added a commit to kkmuffme/psalm that referenced this issue Dec 29, 2022
* fix implicit void return type errors https://psalm.dev/r/b0e397c14e
* fix void + never = null return type error https://psalm.dev/r/6f227ae9b9
* fixes vimeo#8912 and vimeo#8862
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Dec 29, 2022
* fix implicit void return type errors https://psalm.dev/r/b0e397c14e
* fix void + never = null return type error https://psalm.dev/r/6f227ae9b9
* fixes vimeo#8912 and vimeo#8862
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Dec 29, 2022
* fix implicit void return type errors https://psalm.dev/r/b0e397c14e
* fix void + never = null return type error https://psalm.dev/r/6f227ae9b9
* fixes vimeo#8912 and vimeo#8862
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Jan 5, 2023
* fix implicit void return type errors https://psalm.dev/r/b0e397c14e
* fix void + never = null return type error https://psalm.dev/r/6f227ae9b9
* fixes vimeo#8912 and vimeo#8862
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants