Skip to content

Commit 4a6443d

Browse files
committed
Keep only the current conditionals a replayed statement's recorded walk kept
A statement replayed from a loop's previous pass carried the recorded exit's whole holder list onto the current scope for each expression whose conditional holders the statement changed. When the current holders of that expression differed from the recorded entry's, the recorded ones described the previous pass's state: `$rowCount++` dropping the `$values` holders guarded by `$rowCount` replaced the final walk's `$values` holders with the pass's, such as `$cols` int<1, max> implying `$values` is array{0?: ...}. The while loop's merge with its not-ran path then kept that holder, and `$col = 0` in a later for loop narrowed `$values` to array{0?: ...}. Such an expression now keeps only its current holders that the recorded walk kept too. Amp-Thread-ID: https://ampcode.com/threads/T-01a1deb9-7acb-72e2-bab8-0404b371a021
1 parent c2f6bd9 commit 4a6443d

4 files changed

Lines changed: 140 additions & 16 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
use function abs;
107107
use function array_diff_key;
108108
use function array_filter;
109+
use function array_intersect_key;
109110
use function array_key_exists;
110111
use function array_keys;
111112
use function array_last;
@@ -1515,17 +1516,25 @@ private static function getVariablesOfExpressionKey(string $key): ?array
15151516
public function withRecordedStatementDelta(self $recordedEntry, self $recordedExit): self
15161517
{
15171518
$conditionalExpressions = $this->conditionalExpressions;
1518-
foreach ($recordedExit->conditionalExpressions as $key => $holders) {
1519-
if (isset($recordedEntry->conditionalExpressions[$key]) && $recordedEntry->conditionalExpressions[$key] === $holders) {
1519+
foreach (array_keys($recordedExit->conditionalExpressions + $recordedEntry->conditionalExpressions) as $key) {
1520+
$entryHolders = $recordedEntry->conditionalExpressions[$key] ?? [];
1521+
$exitHolders = $recordedExit->conditionalExpressions[$key] ?? [];
1522+
if ($entryHolders === $exitHolders) {
15201523
continue;
15211524
}
1522-
$conditionalExpressions[$key] = $holders;
1523-
}
1524-
foreach (array_keys($recordedEntry->conditionalExpressions) as $key) {
1525-
if (isset($recordedExit->conditionalExpressions[$key])) {
1525+
$currentHolders = $conditionalExpressions[$key] ?? [];
1526+
if ($currentHolders !== $entryHolders) {
1527+
// the statement changed the holders of an expression whose holders
1528+
// differ here (e.g. it assigned the guard of one): the recorded
1529+
// exit's holders describe the recorded state, so keep only this
1530+
// scope's holders that the recorded walk kept
1531+
$exitHolders = array_intersect_key($currentHolders, $entryHolders, $exitHolders);
1532+
}
1533+
if ($exitHolders === []) {
1534+
unset($conditionalExpressions[$key]);
15261535
continue;
15271536
}
1528-
unset($conditionalExpressions[$key]);
1537+
$conditionalExpressions[$key] = $exitHolders;
15291538
}
15301539

15311540
return $this->duplicateWith(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
/**
9+
* Without bleeding edge, which builds the loop's array shapes differently
10+
* and does not reach the replayed statement this covers.
11+
*/
12+
class Bug15736Test extends TypeInferenceTestCase
13+
{
14+
15+
public static function dataFileAsserts(): iterable
16+
{
17+
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-15736.php');
18+
}
19+
20+
/**
21+
* @param mixed ...$args
22+
*/
23+
#[DataProvider('dataFileAsserts')]
24+
public function testFileAsserts(
25+
string $assertType,
26+
string $file,
27+
...$args,
28+
): void
29+
{
30+
$this->assertFileAsserts($assertType, $file, ...$args);
31+
}
32+
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15736;
4+
5+
use stdClass;
6+
use function PHPStan\Testing\assertType;
7+
8+
/** @return array{value: int}|false */
9+
function fetchRow(object $result)
10+
{
11+
return rand(0, 1) === 1 ? ['value' => 1] : false;
12+
}
13+
14+
function query(): object
15+
{
16+
return new stdClass();
17+
}
18+
19+
function doFoo(object $rows): void
20+
{
21+
$values = [];
22+
$header = 0;
23+
$cols = 0;
24+
$rowCount = 0;
25+
$items = query();
26+
while ($row = fetchRow($rows)) {
27+
$header++;
28+
if ($header === 1) {
29+
while ($item = fetchRow($items)) {
30+
$cols++;
31+
}
32+
}
33+
34+
$col = 0;
35+
$items = query();
36+
while ($item = fetchRow($items)) {
37+
$values[$col][$rowCount] = $item['value'];
38+
$col++;
39+
}
40+
$rowCount++;
41+
}
42+
43+
assertType('array<int<0, max>, non-empty-array<int<0, max>, int>>', $values);
44+
for ($col = 0; $col < $cols; $col++) {
45+
assertType('array<int<0, max>, non-empty-array<int<0, max>, int>>', $values);
46+
for ($r = 0; $r < $rowCount; $r++) {
47+
echo $values[$col][$r];
48+
}
49+
}
50+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,6 +2996,42 @@ class MutatingScope
29962996
return true;
29972997
}
29982998

2999+
/* withRecordedStatementDelta()'s step for one conditional expression
3000+
* key; a missing or null holder list (NULL here) is the twin's `?? []` */
3001+
static void applyRecordedConditionalDelta(zv::Arr &conditionalExpressions, const zv::ArrayEntry &key, zval *entryHolders, zval *exitHolders)
3002+
{
3003+
zval empty;
3004+
ZVAL_EMPTY_ARRAY(&empty);
3005+
if (entryHolders == NULL || Z_TYPE_P(entryHolders) == IS_NULL) entryHolders = &empty;
3006+
if (exitHolders == NULL || Z_TYPE_P(exitHolders) == IS_NULL) exitHolders = &empty;
3007+
if (zend_is_identical(entryHolders, exitHolders)) return;
3008+
3009+
zval *currentHolders = findByEntryKey(conditionalExpressions.table(), key);
3010+
if (currentHolders == NULL || Z_TYPE_P(currentHolders) == IS_NULL) currentHolders = &empty;
3011+
zval kept;
3012+
if (!zend_is_identical(currentHolders, entryHolders)) {
3013+
/* array_intersect_key($currentHolders, $entryHolders, $exitHolders) */
3014+
zv::Arr intersected = zv::Arr::create(0);
3015+
for (auto holder : zv::TableRef(Z_ARRVAL_P(currentHolders))) {
3016+
if (findByEntryKey(Z_ARRVAL_P(entryHolders), holder) == NULL || findByEntryKey(Z_ARRVAL_P(exitHolders), holder) == NULL) continue;
3017+
zval copy;
3018+
ZVAL_COPY(&copy, holder.value().raw());
3019+
updateByEntryKey(intersected.table(), holder, &copy);
3020+
}
3021+
kept = intersected.take();
3022+
} else {
3023+
ZVAL_COPY(&kept, exitHolders);
3024+
}
3025+
3026+
conditionalExpressions.separate();
3027+
if (zend_hash_num_elements(Z_ARRVAL(kept)) == 0) {
3028+
zval_ptr_dtor(&kept);
3029+
deleteByEntryKey(conditionalExpressions.table(), key);
3030+
return;
3031+
}
3032+
updateByEntryKey(conditionalExpressions.table(), key, &kept);
3033+
}
3034+
29993035
/* This scope after a statement whose recorded walk stands. */
30003036
zv::Val withRecordedStatementDelta(zend_object *recordedEntryObject, zend_object *recordedExitObject)
30013037
{
@@ -3009,18 +3045,14 @@ class MutatingScope
30093045
zv::Arr conditionalExpressions = zv::Arr::copyOfTable(Z_ARRVAL_P(slot(PT_MS_PROP_CONDITIONAL_EXPRESSIONS).raw()));
30103046
HashTable *entryConditionals = Z_ARRVAL_P(recordedEntry.slot(PT_MS_PROP_CONDITIONAL_EXPRESSIONS).raw());
30113047
HashTable *exitConditionals = Z_ARRVAL_P(recordedExit.slot(PT_MS_PROP_CONDITIONAL_EXPRESSIONS).raw());
3048+
/* the twin's `$recordedExit + $recordedEntry` key walk: the exit's
3049+
* keys, then the keys only the entry has */
30123050
for (auto entry : zv::TableRef(exitConditionals)) {
3013-
zval *entryHolders = findByEntryKey(entryConditionals, entry);
3014-
if (entryHolders != NULL && Z_TYPE_P(entryHolders) != IS_NULL && zend_is_identical(entryHolders, entry.value().raw())) continue;
3015-
conditionalExpressions.separate();
3016-
zval copy;
3017-
ZVAL_COPY(&copy, entry.value().raw());
3018-
updateByEntryKey(conditionalExpressions.table(), entry, &copy);
3051+
applyRecordedConditionalDelta(conditionalExpressions, entry, findByEntryKey(entryConditionals, entry), entry.value().raw());
30193052
}
30203053
for (auto entry : zv::TableRef(entryConditionals)) {
3021-
if (issetByEntryKey(exitConditionals, entry)) continue;
3022-
conditionalExpressions.separate();
3023-
deleteByEntryKey(conditionalExpressions.table(), entry);
3054+
if (findByEntryKey(exitConditionals, entry) != NULL) continue;
3055+
applyRecordedConditionalDelta(conditionalExpressions, entry, entry.value().raw(), NULL);
30243056
}
30253057

30263058
static const struct { uint32_t slot; const char *name; } holderTables[] = {

0 commit comments

Comments
 (0)