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
IntegerRangeMath: cover more maxima cases #702
Conversation
@@ -281,6 +281,16 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) { | |||
|
|||
assertType('5|10|15|20|30', $x / $y); | |||
|
|||
assertType('float|int<0, max>', $rMax / $rMax); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Psalm I chose not to go this route and just inferred int|float
. There's a point where the line is too blurry. In this case, whatever operations or expectation you have that would justify knowing the range will get invalidated by float
. You can't deduce the result is an int, nor a float, you can't deduce it's positive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd have to fight current way of working of PHPStan's typesystem to make the result less precise like that. I don't think it's worth it (unless there's a bug stemming from that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes I'm not sure I understood what you meant by that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to sum up: the assertion for this line should be changed to float|int
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@orklah It's harder for this to make float|int
instead of the current result. I'd leave it the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@orklah I mentioned the cast-cases in phpstan/phpstan#5731 (comment)
not sure how hard it would be to get the remaing failing cast-case work though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, the current type is also wrong https://phpstan.org/r/b6b8648f-b0a3-4c37-97cf-12ee838d3336
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, this case is fixed with this PR, see assertion in line 284
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
back to the initial question: IMO this is the most correct type we can expect atm (until phpstan supports float-ranges).
since its also the one which is mathematically correct and we need the implementation lines anayway for the other expectations to pass with this PR, we should merge as is IMO.
@@ -281,6 +281,16 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) { | |||
|
|||
assertType('5|10|15|20|30', $x / $y); | |||
|
|||
assertType('float|int<0, max>', $rMax / $rMax); | |||
assertType('(float|int)', $rMin / $rMin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param int<0, max> $b | ||
*/ | ||
function divisionLoosesInformation(int $a, int $b): void { | ||
assertType('float|int<0, max>',$a/$b); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//cc @orklah ;) |
* @param int<0, max> $a | ||
* @param int<0, max> $b | ||
*/ | ||
function divisionLoosesInformation(int $a, int $b): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats the actual case/feature reported/requested in phpstan/phpstan#5731
THank you! |
closes phpstan/phpstan#5731