phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Union Type with `match (true)` causes wrong type assumption.

Open Dgame opened this issue 3 years ago • 9 comments

Bug report

Maybe related to #7698: Union Type with match (true) causes wrong type assumption.

Code snippet that reproduces the problem

https://phpstan.org/r/f5296bb1-399d-48ee-92c1-0c1ae8eb6e45

Expected output

No error

Did PHPStan help you today? Did it make you happy in any way?

Of course it does 👍

Dgame avatar Jul 29 '22 21:07 Dgame

I don't get it - what else can it be besides A and B?

ondrejmirtes avatar Aug 05 '22 20:08 ondrejmirtes

Reduced example: https://phpstan.org/r/aa3d6eea-69cb-421d-8b65-429fb0f30d9b

I don't get it - what else can it be besides A and B?

Not sure, but apparently it now thinks that the second match-arm is always true.

Dgame avatar Aug 05 '22 20:08 Dgame

Because it is?

ondrejmirtes avatar Aug 05 '22 20:08 ondrejmirtes

How? Could be both, either A or B, so no error should occur: https://3v4l.org/9Q7G9 As you see, the second match-arm is not true if we call it with test(new A());

Dgame avatar Aug 05 '22 20:08 Dgame

You're just misunderstanding the message...

ondrejmirtes avatar Aug 06 '22 11:08 ondrejmirtes

I use true as a condition and either is $t instanceof A or $t instanceof B, but never both. So all correct, there should be no error. So I am not sure how I could misunderstand the message. Care to explain? 😕

Dgame avatar Aug 06 '22 15:08 Dgame

BTW, just tested, neither psalm nor phan report an error here. Normally I think phpstan is more accurate, but in this particular case I'm a bit doubtful, no offense of course.

Dgame avatar Aug 06 '22 15:08 Dgame

It's the same reason why the same error is reported here: https://phpstan.org/r/a1b566b9-6ed4-46e8-b6b1-db5cf4cf205a

PHPStan wants you to use default instead of $t instanceof B there (only in case you're using strict-rules). But I understand why it might not be always ideal, I've got the idea that the last match arm should not report "always true" situations.

ondrejmirtes avatar Aug 06 '22 20:08 ondrejmirtes

It's the same reason why the same error is reported here: https://phpstan.org/r/a1b566b9-6ed4-46e8-b6b1-db5cf4cf205a

PHPStan wants you to use default instead of $t instanceof B there (only in case you're using strict-rules). But I understand why it might not be always ideal, I've got the idea that the last match arm should not report "always true" situations.

Ah, I think I get it now. Thanks for the reopening though.

Dgame avatar Aug 06 '22 21:08 Dgame

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[bot] avatar Feb 10 '23 00:02 github-actions[bot]