Skip to content

Commit a8975b1

Browse files
committed
Fix regression in for loop
1 parent 2a61ebc commit a8975b1

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ private function processStmtNode(
835835
$bodyScope = $this->enterForeach($bodyScope, $stmt);
836836
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
837837
}, $context->enterDeep())->filterOutLoopExitPoints();
838-
$alwaysTerminating = $bodyScopeResult->isAlwaysTerminating();
839838
$bodyScope = $bodyScopeResult->getScope();
840839
foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) {
841840
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
@@ -848,7 +847,7 @@ private function processStmtNode(
848847
$bodyScope = $prevScope->generalizeWith($bodyScope);
849848
}
850849
$count++;
851-
} while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS);
850+
} while ($count < self::LOOP_SCOPE_ITERATIONS);
852851
}
853852

854853
$bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope);
@@ -904,7 +903,6 @@ private function processStmtNode(
904903
}, ExpressionContext::createDeep())->getTruthyScope();
905904
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
906905
}, $context->enterDeep())->filterOutLoopExitPoints();
907-
$alwaysTerminating = $bodyScopeResult->isAlwaysTerminating();
908906
$bodyScope = $bodyScopeResult->getScope();
909907
foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) {
910908
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
@@ -917,7 +915,7 @@ private function processStmtNode(
917915
$bodyScope = $prevScope->generalizeWith($bodyScope);
918916
}
919917
$count++;
920-
} while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS);
918+
} while ($count < self::LOOP_SCOPE_ITERATIONS);
921919
}
922920

923921
$bodyScope = $bodyScope->mergeWith($scope);
@@ -999,7 +997,7 @@ private function processStmtNode(
999997
$bodyScope = $prevScope->generalizeWith($bodyScope);
1000998
}
1001999
$count++;
1002-
} while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS);
1000+
} while ($count < self::LOOP_SCOPE_ITERATIONS);
10031001

10041002
$bodyScope = $bodyScope->mergeWith($scope);
10051003
}
@@ -1082,7 +1080,6 @@ private function processStmtNode(
10821080
}
10831081
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
10841082
}, $context->enterDeep())->filterOutLoopExitPoints();
1085-
$alwaysTerminating = $bodyScopeResult->isAlwaysTerminating();
10861083
$bodyScope = $bodyScopeResult->getScope();
10871084
foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) {
10881085
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
@@ -1103,7 +1100,7 @@ private function processStmtNode(
11031100
$bodyScope = $prevScope->generalizeWith($bodyScope);
11041101
}
11051102
$count++;
1106-
} while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS);
1103+
} while ($count < self::LOOP_SCOPE_ITERATIONS);
11071104
}
11081105

11091106
$bodyScope = $bodyScope->mergeWith($initScope);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ public function dataFileAsserts(): iterable
11421142
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-82.php');
11431143
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4565.php');
11441144
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3789.php');
1145+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8520.php');
11451146
}
11461147

11471148
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug8520;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
for ($i = 0; $i < 7; $i++) {
8+
assertType('int<0, 6>', $i);
9+
$tryMax = true;
10+
while ($tryMax) {
11+
$tryMax = false;
12+
}
13+
}
14+
15+
assertType('int<7, max>', $i);

0 commit comments

Comments
 (0)