Skip to content

Commit 73fbad4

Browse files
dmaicherclaude
andauthored
Keep a merged call whose uninformative check leads back to itself (#7088)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f8d6a26 commit 73fbad4

5 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ class MutatingScope implements Scope, NodeCallbackInvoker, CollectedDataEmitter,
163163
*/
164164
private static array $globalConstantFetchKeys = [];
165165

166+
/**
167+
* Keys of the merged calls findUninformativeMergedCalls() is resolving.
168+
* Resolving a call processes its arguments again, and merging a ternary
169+
* among them can make the same call a candidate again, e.g. when
170+
* conditional expressions tie the call's type to the ternary's condition
171+
* after `$s = strlen($s ?: '') ?: 0;`. Such a nested check keeps the
172+
* merged entry instead of resolving the call once more.
173+
*
174+
* @var array<string, true>
175+
*/
176+
private static array $resolvingMergedCalls = [];
177+
166178
/**
167179
* @internal accessed by ScopeOps (native and PHP implementations)
168180
* @var array<string, Type>
@@ -4561,12 +4573,19 @@ private function findUninformativeMergedCalls(array $candidates, array $mergedNa
45614573
{
45624574
$uninformativeKeys = [];
45634575
foreach ($candidates as $exprString => $holder) {
4564-
$expr = $holder->getExpr();
4565-
if (!$holder->getType()->equals($this->getType($expr))) {
4576+
if (isset(self::$resolvingMergedCalls[$exprString])) {
45664577
continue;
45674578
}
4579+
$expr = $holder->getExpr();
45684580
$nativeHolder = $mergedNativeTypes[$exprString] ?? null;
4569-
if ($nativeHolder !== null && !$nativeHolder->getType()->equals($this->getNativeType($expr))) {
4581+
self::$resolvingMergedCalls[$exprString] = true;
4582+
try {
4583+
$isUninformative = $holder->getType()->equals($this->getType($expr))
4584+
&& ($nativeHolder === null || $nativeHolder->getType()->equals($this->getNativeType($expr)));
4585+
} finally {
4586+
unset(self::$resolvingMergedCalls[$exprString]);
4587+
}
4588+
if (!$isUninformative) {
45704589
continue;
45714590
}
45724591
$uninformativeKeys[$exprString] = true;

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,17 @@ public function testBug15520(): void
18821882
}
18831883
}
18841884

1885+
#[RequiresPhp('>= 8.0.0')]
1886+
public function testBug15754(): void
1887+
{
1888+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15754.php');
1889+
$this->assertCount(2, $errors);
1890+
$this->assertSame('Value assigned to variable $s is never read.', $errors[0]->getMessage());
1891+
$this->assertSame(5, $errors[0]->getLine());
1892+
$this->assertSame('Parameter #1 $string of function strlen expects string, int<1, max>|string given.', $errors[1]->getMessage());
1893+
$this->assertSame(23, $errors[1]->getLine());
1894+
}
1895+
18851896
/**
18861897
* @param string[]|null $allAnalysedFiles
18871898
* @return list<Error>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug15754;
4+
5+
function f(?string $s): void { $s = strlen($s ?: "") ?: 0; }
6+
7+
/**
8+
* @return list<string>
9+
*/
10+
function splitIds(?string $ids): array
11+
{
12+
$ids = preg_split('/,/', $ids ?: '', flags: PREG_SPLIT_NO_EMPTY) ?: [];
13+
foreach ($ids as $id) {
14+
echo $id;
15+
}
16+
17+
return $ids;
18+
}
19+
20+
function twice(?string $s): int
21+
{
22+
$s = strlen($s ?: "") ?: 0;
23+
$s = strlen($s ?: "") ?: 0;
24+
25+
return $s;
26+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8600,26 +8600,81 @@ class MutatingScope
86008600
return true;
86018601
}
86028602

8603+
/* MutatingScope::$resolvingMergedCalls - the same static property the twin
8604+
* guards findUninformativeMergedCalls() with; NULL = pending exception */
8605+
[[nodiscard]] static zval *resolvingMergedCallsTable()
8606+
{
8607+
if (UNEXPECTED(pt_ce_mutating_scope == NULL)) {
8608+
zend_throw_error(NULL, "phpstan_turbo: MutatingScope class entry is not available");
8609+
return NULL;
8610+
}
8611+
zval *table = zend_read_static_property(pt_ce_mutating_scope, PT_LC("resolvingMergedCalls"), 0);
8612+
if (UNEXPECTED(table == NULL)) return NULL;
8613+
ZVAL_DEREF(table);
8614+
if (UNEXPECTED(Z_TYPE_P(table) != IS_ARRAY)) {
8615+
zend_throw_error(NULL, "phpstan_turbo: MutatingScope::$resolvingMergedCalls is not an array");
8616+
return NULL;
8617+
}
8618+
return table;
8619+
}
8620+
8621+
/* the twin's `self::$resolvingMergedCalls[$exprString] = true` before
8622+
* resolving a candidate and the `unset()` in its `finally`. The static
8623+
* property's slot stays put, its array does not - nested checks separate
8624+
* and grow it - so it is looked up through the slot on both ends */
8625+
class ResolvingMergedCall
8626+
{
8627+
public:
8628+
ResolvingMergedCall(zval *table, zend_string *key, zend_ulong index) : table(table), key(key), index(index)
8629+
{
8630+
SEPARATE_ARRAY(table);
8631+
zval trueZv;
8632+
ZVAL_TRUE(&trueZv);
8633+
pt_ht_update(Z_ARRVAL_P(table), key, index, &trueZv);
8634+
}
8635+
8636+
~ResolvingMergedCall()
8637+
{
8638+
if (!pt_ht_exists(Z_ARRVAL_P(table), key, index)) return;
8639+
SEPARATE_ARRAY(table);
8640+
pt_ht_del(Z_ARRVAL_P(table), key, index);
8641+
}
8642+
8643+
ResolvingMergedCall(const ResolvingMergedCall &) = delete;
8644+
ResolvingMergedCall &operator=(const ResolvingMergedCall &) = delete;
8645+
8646+
private:
8647+
zval *table;
8648+
zend_string *key;
8649+
zend_ulong index;
8650+
};
8651+
86038652
/* private (twin findUninformativeMergedCalls(), called on the merged scope
86048653
* without the candidates): keys of candidates whose merged type (and
8605-
* native type) is exactly what the call resolves to there */
8654+
* native type) is exactly what the call resolves to there. A candidate
8655+
* that resolving another candidate (or itself) led back to is kept, like
8656+
* the twin's `isset(self::$resolvingMergedCalls[...])` */
86068657
static zv::Val findUninformativeMergedCalls(zv::Val &scope, HashTable *candidates, HashTable *mergedNativeTypes)
86078658
{
86088659
zend_object *scopeObject = requireObject(scope, "getType");
86098660
if (UNEXPECTED(scopeObject == NULL)) return zv::Val();
8661+
zval *resolving = resolvingMergedCallsTable();
8662+
if (UNEXPECTED(resolving == NULL)) return zv::Val();
86108663

86118664
zv::Arr uninformativeKeys = zv::Arr::create(0);
86128665
for (auto entry : zv::TableRef(candidates)) {
8666+
zend_string *key = entry.stringKeyOrNull();
8667+
if (pt_ht_exists(Z_ARRVAL_P(resolving), key, entry.indexKey())) continue;
86138668
zv::Val expr = holderExpr(entry.value());
86148669
if (UNEXPECTED(expr.isUndef())) return zv::Val();
86158670
zv::Val type = holderType(entry.value());
86168671
if (UNEXPECTED(type.isUndef())) return zv::Val();
8672+
ResolvingMergedCall resolvingCall(resolving, key, entry.indexKey());
86178673
zv::Val freshType = pt_mutating_scope_get_type(scopeObject, expr.raw());
86188674
if (UNEXPECTED(freshType.isUndef())) return zv::Val();
86198675
bool equal = pt_call_type_equals(type.raw(), freshType.raw());
86208676
if (UNEXPECTED(EG(exception) != NULL)) return zv::Val();
86218677
if (!equal) continue;
8622-
zend_string *key = entry.stringKeyOrNull();
86238678
zval *nativeHolder = key != NULL ? zend_hash_find(mergedNativeTypes, key) : zend_hash_index_find(mergedNativeTypes, entry.indexKey());
86248679
if (nativeHolder != NULL) {
86258680
zv::Val nativeType = holderType(zv::Ref(nativeHolder));
@@ -12542,6 +12597,9 @@ PT_MINIT_REGISTRATION(pt_register_mutating_scope)
1254212597
* static takes no OBJ_PROP_NUM slot, and keeping it out of the run above
1254312598
* keeps that run a literal transcript of the PT_MS_PROP_* enum */
1254412599
cls.privateStaticTypedArrayPropertyDefaultEmpty("globalConstantFetchKeys");
12600+
/* the twin's `private static array $resolvingMergedCalls = []`, the
12601+
* re-entry guard of findUninformativeMergedCalls() on both sides */
12602+
cls.privateStaticTypedArrayPropertyDefaultEmpty("resolvingMergedCalls");
1254512603
cls.privateClassConstantLong("COMPLEX_UNION_TYPE_MEMBER_LIMIT", PT_MS_COMPLEX_UNION_TYPE_MEMBER_LIMIT);
1254612604
cls.privateClassConstantLong("GLOBAL_CONSTANT_FETCH_KEYS_LIMIT", PT_MS_GLOBAL_CONSTANT_FETCH_KEYS_LIMIT);
1254712605
pt_ms_custom_serialization_methods_list = pt_persistent_string_list(pt_ms_custom_serialization_methods, sizeof(pt_ms_custom_serialization_methods) / sizeof(pt_ms_custom_serialization_methods[0]));

‎turbo-ext/src/generated/MutatingScope.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ inline void declareClass(reg::Class &cls)
4747
inline void declareProperties(reg::Class &cls)
4848
{
4949
cls.property("globalConstantFetchKeys", ZEND_ACC_PRIVATE | ZEND_ACC_STATIC, reg::PropertyKind::TypedEmptyArray, MAY_BE_ARRAY);
50+
cls.property("resolvingMergedCalls", ZEND_ACC_PRIVATE | ZEND_ACC_STATIC, reg::PropertyKind::TypedEmptyArray, MAY_BE_ARRAY);
5051
cls.property("resolvedTypes", ZEND_ACC_PUBLIC, reg::PropertyKind::TypedEmptyArray, MAY_BE_ARRAY);
5152
cls.property("nodeCallbackScope", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedNull, MAY_BE_NULL, "PHPStan\\Analyser\\MutatingScope");
5253
cls.property("namespace", ZEND_ACC_PRIVATE, reg::PropertyKind::Typed, MAY_BE_NULL | MAY_BE_STRING);

0 commit comments

Comments
 (0)