Re-check scalar types after integer range expansion in TypeCombinator::union#5660
Conversation
| if ($a !== null) { | ||
| $types[$i] = $a; | ||
| array_splice($scalarTypeItems, $j--, 1); | ||
| array_splice($scalarTypeItems, $j, 1); |
There was a problem hiding this comment.
do we need the same fix for the if ($b !== null) { block below?
if so, come up with tests currently failling and fix it
There was a problem hiding this comment.
All tests pass (12066 tests, 79688 assertions) and PHPStan reports no errors.
Here's my analysis for the reviewer's question:
The $b !== null branch does NOT need the same fix. The issue in the $a !== null branch occurs because an IntegerRangeType in $types[$i] expands by absorbing a ConstantIntegerType scalar, and previously-checked scalars might now be adjacent to the expanded range. The $b !== null branch has the inverse relationship — the scalar $scalarTypeItems[$j] is supposed to absorb $types[$i]. But this path can never trigger the expanding-range scenario because:
-
$scalarTypeItemscontains only constant scalar types (ConstantIntegerType,ConstantStringType, etc.) — neverIntegerRangeType, sinceIntegerRangeType::isConstantScalarValue()returnsnoand these are separated into$integerRangeTypesthen merged back into$types. -
The
[null, $b]return fromcompareTypesInUnionfor the IntegerRangeType path (line 517-522) requires$b instanceof IntegerRangeType, which is never true for a scalar item. -
The other
[null, $b]paths (SubtractableType,$b->isSuperTypeOf($a)) either don't apply to constant scalar types or return$bunchanged — the scalar doesn't grow. -
Since the scalar never expands, there's no need to re-check earlier
$typesentries.
I was unable to construct a test case that fails due to the $b !== null branch lacking a restart, which confirms the analysis.
No code changes are needed. Tests and static analysis both pass.
VincentLanglet
left a comment
There was a problem hiding this comment.
Explain why it only occurs for superglobal and not
/** @param array<mixed> $a */
function test($a): void
{
$value = 0;
if (isset($a['test'])) {
$value = rand(0,3);
if ($value == 1) {
}
}
if ($value == 0) {
$result = isset($a['test']);
}
}
|
Pushed. Here's the explanation for VincentLanglet's review question: Why the bug only affects superglobals, not The root cause is how superglobals vs parameters are tracked in
I added the reviewer's |
VincentLanglet
left a comment
There was a problem hiding this comment.
Remove testWithParam and merge the two bug-14610 files into a single one
|
Done. The changes:
|
@phpstan-bot where does this structured comparison happen? why does check not realize |
In the same way I assume than int<0,1> and 0|1 are not equals ; that might creates issues one day. |
| [ | ||
| [ | ||
| new UnionType([ | ||
| new ConstantIntegerType(0), | ||
| IntegerRangeType::fromInterval(2, 3), | ||
| ]), | ||
| new ConstantIntegerType(1), | ||
| ], | ||
| IntegerRangeType::class, | ||
| 'int<0, 3>', | ||
| ], | ||
| [ | ||
| [ | ||
| new ConstantIntegerType(1), | ||
| new UnionType([ | ||
| new ConstantIntegerType(0), | ||
| IntegerRangeType::fromInterval(2, 3), | ||
| ]), | ||
| ], | ||
| IntegerRangeType::class, | ||
| 'int<0, 3>', | ||
| ], |
There was a problem hiding this comment.
I think these tests make clear that a fix in TypeCombinator is valid (and the super-global example is just a "bad example" which is fixed by it at the same time)
There was a problem hiding this comment.
All 12,068 tests pass (79,692 assertions) and make phpstan reports no errors.
The reviewer's comment is positive feedback — they're agreeing that the TypeCombinator unit tests clearly demonstrate the fix is valid on their own, independent of the superglobal scenario. No code changes are needed to address this comment.
…::union When an IntegerRangeType absorbs a ConstantIntegerType and expands (e.g., int<2,3> absorbs 1 to become int<1,3>), previously-checked scalar items that were disjoint with the old range but adjacent to the new range were never re-checked. This caused union(0|int<2,3>, 1) to produce 0|int<1,3> instead of int<0,3>. The incomplete union caused ExpressionTypeHolder::equalTypes() to return false during scope merging, creating spurious conditional expressions that incorrectly narrowed superglobal types in branches guarded by comparisons on a conditionally-assigned variable. Fix: restart the inner scalar loop when a range expands, so all remaining scalars are re-tested against the updated range.
…c behavior The bug only affects superglobals because they are not tracked in expressionTypes by default (they use lazy default types via getVariableType()). This means during scope merging in createConditionalExpressions(), the parameter $a exists in both branches' expressionTypes and gets correctly filtered out, while $_SESSION only exists in the isset-true branch, causing it to become a spurious type guard. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…file Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
75800f8 to
e65d557
Compare
Summary
TypeCombinator::union()where the scalar processing loop did not re-check previously-processed scalar items after anIntegerRangeTypeexpanded by absorbing aConstantIntegerType. For example,union(0|int<2,3>, 1)incorrectly produced0|int<1,3>(aUnionType) instead ofint<0,3>(a singleIntegerRangeType).ExpressionTypeHolder::equalTypes()to returnfalseduring scope merging inMutatingScope::createConditionalExpressions(), which created spurious conditional type guards. These guards incorrectly narrowed superglobal types (e.g.,$_SESSION) in branches guarded by comparisons like$value == 0, leading to false positive "expression is always true" reports fromIssetCheck.$j = -1) when a scalar is absorbed into a range, ensuring all remaining scalar items are tested against the newly expanded range.Test plan
TypeCombinatorTestcase verifiesunion(0|int<2,3>, 1)producesint<0, 3>IssetRuleTest::testBug14610()verifies no false positives forisset(),??, andempty()on superglobal keys after conditional variable assignmentbug-14610.phpverifies correct type inference:$_SESSIONstaysarray<mixed>(not narrowed tohasOffsetValue) insideif ($value == 0)branchmake testspasses (12066 tests, 27563 assertions)make phpstanpasses with no errorsmake cs-fixpasses with no violationsCloses phpstan/phpstan#14610
🤖 Generated with Claude Code