Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Analyser/StmtHandler/ForHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,14 @@ public function processStmt(
$passFlows[] = $bodyScopeResult->getVariableFlow();
$backEdgeScope = $bodyScopeResult->getLoopBackEdgeScope();
if ($backEdgeScope === null) {
$bodyScope = $prevScope;
break;
if ($nodeScopeResolver->isReplayableConvergenceBody($stmt, $stmt->stmts)) {
$bodyScope = $prevScope;
break;
}
// a nested loop is walked only once in deep context, so the
// back edge it would reach on a later iteration can look dead -
// continue from the end of the body instead of stopping here
$backEdgeScope = $bodyScopeResult->getScope();
}
$bodyScope = $backEdgeScope;

Expand Down
2 changes: 1 addition & 1 deletion src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
final class TurboExtensionEnabler
{

public const EXPECTED_EXTENSION_VERSION = 'a0f7086';
public const EXPECTED_EXTENSION_VERSION = '6f46059';

private static bool $active = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ public function testBug15243(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-15243.php'], []);
}

public function testBug15420(): void
{
$this->analyse([__DIR__ . '/data/bug-15420.php'], []);
}

public function testShiftLeftUnboundedRange(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/shift-left-unbounded-range.php'], []);
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-15420.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug15420;

class Test {
/** @param string[] $cards */
public function f(array $cards): void
{
for ($i = 0; $i < 10; $i++) {
$seen = [];
$later = [];
foreach ($cards as $card) {
if (array_key_exists($card, $seen)) {
$later[] = $card;
continue;
}
$seen[$card] = true;
}
if ($later !== []) {
$cards = $later;
} else {
break;
}
}
}
}
17 changes: 14 additions & 3 deletions turbo-ext/src/ForHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,20 @@ class ForHandler
zv::Val backEdgeScope = pt_internal_statement_result_loop_back_edge_scope(bodyScopeResult.raw());
if (UNEXPECTED(backEdgeScope.isUndef())) return;
if (backEdgeScope.isNull()) {
bodyScope = std::move(prevScope);
brokeOut = true;
return;
bool bodyIsReplayable;
if (UNEXPECTED(!pt_node_scope_resolver_is_replayable_convergence_body(nodeScopeResolver, stmt, stmtsHold.raw(), bodyIsReplayable))) return;
if (bodyIsReplayable) {
bodyScope = std::move(prevScope);
brokeOut = true;
return;
}
// a nested loop is walked only once in deep context, so the
// back edge it would reach on a later iteration can look dead -
// continue from the end of the body instead of stopping here
zv::Val hold;
zval *resultScope = pt_internal_statement_result_scope(bodyScopeResult.raw(), hold);
if (UNEXPECTED(resultScope == NULL)) return;
backEdgeScope = zv::Val::copyOf(zv::Ref(resultScope));
}
bodyScope = std::move(backEdgeScope);

Expand Down
Loading