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

property assignment bug with union and int ranges #10826

Open
staabm opened this issue Mar 14, 2024 · 3 comments
Open

property assignment bug with union and int ranges #10826

staabm opened this issue Mar 14, 2024 · 3 comments

Comments

@staabm
Copy link
Contributor

staabm commented Mar 14, 2024

this should not error https://psalm.dev/r/808fd263bd

Copy link

I found these snippets:

https://psalm.dev/r/808fd263bd
<?php

/**
 * @return -1|positive-int
 */
function getInt():int {
    if (rand(0,1)) {
        return 234;
    }
    return -1;
}

class C {
    /** @var positive-int|null */
    private $p;
    
    function doFoo():void {
        $i = getInt();
        if ($i === -1) {
            $i = null;
        }
        $this->p = $i;
    }
}
Psalm output (using commit ef3b018):

ERROR: InvalidPropertyAssignmentValue - 22:20 - $this->p with declared type 'int<1, max>|null' cannot be assigned type 'int<0, max>|null'

@jack-worman
Copy link
Contributor

jack-worman commented Mar 15, 2024

Simplified: https://psalm.dev/r/4998359b55

The root issue seems to be -1|positive-int getting wrongly converted to int<-1, max>.
Technically it isn't wrong, just more general than it needs to be.

Copy link

I found these snippets:

https://psalm.dev/r/4998359b55
<?php

/**
 * @return -1|positive-int
 */
function foo(): int { return random_int(0, 1) ? -1 : 1; }

$a = foo();

/** @psalm-trace $a */
Psalm output (using commit ef3b018):

INFO: Trace - 10:23 - $a: int<-1, max>

INFO: UnusedVariable - 8:1 - $a is never referenced or the value is not used

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

No branches or pull requests

2 participants