Skip to content

Commit c3fb17b

Browse files
committed
Keep the tracked offsets of a widened variable that both loop passes track
Widening a variable between loop passes dropped every tracked expression on it, such as $byHash[$hash] after $byHash[$hash]['list'][] = $x widened $byHash. The next pass then no longer knew the offset exists, so the write could start a new array{list: ...} and the shape lost its other keys: array{block: Block, list: list<X>} -> array<'block'|'list', Block|list<X>> with false "Cannot access an offset on Block|list<X>" after it. Nested loops converging in the enclosing loop's passes hit this in shopware, matomo, glpi, openemr, pmmp, phpstan-dba and phpstan-laravel. An expression both passes track is now widened like any other; only one that the other pass does not track is dropped, as before. - bug-10438: $meta[$key] after $meta[$key] = [] is list<string> inside the inner loop, no longer list<string>|string. - turbo-ext: the same in MutatingScope.cpp. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
1 parent 7c7ff95 commit c3fb17b

4 files changed

Lines changed: 108 additions & 30 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,14 +5008,17 @@ private function generalizeVariableTypeHolders(
50085008
$generalizedExpressions = [];
50095009
$newVariableTypeHolders = [];
50105010
foreach ($variableTypeHolders as $variableExprString => $variableTypeHolder) {
5011-
foreach ($generalizedExpressions as $generalizedExprString => $generalizedExpr) {
5012-
if (!ScopeOps::shouldInvalidateExpression($this, $this->services->exprPrinter, $generalizedExprString, $generalizedExpr, $variableTypeHolder->getExpr(), $variableExprString)) {
5013-
continue;
5011+
if (!isset($otherVariableTypeHolders[$variableExprString])) {
5012+
foreach ($generalizedExpressions as $generalizedExprString => $generalizedExpr) {
5013+
if (!ScopeOps::shouldInvalidateExpression($this, $this->services->exprPrinter, $generalizedExprString, $generalizedExpr, $variableTypeHolder->getExpr(), $variableExprString)) {
5014+
continue;
5015+
}
5016+
5017+
// an expression on a widened root that only this pass tracks
5018+
// would keep its precise type
5019+
continue 2;
50145020
}
50155021

5016-
continue 2;
5017-
}
5018-
if (!isset($otherVariableTypeHolders[$variableExprString])) {
50195022
$newVariableTypeHolders[$variableExprString] = $variableTypeHolder;
50205023
continue;
50215024
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function extract(SimpleXMLElement $data, string $type = 'Meta'): array
2525
$meta[$key] = [];
2626
assertType('array{}', $meta[$key]);
2727
foreach ($tag->{$valueName} as $value) {
28-
assertType('list<string>|string', $meta[$key]);
28+
assertType('list<string>', $meta[$key]);
2929
$meta[$key][] = (string)$value;
3030
}
3131
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LoopGeneralizeNestedOffset;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
final class Directive
8+
{
9+
10+
}
11+
12+
final class Block
13+
{
14+
15+
public function getHash(): string
16+
{
17+
return '';
18+
}
19+
20+
/** @return list<Directive> */
21+
public function getPathDirectives(): array
22+
{
23+
return [];
24+
}
25+
26+
}
27+
28+
function topLevel(Block $block, string $hash): void
29+
{
30+
$byHash = [];
31+
if (rand(0, 1)) {
32+
$byHash[$hash] = [
33+
'block' => $block,
34+
'pathDirectives' => [],
35+
];
36+
} else {
37+
$byHash[$hash] = [
38+
'block' => $block,
39+
'pathDirectives' => [],
40+
];
41+
}
42+
foreach ($block->getPathDirectives() as $directive) {
43+
assertType('list<LoopGeneralizeNestedOffset\Directive>', $byHash[$hash]['pathDirectives']);
44+
$byHash[$hash]['pathDirectives'][] = $directive;
45+
}
46+
47+
assertType('non-empty-array<string, array{block: LoopGeneralizeNestedOffset\Block, pathDirectives: list<LoopGeneralizeNestedOffset\Directive>}>', $byHash);
48+
}
49+
50+
/**
51+
* @param array<string, list<Block>> $blocksByDomain
52+
*/
53+
function nested(array $blocksByDomain): void
54+
{
55+
$byHash = [];
56+
foreach ($blocksByDomain as $domain => $blocks) {
57+
foreach ($blocks as $block) {
58+
$hash = $block->getHash();
59+
if (!isset($byHash[$hash])) {
60+
$byHash[$hash] = [
61+
'block' => $block,
62+
'pathDirectives' => [],
63+
];
64+
}
65+
66+
foreach ($block->getPathDirectives() as $directive) {
67+
$byHash[$hash]['pathDirectives'][] = $directive;
68+
}
69+
}
70+
}
71+
72+
assertType('array<string, array{block: LoopGeneralizeNestedOffset\Block, pathDirectives: list<LoopGeneralizeNestedOffset\Directive>}>', $byHash);
73+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9376,31 +9376,33 @@ class MutatingScope
93769376
zend_throw_error(NULL, "phpstan_turbo: an expression type holder has no expression");
93779377
return zv::Val();
93789378
}
9379-
bool invalidated = false;
9380-
for (auto generalized : zv::TableRef(generalizedExpressions.table())) {
9381-
zv::Str generalizedExprString = entryKey(generalized.stringKeyOrNull(), generalized.indexKey());
9382-
bool failed = false;
9383-
bool should = pt_scope_ops_should_invalidate_expression(
9384-
thisZval(),
9385-
exprPrinter.raw(),
9386-
generalizedExprString.get(),
9387-
generalized.value().deref().raw(),
9388-
Z_OBJ_P(variableExpr.raw()),
9389-
variableExprString.get(),
9390-
false,
9391-
NULL,
9392-
false,
9393-
NULL,
9394-
&failed);
9395-
if (UNEXPECTED(failed)) return zv::Val();
9396-
if (!should) continue;
9397-
invalidated = true;
9398-
break;
9399-
}
9400-
if (invalidated) continue;
9401-
94029379
zval *otherHolder = pt_ht_find(otherVariableTypeHolders, entry.key, entry.index);
94039380
if (otherHolder == NULL || Z_TYPE_P(otherHolder) == IS_NULL) {
9381+
bool invalidated = false;
9382+
for (auto generalized : zv::TableRef(generalizedExpressions.table())) {
9383+
zv::Str generalizedExprString = entryKey(generalized.stringKeyOrNull(), generalized.indexKey());
9384+
bool failed = false;
9385+
bool should = pt_scope_ops_should_invalidate_expression(
9386+
thisZval(),
9387+
exprPrinter.raw(),
9388+
generalizedExprString.get(),
9389+
generalized.value().deref().raw(),
9390+
Z_OBJ_P(variableExpr.raw()),
9391+
variableExprString.get(),
9392+
false,
9393+
NULL,
9394+
false,
9395+
NULL,
9396+
&failed);
9397+
if (UNEXPECTED(failed)) return zv::Val();
9398+
if (!should) continue;
9399+
invalidated = true;
9400+
break;
9401+
}
9402+
/* an expression on a widened root that only this pass tracks
9403+
* would keep its precise type */
9404+
if (invalidated) continue;
9405+
94049406
newVariableTypeHolders.set(variableExprString.get(), zv::Val::copyOf(zv::Ref(entry.value)));
94059407
continue;
94069408
}

0 commit comments

Comments
 (0)