Skip to content

fix(loop): a variable assigned in the right operand of || is defined after a while loop left through its condition - #14396

Merged
orklah merged 2 commits into
vimeo:masterfrom
jirkace:fix/13765-while-or-assignment
Nov 22, 2026
Merged

orklah merged 2 commits into
vimeo:masterfrom
jirkace:fix/13765-while-or-assignment

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

A variable assigned in the right operand of || in a while condition is reported as possibly undefined after the loop, although the loop is only left once the whole condition is false, so both operands ran.

Before

/** @param array<int, int> $t */
function f(array $t, int $d): int {
    $s = 0;
    while (!($s > 3) || ($a = $t[$s] ?? 0) === $d) {
        $s++;
    }
    return $a; // PossiblyUndefinedVariable, MixedReturnStatement
}

After

    return $a; // no issue

Fixes #13765

…after a while loop left through its condition vimeo#13765

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • A variable assigned in the right operand of || (or or) in a while condition is defined after the loop when the loop has no break and no do: the loop is left only when the condition is false, i.e. both operands ran. Verified by the new WhileTest cases.
  • Unchanged and covered by tests: the right operand of && stays PossiblyUndefinedVariable, and so does an || assignment in a loop that contains a break.
  • Self-analysis: only the 2 PoolEventLoopDriverTest MixedArgument errors that master reports too here. Not run: the full suite (CI), corpus benchmarks.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 766 71.4 $\color{gray}{\textsf{+0.3\%}}$ 25 925 53 512 677
✅ symfony 3 606 251.8 $\color{gray}{\textsf{+0.9\%}}$ 50 580 98 373 1 491
✅ doctrine-orm 393 21.2 $\color{gray}{\textsf{+0.7\%}}$ 3 480 6 161 0
✅ psl 632 27.2 $\color{gray}{\textsf{+0.4\%}}$ 1 299 5 455 1
✅ phpunit 472 20.5 $\color{gray}{\textsf{+1.2\%}}$ 1 744 3 459 18
✅ php-parser 237 13.9 $\color{gray}{\textsf{-1.0\%}}$ 17 097 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-36}}$ 20 987 $\color{green}{\textsf{-48}}$ 0
✅ tempest 905 36.5 $\color{gray}{\textsf{-0.1\%}}$ 5 045 10 533 242
✅ amphp-amp 62 2.1 $\color{gray}{\textsf{-0.5\%}}$ 29 49 0
✅ webmozarts-assert 287 5.0 $\color{gray}{\textsf{+0.8\%}}$ 578 2 724 0
✅ guzzle-promises 57 2.1 $\color{gray}{\textsf{+0.5\%}}$ 154 427 2
✅ monolog 154 6.1 $\color{gray}{\textsf{-0.7\%}}$ 903 2 403 10
✅ drupal 4 851 230.4 $\color{gray}{\textsf{-0.2\%}}$ 97 524 169 104 568
Total 13 423 688.1 $\color{gray}{\textsf{+0.3\%}}$ 204 358 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-36}}$ 373 187 $\color{green}{\textsf{-48}}$ 3 009
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 on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster 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. Thresholds apply per project, not to the total.
  • 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.
✅ php-parser: issues +0 −36

Memory 237 → 237 MB (<0.1 %) · Wall 14.1 → 13.9 s (−1.0 %)

Weighted 21 035 → 20 987 (−48, −0.23 %): L1 −30 · L3 −6

Issue type added removed
MixedArgument 0 1
MixedArrayOffset 0 11
MixedArrayTypeCoercion 0 7
MixedAssignment 0 3
MixedOperand 0 4
PossiblyNullArgument 0 1
PossiblyNullArrayOffset 0 4
PossiblyNullOperand 0 4
PossiblyUndefinedVariable 0 1

Removed (36)

  • lib/PhpParser/ParserAbstract.php:254 MixedArrayOffset (error): Cannot access value on variable $this->actionBase using mixed offset
  • lib/PhpParser/ParserAbstract.php:254 MixedArrayTypeCoercion (error): Coercion from array offset type 'int|mixed|null' to the expected type 'array-key'
  • lib/PhpParser/ParserAbstract.php:254 PossiblyNullArrayOffset (error): Cannot access value on variable $this->actionBase using possibly null offset int|mixed|null
  • lib/PhpParser/ParserAbstract.php:255 MixedArrayOffset (error): Cannot access value on variable $this->actionDefault using mixed offset
  • lib/PhpParser/ParserAbstract.php:255 MixedArrayTypeCoercion (error): Coercion from array offset type 'int|mixed|null' to the expected type 'array-key'
  • lib/PhpParser/ParserAbstract.php:255 PossiblyNullArrayOffset (error): Cannot access value on variable $this->actionDefault using possibly null offset int|mixed|null
  • lib/PhpParser/ParserAbstract.php:279 MixedArrayOffset (error): Cannot access value on variable $this->actionBase using mixed offset
  • lib/PhpParser/ParserAbstract.php:279 MixedOperand (error): Left operand cannot be mixed
  • lib/PhpParser/ParserAbstract.php:279 PossiblyNullOperand (error): Left operand cannot be nullable, got int|mixed|null
  • lib/PhpParser/ParserAbstract.php:314 MixedArrayOffset (error): Cannot access value on variable $this->actionDefault using mixed offset
  • … and 26 more

head a3ff99e · base cc4ab02 (merge-base) · workflow run

@orklah orklah 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.

Thanks, the rule is right for a single || condition, but it is also applied to conditions that don't decide the exit, which makes a really undefined variable look defined:

function w(bool $x): int {
    while ((!$x || ($a = rand()) === 3) && rand(0, 1)) {
        $x = true;
    }
    return $a; // PossiblyUndefinedVariable on master, nothing on this branch; w(false) leaves the loop with $a undefined
}

function f(bool $x): int {
    for (; !$x || ($b = rand()) === 3, rand(0, 1);) {
        $x = true;
    }
    return $b; // same
}

A while condition's && is split into several $pre_conditions, so the loop can be left through another conjunct, and a for is only decided by its last condition. Walking $deciding_condition (computed just above) instead of $evaluated_conditions fixes both, and keeps the case of the PR passing:

if ($deciding_condition) {
    self::addWrittenWhenFalseTypes(
        $deciding_condition,
        $loop_context->self,
        $statements_analyzer,
        $written_when_false_types,
    );
}

Could you add both snippets above as invalid cases (PossiblyUndefinedVariable), and a valid for case where the || is the last condition, e.g. for ($i = 0; $i++, !$x || ($c = rand()) === 3;) { $x = true; } return $c;?

…|| wrote vimeo#13765

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@orklah thanks, done in a3ff99e: the loop now walks $deciding_condition instead of $evaluated_conditions, so only the condition that decides the exit defines what its || wrote.

  • Your while snippet is WhileTest::variableAssignedInAnOrThatIsOneConjunctOfTheWhileConditionIsPossiblyUndefined and your for snippet is variableAssignedInAnOrThatIsNotTheLastForConditionIsPossiblyUndefined (both PossiblyUndefinedVariable): they report nothing on the commit before the fix and pass on the current head.
  • Valid case variableAssignedInTheRightOperandOfOrInTheLastForConditionIsDefinedAfterTheLoop (the || as the last for condition, as you suggested); it passes before and after, as a guard.

CI is green apart from label. PHPUnit suite not run locally.

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

@orklah orklah 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.

Thanks @jirkace!

@orklah
orklah merged commit a27121b into vimeo:master Nov 22, 2026
71 of 72 checks passed
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.

An assignment on the right of || in a while condition is possibly undefined after the loop

2 participants