Skip to content

Commit d356a46

Browse files
committed
Keep only the accessories both passes satisfy when widening a type
MutatingScope::generalizeType() intersected the widened type with every accessory of the previous pass's type, also when the new pass's type lacks it: `list<'x'>` widened with `array<int<0, max>, 'x'>` came back as `list<'x'>`, which does not contain the new pass. A convergence then walks back to the new pass's type each time, never reaches a fixpoint, and since the by-ref closures give up at the iteration limit, their by-ref variables became mixed - issue-bot's RunCommand::runPool() failed its own analysis with it. An accessory is now kept only when no type of the new pass may lack it. Amp-Thread-ID: https://ampcode.com/threads/T-01a1976c-1bde-72f8-b96c-072279109d59
1 parent 1a5636a commit d356a46

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6322,10 +6322,20 @@ private function generalizeType(Type $a, Type $b, int $depth, ?Type $entryType =
63226322
$resultTypes[] = TypeCombinator::union(...$floatRanges['b']);
63236323
}
63246324

6325-
$accessoryTypes = array_map(
6326-
static fn (Type $type): Type => $type->generalize(GeneralizePrecision::moreSpecific()),
6327-
TypeUtils::getAccessoryTypes($a),
6328-
);
6325+
// an accessory a type of the other pass may lack would leave that
6326+
// type out of the widened one, and a convergence would keep walking
6327+
// back to it
6328+
$bTypes = $this->flattenUnionForGeneralization($b);
6329+
$accessoryTypes = [];
6330+
foreach (TypeUtils::getAccessoryTypes($a) as $accessoryType) {
6331+
$accessoryType = $accessoryType->generalize(GeneralizePrecision::moreSpecific());
6332+
foreach ($bTypes as $bType) {
6333+
if ($accessoryType->isSuperTypeOf($bType)->maybe()) {
6334+
continue 2;
6335+
}
6336+
}
6337+
$accessoryTypes[] = $accessoryType;
6338+
}
63296339

63306340
$combined = TypeCombinator::union(...$resultTypes, ...$emptyArrays, ...$otherTypes);
63316341
if ($wrapBenevolent) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ByRefClosureListWidening;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
// a pass that writes a list at a computed index makes it an array: widening
8+
// the previous pass's list with it must not bring the list back, or the
9+
// convergence keeps walking back to it until the iteration limit, where the
10+
// by-ref variables keep no type
11+
12+
/**
13+
* @param list<int> $jobs
14+
* @param callable(int): void $jobRunner
15+
*/
16+
function runPool(array $jobs, int $concurrency, callable $jobRunner): void
17+
{
18+
$results = [];
19+
$pending = 0;
20+
$index = 0;
21+
$total = count($jobs);
22+
$rejected = false;
23+
24+
$runNext = static function () use (&$runNext, &$jobs, &$results, &$pending, &$index, &$rejected, $total, $concurrency, $jobRunner): void {
25+
if ($rejected) {
26+
return;
27+
}
28+
while ($pending < $concurrency && $index < $total) {
29+
$currentIndex = $index++;
30+
$pending++;
31+
$jobRunner($jobs[$currentIndex]);
32+
$results[$currentIndex] = 'x';
33+
$pending--;
34+
if (count($results) === $total) {
35+
ksort($results);
36+
} else {
37+
$runNext();
38+
}
39+
}
40+
};
41+
42+
$runNext();
43+
assertType('false', $rejected);
44+
assertType('array<int<0, max>, \'x\'>', $results);
45+
assertType('int<0, max>', $index);
46+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11379,12 +11379,36 @@ class MutatingScope
1137911379
resultTypes.push(std::move(united));
1138011380
}
1138111381

11382+
/* an accessory a type of the other pass may lack is left out (see the twin) */
11383+
zv::Arr bTypesList = zv::Arr::create(4);
11384+
if (UNEXPECTED(!flattenUnionForGeneralization(b, bTypesList))) return zv::Val();
11385+
zv::Val bTypes(std::move(bTypesList));
1138211386
zv::Val accessoryTypes = pt_type_call_static_ce(pt_ce_type_utils, PT_LC("getaccessorytypes"), 1, a);
1138311387
if (UNEXPECTED(accessoryTypes.isUndef() || Z_TYPE_P(accessoryTypes.raw()) != IS_ARRAY)) return zv::Val();
1138411388
zv::Arr generalizedAccessories = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(accessoryTypes.raw())));
1138511389
for (auto accessoryEntry : zv::TableRef(Z_ARRVAL_P(accessoryTypes.raw()))) {
1138611390
zv::Val generalized = generalizeMoreSpecific(accessoryEntry.value().deref().raw());
1138711391
if (UNEXPECTED(generalized.isUndef())) return zv::Val();
11392+
if (UNEXPECTED(Z_TYPE_P(generalized.raw()) != IS_OBJECT)) {
11393+
zend_throw_error(NULL, "Call to a member function isSuperTypeOf() on %s", zend_zval_value_name(generalized.raw()));
11394+
return zv::Val();
11395+
}
11396+
bool lackedByB = false;
11397+
for (auto bEntry : zv::TableRef(Z_ARRVAL_P(bTypes.raw()))) {
11398+
zv::Val superResult = pt_type_op(Z_OBJ_P(generalized.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, bEntry.value().deref().raw());
11399+
if (UNEXPECTED(superResult.isUndef())) return zv::Val();
11400+
if (UNEXPECTED(Z_TYPE_P(superResult.raw()) != IS_OBJECT)) {
11401+
zend_throw_error(NULL, "Call to a member function maybe() on %s", zend_zval_value_name(superResult.raw()));
11402+
return zv::Val();
11403+
}
11404+
zv::Val maybe = pt_type_call(Z_OBJ_P(superResult.raw()), PT_LC("maybe"), 0, NULL);
11405+
if (UNEXPECTED(maybe.isUndef())) return zv::Val();
11406+
if (zend_is_true(maybe.raw())) {
11407+
lackedByB = true;
11408+
break;
11409+
}
11410+
}
11411+
if (lackedByB) continue;
1138811412
generalizedAccessories.push(std::move(generalized));
1138911413
}
1139011414

0 commit comments

Comments
 (0)