Skip to content

Commit f936452

Browse files
committed
Keep only the conditional expressions both passes derived when widening a loop scope
generalizeWith() widened changing variables but kept every holder of the new pass, so a holder whose types grew by one literal per pass (`if $save_item_flag=0 then $items_c is 1|2|...|n` in nested while loops with counters) kept the loop changing until the iteration limit, and every variable it writes became mixed. Intersecting with the previous pass's holders drops the ones still changing; the set can only shrink, so it converges. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c26-db20-7349-9bc4-d608e06a112b
1 parent f19dab4 commit f936452

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,7 +5059,7 @@ private function generalizeWithVariableState(self $otherScope, ?array $writableV
50595059
$this->getNamespace(),
50605060
$variableTypeHolders,
50615061
$nativeTypes,
5062-
self::withoutWidenedConditionalExpressions($otherScope->conditionalExpressions, $otherScope->expressionTypes, $variableTypeHolders),
5062+
self::withoutWidenedConditionalExpressions(ScopeOps::intersectConditionalExpressions($otherScope->conditionalExpressions, $this->conditionalExpressions), $otherScope->expressionTypes, $variableTypeHolders),
50635063
$this->inClosureBindScopeClasses,
50645064
$this->anonymousFunctionReflection,
50655065
$this->inFirstLevelStatement,
@@ -5078,7 +5078,10 @@ private function generalizeWithVariableState(self $otherScope, ?array $writableV
50785078
* Widening replaces a variable's type with a coarser one meant to cover the
50795079
* passes not walked yet. The holders come from the new pass's state: one the
50805080
* previous pass derived may no longer hold, and kept here it would narrow the
5081-
* next pass's entry by itself, never contradicted again. A holder targeting a
5081+
* next pass's entry by itself, never contradicted again. Only the holders
5082+
* both passes derived are kept: one that still changes, like a target type
5083+
* growing by one literal per pass, is dropped as a changing variable is
5084+
* widened, so the holders cannot keep the loop from converging. A holder targeting a
50825085
* widened variable was derived from the type before widening, and applying it
50835086
* would narrow the variable back for the states its guards match - so holders
50845087
* of widened targets are dropped; the next pass derives them again if they

‎tests/PHPStan/Analyser/nsrt/bug-15850.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public function getListenerList(): array {
5656
}
5757
}
5858

59+
function test(): void
60+
{
61+
$items_c = 0;
62+
$save_item_flag = 0;
63+
while (rand(0, 1)) {
64+
while (rand(0, 1)) {
65+
if ($save_item_flag == 0) {
66+
$items_c++;
67+
}
68+
}
69+
$save_item_flag++;
70+
}
71+
assertType('int<0, max>', $items_c);
72+
}
73+
5974
class Node
6075
{
6176
public ?Node $parent = null;

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9440,10 +9440,12 @@ class MutatingScope
94409440
zv::Val nativeTypes = generalizeVariableTypeHolders(Z_ARRVAL_P(slot(PT_MS_PROP_NATIVE_EXPRESSION_TYPES).raw()), otherNativeTypes, writableTable, entryNativeTypes);
94419441
if (UNEXPECTED(nativeTypes.isUndef())) return zv::Val();
94429442

9443-
/* the holders of the new pass's state, as the twin */
9443+
/* the holders of the new pass's state that the previous pass derived too, as the twin */
94449444
HashTable *otherConditionalExpressions = otherTable(otherScope, PT_MS_PROP_CONDITIONAL_EXPRESSIONS, PT_LC("conditionalExpressions"));
9445-
if (UNEXPECTED(otherConditionalExpressions == NULL)) return zv::Val();
9446-
zv::Val conditionalExpressions = withoutWidenedConditionalExpressions(otherConditionalExpressions, otherExpressionTypes, Z_ARRVAL_P(variableTypeHolders.raw()));
9445+
if (UNEXPECTED(otherConditionalExpressions == NULL || !requireSlot(PT_MS_PROP_CONDITIONAL_EXPRESSIONS, "conditionalExpressions"))) return zv::Val();
9446+
zv::Val keptConditionalExpressions = pt_scope_ops_intersect_conditional_expressions(otherConditionalExpressions, Z_ARRVAL_P(slot(PT_MS_PROP_CONDITIONAL_EXPRESSIONS).raw()));
9447+
if (UNEXPECTED(keptConditionalExpressions.isUndef())) return zv::Val();
9448+
zv::Val conditionalExpressions = withoutWidenedConditionalExpressions(Z_ARRVAL_P(keptConditionalExpressions.raw()), otherExpressionTypes, Z_ARRVAL_P(variableTypeHolders.raw()));
94479449
if (UNEXPECTED(conditionalExpressions.isUndef())) return zv::Val();
94489450

94499451
CreateArgs a;

0 commit comments

Comments
 (0)