-
Notifications
You must be signed in to change notification settings - Fork 669
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
Type inference issue when using arithmetic assignments #11241
Comments
I found these snippets: https://psalm.dev/r/6385ed8d8d<?php
$var1 = 15;
/** @psalm-trace $var1 */
function getRandomIntOrNull(): int|null
{
$int = random_int(0, 10);
if ($int < 5) {
return null;
}
return 5;
}
$var1 -= getRandomIntOrNull() ?? 0;
/** @psalm-trace $var1 */
if ($var1 === 15) {
echo "No value subtracted";
}
https://psalm.dev/r/c9b1c78210<?php
$var1 = 15;
/** @psalm-trace $var1 */
function getRandomIntOrNull(): int|null
{
$int = random_int(0, 10);
if ($int < 5) {
return null;
}
return 5;
}
$var1 += getRandomIntOrNull() ?? 0;
/** @psalm-trace $var1 */
if ($var1 === 15) {
echo "No value added";
}
|
Oh yeah that's weird, Psalm seems to assume any integer is 1 for these operations: https://psalm.dev/r/bda522d385 |
I found these snippets: https://psalm.dev/r/bda522d385<?php
$var1 = 15;
/** @psalm-trace $var1 */;
function getRandomInt(): int
{
return 5;
}
$var1 += getRandomInt();
/** @psalm-trace $var1 */;
$var1 -= getRandomInt();
/** @psalm-trace $var1 */;
|
Pretty sure the issue is here: https://github.com/vimeo/psalm/blob/6.x/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php#L826 We assume that VirtualPlus and VirtualMinus are only used for things like What we should do is check for additional operands to see if there is a real type to be handled after the operator |
When upgrading to Psalm 6 we've noticed some new errors in the codebase.
Which seem to be caused by psalm incorrectly tracking the type of a variable, if it is created as an integer, and then updated through arithmetic addition or subtraction assignments.
See
https://psalm.dev/r/6385ed8d8d
and
https://psalm.dev/r/c9b1c78210
This problem only seems to happen during addition and subtraction though. The other types of arithmetic assignments change the type of the variable back to either
int
orfloat|int
.The text was updated successfully, but these errors were encountered: