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

InvalidReturnType due to void and null not treated the same #8178

Closed
kkmuffme opened this issue Jun 27, 2022 · 7 comments · Fixed by #8624
Closed

InvalidReturnType due to void and null not treated the same #8178

kkmuffme opened this issue Jun 27, 2022 · 7 comments · Fixed by #8624
Labels

Comments

@kkmuffme
Copy link
Contributor

kkmuffme commented Jun 27, 2022

https://psalm.dev/r/f4bb991d20

void and null must be treated as the same thing.

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @return void|never
 */
function foo() {
    if ( rand( 0, 10 ) > 5 ) {
        return;
    }    
}
Psalm output (using commit 8b7bc07):

ERROR: InvalidReturnType - 4:12 - The declared return type 'null' for foo is incorrect, got 'void'

@AndrolGenhald
Copy link
Collaborator

Hmmm, I don't think it actually makes sense to have a return type of void|never. Regardless though, that error is incredibly misleading.

@orklah
Copy link
Collaborator

orklah commented Jun 27, 2022

Hmmm, I don't think it actually makes sense to have a return type of void|never. Regardless though, that error is incredibly misleading.

It can make sense for assertions methods where you either throw or do nothing. but the snippet in example doesn't do that. So yeah, it's just a wrong phrasing

@AndrolGenhald
Copy link
Collaborator

It can make sense for assertions methods where you either throw or do nothing.

Shouldn't that just combine to void though? never is the bottom type, so it's a subtype of all other types, so if you do x|never it should just simplify to x.

@orklah
Copy link
Collaborator

orklah commented Jun 27, 2022

True. The case I had some times ago was something like ($die_on_redirect is true ? never : void). So technically it's the same case but the combination doesn't happen

@AndrolGenhald
Copy link
Collaborator

The case I had some times ago was something like ($die_on_redirect is true ? never : void).

Ahh, yeah I could see that then. Interestingly though, this actually works fine while this doesn't.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8dad765588
<?php

/**
 * @return ($bar is true ? void : never)
 */
function foo(bool $bar) {
    if ($bar) {
        return;
    }
    
    exit;    
}
Psalm output (using commit 8b7bc07):

No issues!
https://psalm.dev/r/d162606c86
<?php

/**
 * @return void|never
 */
function foo(bool $bar) {
    if ($bar) {
        return;
    }
    
    exit;    
}
Psalm output (using commit 8b7bc07):

ERROR: InvalidReturnType - 4:12 - The declared return type 'null' for foo is incorrect, got 'void'

kkmuffme added a commit to kkmuffme/psalm that referenced this issue Oct 17, 2022
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Oct 27, 2022
* require explicit "never" return type when function always exits
* error if function does not exit, but return type explicitly contains "never"
* Fix: vimeo#8175
* Fix: vimeo#8178
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Nov 10, 2022
* require explicit "never" return type when function always exits
* error if function does not exit, but return type explicitly contains "never"
* Fix: vimeo#8175
* Fix: vimeo#8178
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Nov 10, 2022
* require explicit "never" return type when function always exits
* error if function does not exit, but return type explicitly contains "never"
* Fix: vimeo#8175
* Fix: vimeo#8178
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Nov 10, 2022
* require explicit "never" return type when function always exits, except if it only throws
* error if function does not exit, but return type explicitly contains "never"
* Fix: vimeo#8175
* Fix: vimeo#8178
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Dec 1, 2022
* require explicit "never" return type when function always exits, except if it only throws
* error if function does not exit, but return type explicitly contains "never"
* Fix: vimeo#8175
* Fix: vimeo#8178
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants