Skip to content

fix(types): account for overflow in negated float checks - #15495

Open
Golden007-prog wants to merge 1 commit into
vimeo:masterfrom
Golden007-prog:fix/15490-negated-float-calculation
Open

Golden007-prog wants to merge 1 commit into
vimeo:masterfrom
Golden007-prog:fix/15490-negated-float-calculation

Conversation

@Golden007-prog

Copy link
Copy Markdown

Psalm reports a false RedundantCondition for !is_float() on an integer addition that may overflow to float.

Before

<?php
/** @return positive-int */
function getPositive(): int { return 1; }

function f(): int {
    $sum = getPositive() + getPositive();
    if (!is_float($sum)) { // RedundantCondition
        /** @psalm-check-type-exact $sum = int<2, max> */;
        return $sum;
    }

    /** @psalm-check-type-exact $sum = float */;
    return 0;
}

After

    if (!is_float($sum)) { // no issue

Fixes #15490.

@Golden007-prog

Copy link
Copy Markdown
Author

bugbot review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 96d4a8e. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 796.1 71.4 $\color{gray}{\textsf{+0.6\%}}$ 25 828 52 365 833 0
✅ symfony 3 605.9 303.6 $\color{gray}{\textsf{+0.2\%}}$ 52 447 100 811 1 891 0
✅ doctrine-orm 339.5 26.5 $\color{gray}{\textsf{-0.4\%}}$ 3 352 5 800 0 0
✅ psl 624.3 33.3 $\color{gray}{\textsf{-0.4\%}}$ 1 296 5 125 2 0
✅ phpunit 434.8 22.1 $\color{gray}{\textsf{-1.9\%}}$ 1 575 2 363 18 0
✅ php-parser 229.1 14.8 $\color{gray}{\textsf{+1.9\%}}$ 17 032 21 091 0 0
✅ tempest 915.9 39.7 $\color{gray}{\textsf{-1.2\%}}$ 5 511 11 032 288 0
✅ amphp-amp 48.7 1.2 $\color{gray}{\textsf{+0.8\%}}$ 30 57 0 0
✅ webmozarts-assert 282.6 3.6 $\color{gray}{\textsf{-2.7\%}}$ 576 2 740 0 0
✅ guzzle-promises 47.3 1.4 $\color{gray}{\textsf{-1.4\%}}$ 148 403 2 0
✅ monolog 140.1 4.4 $\color{gray}{\textsf{+1.1\%}}$ 937 2 483 31 0
✅ drupal 5 084.9 188.9 $\color{gray}{\textsf{-0.6\%}}$ 97 865 168 986 684 0
✅ wordpress 2 031.0 166.7 $\color{gray}{\textsf{+0.3\%}}$ 143 503 678 445 479 1
Total 15 580.3 877.6 $\color{gray}{\textsf{-0.1\%}}$ 350 100 1 051 701 4 228 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run side by side on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a second round was taken and the faster run of each side kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Profiling: when both sides support it, the measured runs use --debug-performance=PATH for the hot-files block, which adds the same small CPU and memory cost to both sides.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head 96d4a8e · base be91d66 (merge-base) · workflow run

@psalm-bot psalm-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Status ✅ Ready: approved, merging is up to a maintainer
Approach ✅ Root cause: negated float reconciliation ignored from_calculation
Correctness ✅ No problems found: Only affects non-equality float negation on overflow-flagged int unions.
Tests ✅ Prove the change: Valid case fails on base; two focused guards cover narrowing and bounded ranges.
Docs & comments ✅ OK: One-line comment states why the check isn't redundant.

SimpleNegatedAssertionReconciler::reconcileFloat() treated a non-equality !is_float() on an int union as redundant even when the union was flagged from_calculation. Integer arithmetic can overflow to float, so that check is not redundant. The PR starts $redundant as false for a non-equality assertion on a from_calculation union that contains an int. This matches the positive path (SimpleAssertionReconciler.php:561-566) and the int sibling (reconcileInt()). The narrowed type comes from combineRemainingTypes(), which drops from_calculation, so the true branch keeps int<2, max> without the overflow flag. The fix is in the reconciler that owns the negated float assertion, which is the right layer.

Automated review generated by AI; posted by psalm-bot.

@psalm-bot psalm-bot added the ready to merge Reviewed with no blocking issues and CI is green; a maintainer decides on merging label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to merge Reviewed with no blocking issues and CI is green; a maintainer decides on merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RedundantCondition on !is_float() for an int that may have overflowed to float

2 participants