Skip to content

IntegerRangeMath: cover more maxima cases#702

Merged
ondrejmirtes merged 4 commits intophpstan:masterfrom
staabm:range-divison
Oct 11, 2021
Merged

IntegerRangeMath: cover more maxima cases#702
ondrejmirtes merged 4 commits intophpstan:masterfrom
staabm:range-divison

Conversation

@staabm
Copy link
Copy Markdown
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

assertType('5|10|15|20|30', $x / $y);

assertType('float|int<0, max>', $rMax / $rMax);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

assertType('5|10|15|20|30', $x / $y);

assertType('float|int<0, max>', $rMax / $rMax);
assertType('(float|int)', $rMin / $rMin);
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

Division on number range loses information about range

4 participants