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

False positive with enum narrowing leading to TypeDoesNotContainType #9456

Closed
mkroeders opened this issue Mar 5, 2023 · 3 comments · Fixed by #9457
Closed

False positive with enum narrowing leading to TypeDoesNotContainType #9456

mkroeders opened this issue Mar 5, 2023 · 3 comments · Fixed by #9457

Comments

@mkroeders
Copy link

When checking if a value is not of a specific class ie instanceof and using not equals with an enum, the enum is not added as a possible value. This leads to a TypeDoesNotContainType in valid code.

https://psalm.dev/r/1e34436864

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1e34436864
<?php
interface Inter {
}
enum Enu implements Inter {
    case Bar;
}
class Clas implements Inter {
}

function far(Inter $fiz): string {
    if (!$fiz instanceof Clas && $fiz !== Enu::Bar) {
        throw new Exception();
    }
    
    return $fiz === Enu::Bar
        ? 'abz'
        : 'zba';
}
Psalm output (using commit 3af9ccd):

ERROR: TypeDoesNotContainType - 15:12 - enum(Enu::Bar) cannot be identical to Clas

ERROR: TypeDoesNotContainType - 15:12 - Type Clas for $fiz is never =enum(Enu::Bar)

@weirdan
Copy link
Collaborator

weirdan commented Mar 5, 2023

We should probably start with a simpler https://psalm.dev/r/fb4c624cea.

@psalm-github-bot
Copy link

I found these snippets:

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

interface Inter {}

enum Enu implements Inter {
    case Bar;
}

function far(Inter $fiz): void { 
    if ($fiz === Enu::Bar) {}
    throw new RuntimeException;
}
Psalm output (using commit 3af9ccd):

ERROR: TypeDoesNotContainType - 10:9 - Type Inter for $fiz is never =enum(Enu::Bar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants