Repository navigation
fix(loop): a variable assigned in the right operand of || is defined after a while loop left through its condition - #14396
Conversation
…after a while loop left through its condition vimeo#13765 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Behaviour change
|
✅ Performance: unchanged · Types: decreased
How to read this table
✅ php-parser: issues +0 −36Memory 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
Removed (36)
head a3ff99e · base cc4ab02 (merge-base) · workflow run |
orklah
left a comment
There was a problem hiding this comment.
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>
|
@orklah thanks, done in a3ff99e: the loop now walks
CI is green apart from |
A variable assigned in the right operand of
||in awhilecondition 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
After
Fixes #13765