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

Fix type assertion for Arithemtic addition and subtraction #11269

Merged

Conversation

KevinVanSonsbeek
Copy link
Contributor

Closes #11241

@orklah
Copy link
Collaborator

orklah commented Feb 8, 2025

Cool! Seems like an improvement but I'm not sure it fixes everything

Can you check this for example? https://psalm.dev/r/6f97b61d3d

What you did is replace $sum usage by a generic TInt in one of the branches. It's better because it stops assuming we have a literal 1, but the issue is with $sum everywhere I think

We should fix it here if we can
https://github.com/vimeo/psalm/pull/11269/files#diff-38fd2d45971b85013dfc252b81a4d63816a2f0b9e6d15b5168c5878ad2dd65c4R826

I think we need to check what's in $right_type_part instead of assuming we have 1 or -1.
If it's a literal int, the operation is easy to do. If we have something else, we can just assume a generic TInt will be returned for the whole https://github.com/vimeo/psalm/pull/11269/files#diff-38fd2d45971b85013dfc252b81a4d63816a2f0b9e6d15b5168c5878ad2dd65c4R825 branch

Copy link

I found these snippets:

https://psalm.dev/r/6f97b61d3d
<?php
        /** @return int<0,5> */
        function intFunc(): int {
            return 5;
        }
        $a = 1;
        $a += intFunc();
/** @psalm-trace $a */;
Psalm output (using commit 38fc844):

INFO: Trace - 8:23 - $a: int<1, 6>

INFO: UnusedVariable - 6:9 - $a is never referenced or the value is not used

INFO: UnusedVariable - 7:9 - $a is never referenced or the value is not used

@KevinVanSonsbeek
Copy link
Contributor Author

KevinVanSonsbeek commented Feb 8, 2025

I can't really seem to reproduce the sum issues in other situations.

The example you sent with an int-range is handled in an earlier branch. Same with additions/subtractions that have an TLiteralInt on the right side, and are not inside of a loop.

If i check the right type part, and grab that value if it is a TLiteralInt it actually ends up breaking int-ranges for loops.
ex:

// Replacing the sum assignment with this:
  if ($right_type_part instanceof TLiteralInt) {
      $sum = $right_type_part->value;
  } else {
      $sum = $parent instanceof VirtualPlus ? 1 : -1;
  }

Causes the incrementInLoop test to fail:

-    '$i' => 'int<0, 10>'
-    '$j' => 'int<100, 110>'
+    '$i' => 'int<0, 11>'
+    '$j' => 'int<100, 111>'

Increments in loops also seem to be the only situation where the righthand side is a TLiteralInt from what i can see in the tests present.

Edit: typo

@orklah
Copy link
Collaborator

orklah commented Feb 8, 2025

Fair enough! Can you just leave a comment above the $sum initialisation with something like "This seems arbitrary defined as 1/-1, this may need a better handling if there's issues"

Just to facilitate a future debugging :p

Thanks!

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Feb 10, 2025
@orklah orklah merged commit 27f85a0 into vimeo:6.x Feb 10, 2025
51 of 52 checks passed
@orklah
Copy link
Collaborator

orklah commented Feb 10, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type inference issue when using arithmetic assignments
2 participants