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

Assertion dependent type not recognized in dev-master, but works in 4.x-dev. #7968

Closed
rzvc opened this issue May 14, 2022 · 5 comments
Closed

Comments

@rzvc
Copy link

rzvc commented May 14, 2022

function test(mixed $mixed, bool $b) : void
{
	if ($b && !is_string($mixed))
		return;
    
	echo $b ? $mixed : '';
}

https://psalm.dev/r/8979ac9579

@psalm-github-bot
Copy link

psalm-github-bot bot commented May 14, 2022

I found these snippets:

https://psalm.dev/r/8979ac9579
<?php

function test(mixed $mixed, bool $b) : void
{
	if ($b && !is_string($mixed))
		return;
    
	echo $b ? $mixed : '';
}
Psalm output (using commit f960d71):

INFO: MixedArgument - 8:7 - Argument 1 of echo cannot be ''|mixed, expecting string

@orklah
Copy link
Collaborator

orklah commented May 17, 2022

Possibly a regression of #7484

@muglug do you have an opinion on that?

@muglug
Copy link
Collaborator

muglug commented May 18, 2022

This is a somewhat more simple reproducer: https://psalm.dev/r/3b5374dddc but removing the ternary fixes it: https://psalm.dev/r/7a167ba5cd

So it’s likely an issue with ternary analysis.

I’ll have a look at this tomorrow if you don’t get to it.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/3b5374dddc
<?php

function test(string|object $s, bool $b) : string
{
	if (!$b || is_string($s)) {
		return $b ? $s : '';
    }
    return '';
}
Psalm output (using commit f960d71):

ERROR: InvalidReturnStatement - 6:10 - The inferred type 'object|string' does not match the declared return type 'string' for test

ERROR: InvalidReturnType - 3:44 - The declared return type 'string' for test is incorrect, got 'object|string'
https://psalm.dev/r/7a167ba5cd
<?php

function test(string|object $s, bool $b) : string
{
	if (!$b || is_string($s)) {
        if ($b) {
            return $s;
        } else {
		    return '';
        }
    }
    return '';
}
Psalm output (using commit f960d71):

No issues!

@muglug
Copy link
Collaborator

muglug commented May 20, 2022

Fixed in latest by ensuring clause simplification in ternaries matches behaviour in if/else analysis

@muglug muglug closed this as completed May 20, 2022
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