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

Only one of the @psalm-assert* annotations is remembered #9994

Open
mathroc opened this issue Jul 3, 2023 · 2 comments
Open

Only one of the @psalm-assert* annotations is remembered #9994

mathroc opened this issue Jul 3, 2023 · 2 comments

Comments

@mathroc
Copy link
Contributor

mathroc commented Jul 3, 2023

Bug report

When there is multiple @psalm-assert* annotation, only ...if-true is remembered if present

Code snippet that reproduces the problem

https://psalm.dev/r/bb2095da13

Expected output

$v should be detected as an int line 20, a float line 23 and a !(int|float) line 28

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @psalm-assert-if-true int $v
 * @psalm-assert-if-false float $v
 * @psalm-assert !int $v
 * @psalm-assert !float $v
 */
function f(mixed $v): bool {
    if (is_int($v)) { return true; }
    if (is_float($v)) { return false; }
    
    throw new Exception('not numeric');
}

function g(mixed $v): string {
    try {
        if (f($v)) {
            /** @psalm-trace $v */
            return $v;
        } else {
            /** @psalm-trace $v */
            return $v;
        }
    }
    catch (\Throwable) {
        /** @psalm-trace $v */
        return $v;
    }
}
Psalm output (using commit 8fe1f15):

ERROR: RedundantCondition - 18:13 - Type int for $v is never float

ERROR: InvalidReturnStatement - 20:20 - The inferred type 'int' does not match the declared return type 'string' for g

INFO: Trace - 20:13 - $v: int

INFO: MixedReturnStatement - 23:20 - Possibly-mixed return value

INFO: MixedReturnStatement - 23:20 - Could not infer a return type

INFO: Trace - 23:13 - $v: float|mixed

INFO: MixedReturnStatement - 28:16 - Possibly-mixed return value

INFO: MixedReturnStatement - 28:16 - Could not infer a return type

INFO: Trace - 28:9 - $v: float|mixed

INFO: MixedInferredReturnType - 16:23 - Could not verify return type 'string' for g

@kkmuffme
Copy link
Contributor

kkmuffme commented Jul 4, 2023

Psalm doesn't support multiple @psalm-assert like this yet for the same variable.
Atm (since a recent PR by me) psalm now supports up to 2 (1 psalm-assert-if-true, 1 psalm-assert-if-false) for the same variable.
Feel free to add support for additional assertions though.

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

3 participants