From a8975b1800d6c5cb88a6af02e132b4e44e093fc3 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 16 Dec 2022 16:09:11 +0100 Subject: [PATCH] Fix regression in for loop --- src/Analyser/NodeScopeResolver.php | 11 ++++------- tests/PHPStan/Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-8520.php | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/bug-8520.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index b8c7e178c3..0102032c72 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -835,7 +835,6 @@ private function processStmtNode( $bodyScope = $this->enterForeach($bodyScope, $stmt); $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { }, $context->enterDeep())->filterOutLoopExitPoints(); - $alwaysTerminating = $bodyScopeResult->isAlwaysTerminating(); $bodyScope = $bodyScopeResult->getScope(); foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); @@ -848,7 +847,7 @@ private function processStmtNode( $bodyScope = $prevScope->generalizeWith($bodyScope); } $count++; - } while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS); + } while ($count < self::LOOP_SCOPE_ITERATIONS); } $bodyScope = $bodyScope->mergeWith($this->polluteScopeWithAlwaysIterableForeach ? $scope->filterByTruthyValue($arrayComparisonExpr) : $scope); @@ -904,7 +903,6 @@ private function processStmtNode( }, ExpressionContext::createDeep())->getTruthyScope(); $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { }, $context->enterDeep())->filterOutLoopExitPoints(); - $alwaysTerminating = $bodyScopeResult->isAlwaysTerminating(); $bodyScope = $bodyScopeResult->getScope(); foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); @@ -917,7 +915,7 @@ private function processStmtNode( $bodyScope = $prevScope->generalizeWith($bodyScope); } $count++; - } while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS); + } while ($count < self::LOOP_SCOPE_ITERATIONS); } $bodyScope = $bodyScope->mergeWith($scope); @@ -999,7 +997,7 @@ private function processStmtNode( $bodyScope = $prevScope->generalizeWith($bodyScope); } $count++; - } while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS); + } while ($count < self::LOOP_SCOPE_ITERATIONS); $bodyScope = $bodyScope->mergeWith($scope); } @@ -1082,7 +1080,6 @@ private function processStmtNode( } $bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void { }, $context->enterDeep())->filterOutLoopExitPoints(); - $alwaysTerminating = $bodyScopeResult->isAlwaysTerminating(); $bodyScope = $bodyScopeResult->getScope(); foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { $bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope()); @@ -1103,7 +1100,7 @@ private function processStmtNode( $bodyScope = $prevScope->generalizeWith($bodyScope); } $count++; - } while (!$alwaysTerminating && $count < self::LOOP_SCOPE_ITERATIONS); + } while ($count < self::LOOP_SCOPE_ITERATIONS); } $bodyScope = $bodyScope->mergeWith($initScope); diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 98ab525e18..0e8198e765 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1142,6 +1142,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-82.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4565.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3789.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8520.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-8520.php b/tests/PHPStan/Analyser/data/bug-8520.php new file mode 100644 index 0000000000..d5d6c605fd --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-8520.php @@ -0,0 +1,15 @@ +', $i); + $tryMax = true; + while ($tryMax) { + $tryMax = false; + } +} + +assertType('int<7, max>', $i);