@@ -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]));
0 commit comments