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

IntegerRangeMath: cover more maxima cases #702

Merged
merged 4 commits into from Oct 11, 2021

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 7, 2021

@staabm staabm changed the title IntegerRangeMath: added failling division tests IntegerRangeMath: cover more maxima cases Oct 7, 2021
@@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

Copy link
Member

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).

Copy link
Contributor

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

Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

@staabm staabm Oct 7, 2021

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

Copy link
Contributor

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

Copy link
Contributor Author

@staabm staabm Oct 7, 2021

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

Copy link
Contributor Author

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);
Copy link
Contributor Author

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm
Copy link
Contributor Author

staabm commented Oct 7, 2021

//cc @orklah ;)

* @param int<0, max> $a
* @param int<0, max> $b
*/
function divisionLoosesInformation(int $a, int $b): void {
Copy link
Contributor Author

@staabm staabm Oct 7, 2021

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

@ondrejmirtes
Copy link
Member

THank you!

@staabm staabm deleted the range-divison branch October 11, 2021 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants