-
Notifications
You must be signed in to change notification settings - Fork 548
implemented math on IntegerRangeType and ConstantIntegerType #637
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
Conversation
|
the remaining out-of-memory error is unrelated. I think this is good to go. in the PR description I have separated the possible future improvements from the todo list. |
staabm
left a comment
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 think this one is now good to go.
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0', $r1 / $z); |
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.
the only case, for which I am kind of suprised/not sure is this assertion. everything else feels solid to me
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.
Yeah, this is definitely a bug, it's never a zero.
Also, there are more divison bugs - in some cases where a float can be produced, the correct type should be int<X, Y>|float.
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.
ahh yes.. obviously we can get a float here :-).
fixed.
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.
Did you also cover a case where only int can be produced? And maybe don't do a range but a union of constant integers?
Like:
// $x 20|40|60
// $y 2|4
\PHPStan\dumpType($x / $y); // 5|10|15|20|30There 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.
Might already be covered by MutatingScope::calculateFromScalars actually
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.
just added your testcase which succeeds
| } | ||
|
|
||
| $dotsCount = $parentPartsCount - $i; | ||
| if ($dotsCount < 0) { |
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.
with the new knowledge phpstan gets by this PR, it now knows that this case can never happen.
removed the code, because we get:
------ ---------------------------------------------------------------------
Line src/File/ParentDirectoryRelativePathHelper.php
------ ---------------------------------------------------------------------
58 Comparison operation "<" between int<0, max> and 0 is always false.
------ ---------------------------------------------------------------------
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.
Wow ❤️
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0', $r1 / $z); |
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.
Yeah, this is definitely a bug, it's never a zero.
Also, there are more divison bugs - in some cases where a float can be produced, the correct type should be int<X, Y>|float.
e70bf85 to
2f12f75
Compare
| assertType('int<-19, 13>', $r1 + $z); | ||
| assertType('int<-2, 30>', $r1 - $z); | ||
| assertType('int<-20, 30>', $r1 * $z); | ||
| assertType('0|float', $r1 / $z); |
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.
Still don't get this case. It can be a non-zero integer as well.
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.
added a line to handle this case explicitly. the test was adjusted to float only, without the 0.
|
Thank you! |
initial pass on implementing simple math operations on the
IntegerRangeType.closes phpstan/phpstan#5457
closes phpstan/phpstan#5515
for the time beeing I have only implemented
+and-betweenConstantIntegerTypeandIntegerRangeTypeto gather initial feedback on the implemation details.I am aware that this is missing lot of stuff, e.g.
ConstantIntegerTypeand right type isIntegerRangeTypepossible future improvements (separate PR)
pow()