From b5542cf1343dddfeaf6f12edf6cf34c808a550a4 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 1/8] Build each value's PhpDoc node once in ConstantArrayType::toPhpDocNode() The key-value and value-only item lists now share the node, so describing a value nested N shapes deep no longer costs 2^N. Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Type/Constant/ConstantArrayType.php | 7 +- .../data/closure-nested-shape-description.php | 73 +++++++++++++++++++ turbo-ext/src/ConstantArrayType.cpp | 4 +- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 tests/bench/data/closure-nested-shape-description.php diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 741dcab08e2..66a30ed9d28 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -3758,15 +3758,18 @@ public function toPhpDocNode(): TypeNode if ($isOptional) { $exportValuesOnly = false; } + // one of the two lists is returned, so they share the value node: describing it + // once per list would describe a value nested N shapes deep 2^N times + $valuePhpDocNode = $valueType->toPhpDocNode(); $items[] = new ArrayShapeItemNode( $keyNode, $isOptional, - $valueType->toPhpDocNode(), + $valuePhpDocNode, ); $values[] = new ArrayShapeItemNode( null, $isOptional, - $valueType->toPhpDocNode(), + $valuePhpDocNode, ); } diff --git a/tests/bench/data/closure-nested-shape-description.php b/tests/bench/data/closure-nested-shape-description.php new file mode 100644 index 00000000000..f4d81311e65 --- /dev/null +++ b/tests/bench/data/closure-nested-shape-description.php @@ -0,0 +1,73 @@ + */ + private array $items; + + /** @param list $items */ + public function __construct(array $items) + { + $this->items = $items; + } + + /** + * @template U + * @param callable(T): U $callback + * @return Collection + */ + public function map(callable $callback): Collection + { + return new Collection(array_map($callback, $this->items)); + } + + /** @return list */ + public function all(): array + { + return $this->items; + } + +} + +function run(): int +{ + $collection = new Collection([1]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + $collection = $collection->map(static fn ($item) => [$item]); + + return count($collection->all()); +} diff --git a/turbo-ext/src/ConstantArrayType.cpp b/turbo-ext/src/ConstantArrayType.cpp index 82811c6ac44..bf65de7da2a 100644 --- a/turbo-ext/src/ConstantArrayType.cpp +++ b/turbo-ext/src/ConstantArrayType.cpp @@ -5868,10 +5868,8 @@ class ConstantArrayType zv::Val item = pt_type_new(PT_CLASS_ARRAY_SHAPE_ITEM_NODE, 3, args); if (UNEXPECTED(item.isUndef())) return zv::Val(); items.push(std::move(item)); - zv::Val valueNodeAgain = pt_type_call(Z_OBJ_P(valueType), PT_LC("tophpdocnode"), 0, NULL); - if (UNEXPECTED(valueNodeAgain.isUndef())) return zv::Val(); + /* one of the two lists is returned, so they share the value node */ ZVAL_NULL(&args[0]); - ZVAL_COPY_VALUE(&args[2], valueNodeAgain.raw()); zv::Val valueItem = pt_type_new(PT_CLASS_ARRAY_SHAPE_ITEM_NODE, 3, args); if (UNEXPECTED(valueItem.isUndef())) return zv::Val(); values.push(std::move(valueItem)); From a84060b1fbf91f7c70dfd176c3516a7b54e9be65 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 2/8] Resume nested loop liveness from the previous head when it is covered VariableLivenessResolver remembers the last next set, context and head of each loop; an outer pass whose next set and context cover them starts the inner loop's iteration from that head instead of from scratch. Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Analyser/VariableLivenessResolver.php | 64 +++++++++++++ tests/bench/data/nested-loops-liveness.php | 89 +++++++++++++++++++ turbo-ext/src/VariableLivenessResolver.cpp | 66 ++++++++++++++ .../src/generated/VariableLivenessResolver.h | 31 +++++-- 4 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 tests/bench/data/nested-loops-liveness.php diff --git a/src/Analyser/VariableLivenessResolver.php b/src/Analyser/VariableLivenessResolver.php index 24225d45efa..18c5006b116 100644 --- a/src/Analyser/VariableLivenessResolver.php +++ b/src/Analyser/VariableLivenessResolver.php @@ -118,6 +118,9 @@ final class VariableLivenessResolver private bool $returnsByReference = false; + /** @var array, VariableFlowContext, array}> loop flow id => the live sets after it and the head they resolved to */ + private array $loopHeads = []; + private function __construct() { } @@ -342,7 +345,18 @@ private function liveBefore(?VariableFlow $flow, array $next, VariableFlowContex return $next + $names; } if ($flow->kind === VariableFlow::LOOP) { + // Liveness only grows with the live sets after the loop, so the head the loop + // resolved to for smaller sets is below the new one and the iteration can start + // from it. Each pass of an enclosing loop asks again with sets at least as large, + // so a loop nested N deep is no longer walked 2^N times. $head = []; + $flowId = spl_object_id($flow); + if (isset($this->loopHeads[$flowId])) { + [$previousNext, $previousContext, $previousHead] = $this->loopHeads[$flowId]; + if (self::covers($next, $previousNext) && self::contextCovers($context, $previousContext)) { + $head = $previousHead; + } + } do { $previousCount = count($head); $update = $this->liveBefore($flow->children[2], $head, $context); @@ -351,6 +365,7 @@ private function liveBefore(?VariableFlow $flow, array $next, VariableFlowContex $afterCondition = $flow->canRepeat ? ($flow->canExit ? $body + $next : $body) : $next; $head = $this->liveBefore($flow->children[0], $afterCondition, $context); } while (count($head) !== $previousCount); + $this->loopHeads[$flowId] = [$next, $context, $head]; return $flow->atLeastOnce ? $this->liveBefore($flow->children[0], $body, $context) : $head; } @@ -721,6 +736,55 @@ private function killOverwriteMarkers(VariableWrite $write, array $next): array return self::withoutKeys($next, $markersBySlot[$slot] ?? []); } + /** + * @param array $set + * @param array $subset + */ + private static function covers(array $set, array $subset): bool + { + if (count($set) < count($subset)) { + return false; + } + foreach (array_keys($subset) as $key) { + if (!isset($set[$key])) { + return false; + } + } + + return true; + } + + /** Whether each destination of $context covers the same one of $subcontext. */ + private static function contextCovers(VariableFlowContext $context, VariableFlowContext $subcontext): bool + { + if ( + count($context->breaks) !== count($subcontext->breaks) + || count($context->continues) !== count($subcontext->continues) + || count($context->catches) !== count($subcontext->catches) + || !self::covers($context->return, $subcontext->return) + || !self::covers($context->uncaught, $subcontext->uncaught) + ) { + return false; + } + foreach ($subcontext->breaks as $i => $destination) { + if (!self::covers($context->breaks[$i], $destination)) { + return false; + } + } + foreach ($subcontext->continues as $i => $destination) { + if (!self::covers($context->continues[$i], $destination)) { + return false; + } + } + foreach ($subcontext->catches as $i => [$type, $destination]) { + if ($context->catches[$i][0] !== $type || !self::covers($context->catches[$i][1], $destination)) { + return false; + } + } + + return true; + } + /** * Walks the smaller of the two sets. * diff --git a/tests/bench/data/nested-loops-liveness.php b/tests/bench/data/nested-loops-liveness.php new file mode 100644 index 00000000000..289df7de8ca --- /dev/null +++ b/tests/bench/data/nested-loops-liveness.php @@ -0,0 +1,89 @@ + +#include #include #include @@ -468,10 +469,66 @@ struct Context return result; } + Context share() const + { + Context copy; + copy.return_ = shareArray(return_); + copy.breaks = shareAll(breaks); + copy.continues = shareAll(continues); + copy.catches = shareCatches(catches); + copy.uncaught = shareArray(uncaught); + return copy; + } + + /* Mirrors covers(): every key of $subset is in $set */ + static bool covers(const zv::Val &set, const zv::Val &subset) + { + if (countOf(set) < countOf(subset)) return false; + HashTable *setTable = tableOf(set); + for (auto entry : zv::TableRef(tableOf(subset))) { + zend_string *key = entry.stringKeyOrNull(); + if (key != NULL ? !zend_hash_exists(setTable, key) : !zend_hash_index_exists(setTable, entry.indexKey())) { + return false; + } + } + return true; + } + + static bool coversAll(const std::vector &sets, const std::vector &subsets) + { + if (sets.size() != subsets.size()) return false; + for (size_t i = 0; i < sets.size(); i++) { + if (!covers(sets[i], subsets[i])) return false; + } + return true; + } + + /* Mirrors contextCovers(): each destination covers the same one of $subcontext */ + bool covers(const Context &subcontext) const + { + if (catches.size() != subcontext.catches.size()) return false; + if (!covers(return_, subcontext.return_) || !covers(uncaught, subcontext.uncaught)) return false; + if (!coversAll(breaks, subcontext.breaks) || !coversAll(continues, subcontext.continues)) return false; + for (size_t i = 0; i < catches.size(); i++) { + if (Z_OBJ_P(catches[i].first) != Z_OBJ_P(subcontext.catches[i].first) || !covers(catches[i].second, subcontext.catches[i].second)) { + return false; + } + } + return true; + } + private: Context(const Context &) = delete; }; +/* the live sets after a loop and the head they resolved to */ +struct LoopHead +{ + zv::Val next; + Context context; + zv::Val head; +}; + } // namespace namespace phpstanturbo { @@ -611,6 +668,7 @@ class VariableLivenessResolver bool readsAllVariables = false; bool allNamesMentioned = false; bool returnsByReference = false; + std::unordered_map loopHeads; /* loop flow handle => its last resolution */ VariableLivenessResolver() = default; @@ -1015,7 +1073,14 @@ class VariableLivenessResolver zval *condition = childTable != NULL ? childAt(childTable, 0) : NULL; zval *bodyFlow = childTable != NULL ? childAt(childTable, 1) : NULL; zval *updateFlow = childTable != NULL ? childAt(childTable, 2) : NULL; + // Liveness only grows with the live sets after the loop, so the head the loop + // resolved to for smaller sets is below the new one and the iteration can start + // from it. zv::Val head = emptyArray(); + auto previous = loopHeads.find(flow->handle); + if (previous != loopHeads.end() && Context::covers(next, previous->second.next) && context.covers(previous->second.context)) { + head = shareArray(previous->second.head); + } zv::Val body; uint32_t previousCount; do { @@ -1036,6 +1101,7 @@ class VariableLivenessResolver head = liveBefore(condition, std::move(afterCondition), context); if (UNEXPECTED(head.isUndef())) return zv::Val(); } while (countOf(head) != previousCount); + loopHeads[flow->handle] = LoopHead{shareArray(next), context.share(), shareArray(head)}; return zend_is_true(atLeastOnce) ? liveBefore(condition, std::move(body), context) : std::move(head); } diff --git a/turbo-ext/src/generated/VariableLivenessResolver.h b/turbo-ext/src/generated/VariableLivenessResolver.h index 421254b07fd..18a99b7d258 100644 --- a/turbo-ext/src/generated/VariableLivenessResolver.h +++ b/turbo-ext/src/generated/VariableLivenessResolver.h @@ -39,6 +39,7 @@ inline constexpr uint32_t opaque = 25; inline constexpr uint32_t readsAllVariables = 26; inline constexpr uint32_t allNamesMentioned = 27; inline constexpr uint32_t returnsByReference = 28; +inline constexpr uint32_t loopHeads = 29; } // namespace slot inline void declareClass(reg::Class &cls) @@ -78,6 +79,7 @@ inline void declareProperties(reg::Class &cls) cls.property("readsAllVariables", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedBool, 0); cls.property("allNamesMentioned", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedBool, 0); cls.property("returnsByReference", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedBool, 0); + cls.property("loopHeads", ZEND_ACC_PRIVATE, reg::PropertyKind::TypedEmptyArray, MAY_BE_ARRAY); } /* the string and parameter tables the signatures below index into (see reg::Sig) */ @@ -115,10 +117,15 @@ inline constexpr char strings[] = "observeWrite\0" /* 376 */ "observeOverwrite\0" /* 389 */ "killOverwriteMarkers\0" /* 406 */ - "keys\0" /* 427 */ - "withoutKeys\0" /* 432 */ - "resolveDependencies\0" /* 444 */ - "resolveCoverage"; /* 464 */ + "set\0" /* 427 */ + "subset\0" /* 431 */ + "covers\0" /* 438 */ + "subcontext\0" /* 445 */ + "contextCovers\0" /* 456 */ + "keys\0" /* 470 */ + "withoutKeys\0" /* 475 */ + "resolveDependencies\0" /* 487 */ + "resolveCoverage"; /* 507 */ inline constexpr reg::PackedArg args[] = { reg::packed(12, 0, 21), /* resolve $function */ reg::packed(49, MAY_BE_NULL, 54), /* resolve $flow */ @@ -154,8 +161,14 @@ inline constexpr reg::PackedArg args[] = { reg::packed(262, 0, 202), /* killOverwriteMarkers $write */ reg::packed(133, MAY_BE_ARRAY), /* killOverwriteMarkers $next */ reg::packed(84, MAY_BE_ARRAY), /* killOverwriteMarkers return */ + reg::packed(427, MAY_BE_ARRAY), /* covers $set */ + reg::packed(431, MAY_BE_ARRAY), /* covers $subset */ + reg::packed(84, MAY_BE_BOOL), /* covers return */ + reg::packed(138, 0, 146), /* contextCovers $context */ + reg::packed(445, 0, 146), /* contextCovers $subcontext */ + reg::packed(84, MAY_BE_BOOL), /* contextCovers return */ reg::packed(133, MAY_BE_ARRAY), /* withoutKeys $next */ - reg::packed(427, MAY_BE_ARRAY), /* withoutKeys $keys */ + reg::packed(470, MAY_BE_ARRAY), /* withoutKeys $keys */ reg::packed(84, MAY_BE_ARRAY), /* withoutKeys return */ reg::packed(84, MAY_BE_VOID), /* resolveDependencies return */ reg::packed(84, MAY_BE_VOID), /* resolveCoverage return */ @@ -178,9 +191,11 @@ inline constexpr sigtab::Sig replacedSlot = { { 360 /* replacedSlot */, 1, 23, 1 inline constexpr sigtab::Sig observeWrite = { { 376 /* observeWrite */, 2, 25, 2, 27, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig observeOverwrite = { { 389 /* observeOverwrite */, 2, 28, 2, 30, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig killOverwriteMarkers = { { 406 /* killOverwriteMarkers */, 2, 31, 2, 33, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig withoutKeys = { { 432 /* withoutKeys */, 2, 34, 2, 36, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; -inline constexpr sigtab::Sig resolveDependencies = { { 444 /* resolveDependencies */, 0, 37, 0, 37, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig resolveCoverage = { { 464 /* resolveCoverage */, 0, 38, 0, 38, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig covers = { { 438 /* covers */, 2, 34, 2, 36, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig contextCovers = { { 456 /* contextCovers */, 2, 37, 2, 39, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig withoutKeys = { { 475 /* withoutKeys */, 2, 40, 2, 42, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig resolveDependencies = { { 487 /* resolveDependencies */, 0, 43, 0, 43, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig resolveCoverage = { { 507 /* resolveCoverage */, 0, 44, 0, 44, ZEND_ACC_PRIVATE } }; } // namespace sig } // namespace ptdecl::VariableLivenessResolver From bc700377f8a98572a642d9d43ab7ec4c4548c4a1 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 3/8] Factor shared prefixes out of flow choices and memoize nullsafe non-null narrowing VariableFlow::choice() of a sequence and its own first flow becomes a sequence of that flow and the optional rest, and the nullsafe property fetch and method call handlers ask once per link whether it did not short-circuit. Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- .../ExprHandler/NullsafeMethodCallHandler.php | 18 +- .../NullsafePropertyFetchHandler.php | 18 +- src/Analyser/VariableFlow.php | 16 ++ tests/bench/data/nullsafe-chain-narrowing.php | 231 ++++++++++++++++++ turbo-ext/src/NullsafeMethodCallHandler.cpp | 46 +++- .../src/NullsafePropertyFetchHandler.cpp | 46 +++- turbo-ext/src/VariableFlow.cpp | 39 +++ 7 files changed, 404 insertions(+), 10 deletions(-) create mode 100644 tests/bench/data/nullsafe-chain-narrowing.php diff --git a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php index ebbc5946959..92f2eb7c541 100644 --- a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php +++ b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php @@ -136,6 +136,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $receiverResult = $processedReceiverResult; // lazily memoized receiver-is-null branch scope of the decomposition $leftFalseyScope = null; + // "this link did not short-circuit", by $nativeTypesPromoted: the link after this one + // asks for it twice, through its plain twin's receiver and through its own receiver + // term, so without it a chain N links deep would narrow its first link 2^N times + $notNullTypes = []; return $this->expressionResultFactory->create( $scope, @@ -193,13 +197,18 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc // Inside-out copy of TypeSpecifier::createForExpr()'s `?->` handling. // The short-circuit's null surfaces here, never by walking the chain: // a receiver that is itself a ?-> composes through the parent handler. - createTypesCallback: function (Type $type, TypeSpecifierContext $context, bool $nativeTypesPromoted) use ($expr, $methodCall, $exprResult, $receiverResult, $nullsafeTypeCallback, $beforeScope): SpecifiedTypes { + createTypesCallback: function (Type $type, TypeSpecifierContext $context, bool $nativeTypesPromoted) use ($expr, $methodCall, $exprResult, $receiverResult, $nullsafeTypeCallback, $beforeScope, &$notNullTypes): SpecifiedTypes { // null() context: createForExpr never computes $containsNull and // emits no entry for the subject - behave the same. if ($context->null()) { return (new SpecifiedTypes())->setRootExpr($expr); } + $isNotNull = $type::class === NullType::class && $context === TypeSpecifierContext::createFalse(); + if ($isNotNull && isset($notNullTypes[(int) $nativeTypesPromoted])) { + return $notNullTypes[(int) $nativeTypesPromoted]; + } + $s = $nativeTypesPromoted ? $beforeScope->doNotTreatPhpDocTypesAsCertain() : $beforeScope; $nullsafeType = $nullsafeTypeCallback($nativeTypesPromoted); if ($context->true()) { @@ -220,10 +229,15 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc // double-key), and "receiver is not null". // the receiver composes through its own result so a nullsafe // receiver fans "not null" down its whole chain - return $this->defaultNarrowingHelper->createSubjectTypes($s, $methodCall, $exprResult, $type, $context) + $types = $this->defaultNarrowingHelper->createSubjectTypes($s, $methodCall, $exprResult, $type, $context) ->unionWith($this->defaultNarrowingHelper->createSubjectTypes($s, $expr, null, $type, $context)) ->unionWith($this->defaultNarrowingHelper->createSubjectTypes($s, $expr->var, $receiverResult, new NullType(), TypeSpecifierContext::createFalse())) ->setRootExpr($expr); + if ($isNotNull) { + $notNullTypes[(int) $nativeTypesPromoted] = $types; + } + + return $types; }, dependencies: Dependencies::merge($exprResult->getDependencies(), $processedReceiverResult->getDependencies()), ); diff --git a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php index b54f05529d5..76c1b34ded2 100644 --- a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php +++ b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php @@ -114,6 +114,10 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $receiverResult = $processedReceiverResult; // lazily memoized receiver-is-null branch scope of the decomposition $leftFalseyScope = null; + // "this link did not short-circuit", by $nativeTypesPromoted: the link after this one + // asks for it twice, through its plain twin's receiver and through its own receiver + // term, so without it a chain N links deep would narrow its first link 2^N times + $notNullTypes = []; return $this->expressionResultFactory->create( $scope, @@ -171,13 +175,18 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc // Inside-out copy of TypeSpecifier::createForExpr()'s `?->` handling. // The short-circuit's null surfaces here, never by walking the chain: // a receiver that is itself a ?-> composes through the parent handler. - createTypesCallback: function (Type $type, TypeSpecifierContext $context, bool $nativeTypesPromoted) use ($expr, $propertyFetch, $exprResult, $receiverResult, $nullsafeTypeCallback, $beforeScope): SpecifiedTypes { + createTypesCallback: function (Type $type, TypeSpecifierContext $context, bool $nativeTypesPromoted) use ($expr, $propertyFetch, $exprResult, $receiverResult, $nullsafeTypeCallback, $beforeScope, &$notNullTypes): SpecifiedTypes { // null() context: createForExpr never computes $containsNull and // emits no entry for the subject - behave the same. if ($context->null()) { return (new SpecifiedTypes())->setRootExpr($expr); } + $isNotNull = $type::class === NullType::class && $context === TypeSpecifierContext::createFalse(); + if ($isNotNull && isset($notNullTypes[(int) $nativeTypesPromoted])) { + return $notNullTypes[(int) $nativeTypesPromoted]; + } + $s = $nativeTypesPromoted ? $beforeScope->doNotTreatPhpDocTypesAsCertain() : $beforeScope; $nullsafeType = $nullsafeTypeCallback($nativeTypesPromoted); if ($context->true()) { @@ -198,10 +207,15 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc // double-key), and "receiver is not null". // the receiver composes through its own result so a nullsafe // receiver fans "not null" down its whole chain - return $this->defaultNarrowingHelper->createSubjectTypes($s, $propertyFetch, $exprResult, $type, $context) + $types = $this->defaultNarrowingHelper->createSubjectTypes($s, $propertyFetch, $exprResult, $type, $context) ->unionWith($this->defaultNarrowingHelper->createSubjectTypes($s, $expr, null, $type, $context)) ->unionWith($this->defaultNarrowingHelper->createSubjectTypes($s, $expr->var, $receiverResult, new NullType(), TypeSpecifierContext::createFalse())) ->setRootExpr($expr); + if ($isNotNull) { + $notNullTypes[(int) $nativeTypesPromoted] = $types; + } + + return $types; }, dependencies: Dependencies::merge($exprResult->getDependencies(), $processedReceiverResult->getDependencies()), ); diff --git a/src/Analyser/VariableFlow.php b/src/Analyser/VariableFlow.php index 158d1ec2305..78d009da4b9 100644 --- a/src/Analyser/VariableFlow.php +++ b/src/Analyser/VariableFlow.php @@ -8,6 +8,7 @@ use PHPStan\Node\Variable\VariableWrite; use PHPStan\Turbo\ShadowedByTurboExtension; use PHPStan\Type\Type; +use function array_slice; use function count; use function in_array; @@ -74,6 +75,21 @@ public static function choice(?self ...$branches): ?self if (count($branches) === 1 || (count($branches) === 2 && $branches[0] === $branches[1])) { return $branches[0]; } + if (count($branches) === 2) { + // Both branches start with the same flow, like a `?->` and its short circuit that + // both evaluate the receiver: it runs once before the choice of the rest, or the + // receiver of a chain N links deep would be walked 2^N times. + foreach ([[$branches[0], $branches[1]], [$branches[1], $branches[0]]] as [$longer, $prefix]) { + if ( + $prefix !== null + && $longer instanceof VariableSequenceFlow + && $longer->kind === self::SEQUENCE + && $longer->children[0] === $prefix + ) { + return self::sequence($prefix, new VariableSequenceFlow(self::CHOICE, [self::sequence(...array_slice($longer->children, 1)), null])); + } + } + } return new VariableSequenceFlow(self::CHOICE, $branches); } diff --git a/tests/bench/data/nullsafe-chain-narrowing.php b/tests/bench/data/nullsafe-chain-narrowing.php new file mode 100644 index 00000000000..8c8ffdfa41b --- /dev/null +++ b/tests/bench/data/nullsafe-chain-narrowing.php @@ -0,0 +1,231 @@ +next; + } + +} + +final class Link1 +{ + + public ?Link2 $next = null; + + public function next(): ?Link2 + { + return $this->next; + } + +} + +final class Link2 +{ + + public ?Link3 $next = null; + + public function next(): ?Link3 + { + return $this->next; + } + +} + +final class Link3 +{ + + public ?Link4 $next = null; + + public function next(): ?Link4 + { + return $this->next; + } + +} + +final class Link4 +{ + + public ?Link5 $next = null; + + public function next(): ?Link5 + { + return $this->next; + } + +} + +final class Link5 +{ + + public ?Link6 $next = null; + + public function next(): ?Link6 + { + return $this->next; + } + +} + +final class Link6 +{ + + public ?Link7 $next = null; + + public function next(): ?Link7 + { + return $this->next; + } + +} + +final class Link7 +{ + + public ?Link8 $next = null; + + public function next(): ?Link8 + { + return $this->next; + } + +} + +final class Link8 +{ + + public ?Link9 $next = null; + + public function next(): ?Link9 + { + return $this->next; + } + +} + +final class Link9 +{ + + public ?Link10 $next = null; + + public function next(): ?Link10 + { + return $this->next; + } + +} + +final class Link10 +{ + + public ?Link11 $next = null; + + public function next(): ?Link11 + { + return $this->next; + } + +} + +final class Link11 +{ + + public ?Link12 $next = null; + + public function next(): ?Link12 + { + return $this->next; + } + +} + +final class Link12 +{ + + public ?Link13 $next = null; + + public function next(): ?Link13 + { + return $this->next; + } + +} + +final class Link13 +{ + + public ?Link14 $next = null; + + public function next(): ?Link14 + { + return $this->next; + } + +} + +final class Link14 +{ + + public ?Link15 $next = null; + + public function next(): ?Link15 + { + return $this->next; + } + +} + +final class Link15 +{ + + public ?Link16 $next = null; + + public function next(): ?Link16 + { + return $this->next; + } + +} + +final class Link16 +{ + + public int $value = 1; + +} + +/** + * A chain of 16 `?->` links, as property fetches and as method calls. Each + * link's variable flow is the choice between the fetch, which evaluates the + * receiver first, and the short circuit, which evaluates only the receiver, + * so the receiver's flow was in both branches and VariableLivenessResolver + * walked the first link 2^N times. Narrowing the chain not null asked each + * link for "the receiver is not null" twice, through the plain fetch's + * receiver and through its own receiver term, which was exponential too. + */ +function run(Link0 $link): int +{ + echo $link?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->value; + if ($link?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next !== null) { + echo $link?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next?->next->value; + } + echo $link?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->value; + if ($link?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next() !== null) { + return $link?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()?->next()->value; + } + + return 0; +} diff --git a/turbo-ext/src/NullsafeMethodCallHandler.cpp b/turbo-ext/src/NullsafeMethodCallHandler.cpp index e1ec35da861..e98bd1d4aee 100644 --- a/turbo-ext/src/NullsafeMethodCallHandler.cpp +++ b/turbo-ext/src/NullsafeMethodCallHandler.cpp @@ -11,7 +11,7 @@ * $nodeScopeResolver and the by-reference $leftFalseyScope memo — a reference * zval shared with the left-falsey-scope closure), the createTypesCallback * ($this, $expr, $methodCall, $exprResult, $receiverResult, - * $nullsafeTypeCallback, $beforeScope) and, inside the specifyTypesCallback, + * $nullsafeTypeCallback, $beforeScope and the by-reference $notNullTypes memo) and, inside the specifyTypesCallback, * the $leftTypes / $rightTypes / scope closures handed to * BooleanNarrowingHelper::specifyConjunction(). * @@ -311,7 +311,29 @@ class NullsafeMethodCallHandler ZVAL_COPY_VALUE(&captures[8], leftFalseyScope.raw()); specifyTypesCallback = pt_native_closure_new(&specifyTypesCallbackBody, 9, captures, 1u << 8); } - zv::Val createTypesCallback = pt_native_closure(&createTypesCallbackBody, self, expr, methodCall.raw(), exprResult.raw(), receiverResult, nullsafeTypeCallback.raw(), beforeScope); + // "this link did not short-circuit", by $nativeTypesPromoted (a reference the + // createTypesCallback fills) + zv::Val notNullTypes; + { + zval empty; + ZVAL_EMPTY_ARRAY(&empty); + zval reference; + ZVAL_NEW_REF(&reference, &empty); + notNullTypes = zv::Val::adopt(reference); + } + zv::Val createTypesCallback; + { + zval captures[8]; + ZVAL_OBJ(&captures[0], self); + ZVAL_COPY_VALUE(&captures[1], expr); + ZVAL_COPY_VALUE(&captures[2], methodCall.raw()); + ZVAL_COPY_VALUE(&captures[3], exprResult.raw()); + ZVAL_COPY_VALUE(&captures[4], receiverResult); + ZVAL_COPY_VALUE(&captures[5], nullsafeTypeCallback.raw()); + ZVAL_COPY_VALUE(&captures[6], beforeScope); + ZVAL_COPY_VALUE(&captures[7], notNullTypes.raw()); + createTypesCallback = pt_native_closure_new(&createTypesCallbackBody, 8, captures, 1u << 7); + } zv::Val exprDependenciesHold, receiverDependenciesHold; zval *exprDependencies = pt_expression_result_dependencies(exprResult.raw(), exprDependenciesHold); @@ -590,7 +612,7 @@ class NullsafeMethodCallHandler * $nativeTypesPromoted) use ($expr, $methodCall, $exprResult, * $receiverResult, $nullsafeTypeCallback, $beforeScope): SpecifiedTypes — * captures: $this, $expr, $methodCall, $exprResult, $receiverResult, - * $nullsafeTypeCallback, $beforeScope */ + * $nullsafeTypeCallback, $beforeScope, &$notNullTypes */ static void createTypesCallbackBody(zval *captures, uint32_t argc, zval *argv, zval *return_value) { if (UNEXPECTED(!requireArguments(argc, 3, pt_nmch_closure_name))) return; @@ -618,6 +640,17 @@ class NullsafeMethodCallHandler return; } + // the memoized "did not short-circuit" a link after this one asks for twice + bool isNotNull = Z_TYPE_P(type) == IS_OBJECT && Z_OBJCE_P(type) == pt_ce_null_type && Z_TYPE_P(context) == IS_OBJECT && Z_OBJ_P(context) == pt_type_specifier_context_create_false(); + zend_ulong notNullIndex = nativeTypesPromoted ? 1 : 0; + if (isNotNull) { + zval *cached = zend_hash_index_find(Z_ARRVAL_P(Z_REFVAL(captures[7])), notNullIndex); + if (cached != NULL) { + ZVAL_COPY(return_value, cached); + return; + } + } + zv::Val promotedScope; zval *s = beforeScope; if (nativeTypesPromoted) { @@ -695,6 +728,13 @@ class NullsafeMethodCallHandler if (UNEXPECTED(united.isUndef())) return; zv::Val result = setRootExpr(united, expr); if (UNEXPECTED(result.isUndef())) return; + if (isNotNull) { + zval *memo = Z_REFVAL(captures[7]); + SEPARATE_ARRAY(memo); + zval stored; + ZVAL_COPY(&stored, result.raw()); + zend_hash_index_update(Z_ARRVAL_P(memo), notNullIndex, &stored); + } result.intoReturnValue(return_value); } diff --git a/turbo-ext/src/NullsafePropertyFetchHandler.cpp b/turbo-ext/src/NullsafePropertyFetchHandler.cpp index 484f20acfd1..582dd37adb1 100644 --- a/turbo-ext/src/NullsafePropertyFetchHandler.cpp +++ b/turbo-ext/src/NullsafePropertyFetchHandler.cpp @@ -11,7 +11,7 @@ * $nodeScopeResolver and the by-reference $leftFalseyScope memo — a reference * zval shared with the left-falsey-scope closure), the createTypesCallback * ($this, $expr, $propertyFetch, $exprResult, $receiverResult, - * $nullsafeTypeCallback, $beforeScope) and, inside the specifyTypesCallback, + * $nullsafeTypeCallback, $beforeScope and the by-reference $notNullTypes memo) and, inside the specifyTypesCallback, * the $leftTypes / $rightTypes / scope closures handed to * BooleanNarrowingHelper::specifyConjunction(). * @@ -282,7 +282,29 @@ class NullsafePropertyFetchHandler ZVAL_COPY_VALUE(&captures[8], leftFalseyScope.raw()); specifyTypesCallback = pt_native_closure_new(&specifyTypesCallbackBody, 9, captures, 1u << 8); } - zv::Val createTypesCallback = pt_native_closure(&createTypesCallbackBody, self, expr, propertyFetch.raw(), exprResult.raw(), receiverResult, nullsafeTypeCallback.raw(), beforeScope); + // "this link did not short-circuit", by $nativeTypesPromoted (a reference the + // createTypesCallback fills) + zv::Val notNullTypes; + { + zval empty; + ZVAL_EMPTY_ARRAY(&empty); + zval reference; + ZVAL_NEW_REF(&reference, &empty); + notNullTypes = zv::Val::adopt(reference); + } + zv::Val createTypesCallback; + { + zval captures[8]; + ZVAL_OBJ(&captures[0], self); + ZVAL_COPY_VALUE(&captures[1], expr); + ZVAL_COPY_VALUE(&captures[2], propertyFetch.raw()); + ZVAL_COPY_VALUE(&captures[3], exprResult.raw()); + ZVAL_COPY_VALUE(&captures[4], receiverResult); + ZVAL_COPY_VALUE(&captures[5], nullsafeTypeCallback.raw()); + ZVAL_COPY_VALUE(&captures[6], beforeScope); + ZVAL_COPY_VALUE(&captures[7], notNullTypes.raw()); + createTypesCallback = pt_native_closure_new(&createTypesCallbackBody, 8, captures, 1u << 7); + } zv::Val exprDependenciesHold, receiverDependenciesHold; zval *exprDependencies = pt_expression_result_dependencies(exprResult.raw(), exprDependenciesHold); @@ -559,7 +581,7 @@ class NullsafePropertyFetchHandler * $nativeTypesPromoted) use ($expr, $propertyFetch, $exprResult, * $receiverResult, $nullsafeTypeCallback, $beforeScope): SpecifiedTypes — * captures: $this, $expr, $propertyFetch, $exprResult, $receiverResult, - * $nullsafeTypeCallback, $beforeScope */ + * $nullsafeTypeCallback, $beforeScope, &$notNullTypes */ static void createTypesCallbackBody(zval *captures, uint32_t argc, zval *argv, zval *return_value) { if (UNEXPECTED(!requireArguments(argc, 3, pt_npfh_closure_name))) return; @@ -587,6 +609,17 @@ class NullsafePropertyFetchHandler return; } + // the memoized "did not short-circuit" a link after this one asks for twice + bool isNotNull = Z_TYPE_P(type) == IS_OBJECT && Z_OBJCE_P(type) == pt_ce_null_type && Z_TYPE_P(context) == IS_OBJECT && Z_OBJ_P(context) == pt_type_specifier_context_create_false(); + zend_ulong notNullIndex = nativeTypesPromoted ? 1 : 0; + if (isNotNull) { + zval *cached = zend_hash_index_find(Z_ARRVAL_P(Z_REFVAL(captures[7])), notNullIndex); + if (cached != NULL) { + ZVAL_COPY(return_value, cached); + return; + } + } + zv::Val promotedScope; zval *s = beforeScope; if (nativeTypesPromoted) { @@ -664,6 +697,13 @@ class NullsafePropertyFetchHandler if (UNEXPECTED(united.isUndef())) return; zv::Val result = setRootExpr(united, expr); if (UNEXPECTED(result.isUndef())) return; + if (isNotNull) { + zval *memo = Z_REFVAL(captures[7]); + SEPARATE_ARRAY(memo); + zval stored; + ZVAL_COPY(&stored, result.raw()); + zend_hash_index_update(Z_ARRVAL_P(memo), notNullIndex, &stored); + } result.intoReturnValue(return_value); } diff --git a/turbo-ext/src/VariableFlow.cpp b/turbo-ext/src/VariableFlow.cpp index 30ed11f930f..dc04d8347ec 100644 --- a/turbo-ext/src/VariableFlow.cpp +++ b/turbo-ext/src/VariableFlow.cpp @@ -17,12 +17,14 @@ #include "support.h" #include "generated/VariableFlow.h" +#include "generated/VariableSequenceFlow.h" namespace sigs = ptdecl::VariableFlow::sig; #include "zv.h" #include "TypeTraits.h" #include +#include zend_class_entry *pt_ce_variable_flow; @@ -232,6 +234,43 @@ class VariableFlow { if (argc == 0) return zv::Val::null(); if (argc == 1 || (argc == 2 && zend_is_identical(&argv[0], &argv[1]))) return zv::Val::copyOf(zv::Ref(&argv[0])); + if (argc == 2) { + // both branches start with the same flow, like a `?->` and its short + // circuit that both evaluate the receiver: it runs once before the + // choice of the rest + for (int longerIndex = 0; longerIndex < 2; longerIndex++) { + zval *longer = &argv[longerIndex]; + zval *prefix = &argv[1 - longerIndex]; + if (Z_TYPE_P(prefix) != IS_OBJECT || Z_TYPE_P(longer) != IS_OBJECT || Z_OBJCE_P(longer) != pt_ce_variable_sequence_flow) continue; + zval *kind = OBJ_PROP_NUM(Z_OBJ_P(longer), ptdecl::VariableFlow::slot::kind); + zval *children = OBJ_PROP_NUM(Z_OBJ_P(longer), ptdecl::VariableSequenceFlow::slot::children); + if (Z_TYPE_P(kind) != IS_STRING || !zend_string_equals(Z_STR_P(kind), pt_vf_kind_strings[PT_VF_SEQUENCE]) || Z_TYPE_P(children) != IS_ARRAY) continue; + HashTable *childTable = Z_ARRVAL_P(children); + zval *first = zend_hash_index_find(childTable, 0); + if (first == NULL || Z_TYPE_P(first) != IS_OBJECT || Z_OBJ_P(first) != Z_OBJ_P(prefix)) continue; + + /* self::sequence(...array_slice($longer->children, 1)) */ + uint32_t restCount = zend_hash_num_elements(childTable) - 1; + std::vector rest(restCount); + for (uint32_t i = 0; i < restCount; i++) { + zval *child = zend_hash_index_find(childTable, i + 1); + if (child == NULL) { + ZVAL_NULL(&rest[i]); + } else { + ZVAL_COPY_VALUE(&rest[i], child); + } + } + zv::Val restFlow = sequence(restCount, rest.data()); + if (UNEXPECTED(restFlow.isUndef())) return zv::Val(); + zv::Arr restBranches = zv::Arr::create(2); + restBranches.push(zv::Ref(restFlow.raw())); + restBranches.push(zv::Val::null()); + zv::Val restChoice = newSequenceFlow(PT_VF_CHOICE, restBranches.raw()); + if (UNEXPECTED(restChoice.isUndef())) return zv::Val(); + zv::Args sequenceArgv{prefix, restChoice.raw()}; + return sequence(2, sequenceArgv); + } + } zv::Arr branches = zv::Arr::create(argc); for (uint32_t i = 0; i < argc; i++) { From f5447e54fcb6676ed697c7423413a7f89792627a Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 4/8] Visit each class once in CrossCheckInterfacesHelper and cache generic parent classes Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Reflection/ClassReflection.php | 8 +- .../Generics/CrossCheckInterfacesHelper.php | 13 +- .../bench/data/generic-inheritance-chain.php | 163 ++++++++++++++++++ turbo-ext/src/ClassReflection.cpp | 22 ++- 4 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 tests/bench/data/generic-inheritance-chain.php diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index e685b5e681b..553f68f9c8a 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -277,16 +277,18 @@ public function getParentClass(): ?ClassReflection ); } + // a generic parent is a new reflection, remembered so that walking up a generic + // hierarchy does not resolve each ancestor again from every class below it if (!$extendedType instanceof GenericObjectType) { - return $this->reflectionProvider->getClass($parentClass->getName()); + return $this->cachedParentClass = $this->reflectionProvider->getClass($parentClass->getName()); } - return $extendedType->getClassReflection() ?? $this->reflectionProvider->getClass($parentClass->getName()); + return $this->cachedParentClass = $extendedType->getClassReflection() ?? $this->reflectionProvider->getClass($parentClass->getName()); } $parentReflection = $this->reflectionProvider->getClass($parentClass->getName()); if ($parentReflection->isGeneric()) { - return $parentReflection->withTypes( + return $this->cachedParentClass = $parentReflection->withTypes( array_values($parentReflection->getTemplateTypeMap()->map(static fn (): Type => new ErrorType())->getTypes()), ); } diff --git a/src/Rules/Generics/CrossCheckInterfacesHelper.php b/src/Rules/Generics/CrossCheckInterfacesHelper.php index 25b57e10e62..098b0611a4a 100644 --- a/src/Rules/Generics/CrossCheckInterfacesHelper.php +++ b/src/Rules/Generics/CrossCheckInterfacesHelper.php @@ -21,7 +21,18 @@ public function check(ClassReflection $classReflection): array { $interfaceTemplateTypeMaps = []; $errors = []; - $check = static function (ClassReflection $classReflection, bool $first) use (&$interfaceTemplateTypeMaps, &$check, &$errors): void { + // Each ancestor is reached once from every class below it, and a class seen again + // compares the same interfaces with the same maps, repeating only its errors - visiting + // it every time was exponential in the depth of the hierarchy. + $visited = []; + $check = static function (ClassReflection $classReflection, bool $first) use (&$interfaceTemplateTypeMaps, &$check, &$errors, &$visited): void { + if (!$first) { + if (array_key_exists($classReflection->getName(), $visited)) { + return; + } + $visited[$classReflection->getName()] = true; + } + foreach ($classReflection->getInterfaces() as $interface) { if (!$interface->isGeneric()) { continue; diff --git a/tests/bench/data/generic-inheritance-chain.php b/tests/bench/data/generic-inheritance-chain.php new file mode 100644 index 00000000000..54e1ac59dbc --- /dev/null +++ b/tests/bench/data/generic-inheritance-chain.php @@ -0,0 +1,163 @@ + + */ +class Level0 implements Container +{ + + /** @var T */ + private $value; + + /** @param T $value */ + public function __construct($value) + { + $this->value = $value; + } + + /** @return T */ + public function get() + { + return $this->value; + } + +} + +/** + * @template T + * @extends Level0 + */ +class Level1 extends Level0 +{ +} + +/** + * @template T + * @extends Level1 + */ +class Level2 extends Level1 +{ +} + +/** + * @template T + * @extends Level2 + */ +class Level3 extends Level2 +{ +} + +/** + * @template T + * @extends Level3 + */ +class Level4 extends Level3 +{ +} + +/** + * @template T + * @extends Level4 + */ +class Level5 extends Level4 +{ +} + +/** + * @template T + * @extends Level5 + */ +class Level6 extends Level5 +{ +} + +/** + * @template T + * @extends Level6 + */ +class Level7 extends Level6 +{ +} + +/** + * @template T + * @extends Level7 + */ +class Level8 extends Level7 +{ +} + +/** + * @template T + * @extends Level8 + */ +class Level9 extends Level8 +{ +} + +/** + * @template T + * @extends Level9 + */ +class Level10 extends Level9 +{ +} + +/** + * @template T + * @extends Level10 + */ +class Level11 extends Level10 +{ +} + +/** + * @template T + * @extends Level11 + */ +class Level12 extends Level11 +{ +} + +/** + * @template T + * @extends Level12 + */ +class Level13 extends Level12 +{ +} + +/** + * Fourteen generic classes, each extending the one before with its template + * type. ClassAncestorsRule cross-checks the interfaces of every class and + * reached each ancestor once from every class below it, which was + * exponential in the depth of the hierarchy, and ClassReflection resolved a + * generic parent again on every getParentClass() call, so every walk up the + * hierarchy resolved each ancestor anew. + */ +function run(): int +{ + $value = new Level13(1); + + return $value->get(); +} diff --git a/turbo-ext/src/ClassReflection.cpp b/turbo-ext/src/ClassReflection.cpp index 805e7c5c378..9f24245bbb1 100644 --- a/turbo-ext/src/ClassReflection.cpp +++ b/turbo-ext/src/ClassReflection.cpp @@ -987,14 +987,19 @@ class ClassReflection if (UNEXPECTED(extendedType.isUndef())) return zv::Val(); } + // a generic parent is a new reflection, remembered so that walking up a + // generic hierarchy does not resolve each ancestor again + zv::Val reflection; if (!instanceOfShadowed(extendedType.ref(), pt_ce_generic_object_type, PT_LC(PT_CR_GENERIC_OBJECT_TYPE_NAME))) { - return providerGetClass(parentNameStr); + reflection = providerGetClass(parentNameStr); + } else { + reflection = typeGetClassReflection(extendedType.ref()); + if (UNEXPECTED(reflection.isUndef())) return zv::Val(); + if (reflection.isNull()) reflection = providerGetClass(parentNameStr); } - - zv::Val reflection = typeGetClassReflection(extendedType.ref()); if (UNEXPECTED(reflection.isUndef())) return zv::Val(); - if (!reflection.isNull()) return reflection; - return providerGetClass(parentNameStr); + writeSlot(PT_CR_PROP_CACHED_PARENT_CLASS, zv::Val::copyOf(reflection.ref())); + return reflection; } } @@ -1002,7 +1007,12 @@ class ClassReflection if (UNEXPECTED(parentReflection.isUndef())) return zv::Val(); bool parentGeneric; if (UNEXPECTED(!crIsGeneric(parentReflection.ref(), parentGeneric))) return zv::Val(); - if (parentGeneric) return crWithErrorTypes(parentReflection.ref()); + if (parentGeneric) { + zv::Val withErrorTypes = crWithErrorTypes(parentReflection.ref()); + if (UNEXPECTED(withErrorTypes.isUndef())) return zv::Val(); + writeSlot(PT_CR_PROP_CACHED_PARENT_CLASS, zv::Val::copyOf(withErrorTypes.ref())); + return withErrorTypes; + } writeSlot(PT_CR_PROP_CACHED_PARENT_CLASS, zv::Val::copyOf(parentReflection.ref())); From dd7726af1ac44761796ceff0c419ad26643e3817 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 5/8] Answer UnionType::accepts() of another union through isAcceptedBy() alone The member-by-member check cannot come out yes unless the compound answer does, which replaced its result anyway; asking both walked nested members twice at every level. Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Type/UnionType.php | 8 ++++ .../data/nested-nullable-shape-accepts.php | 39 +++++++++++++++++++ turbo-ext/src/UnionType.cpp | 12 ++++++ 3 files changed, 59 insertions(+) create mode 100644 tests/bench/data/nested-nullable-shape-accepts.php diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 99b46164c63..47dde9a3af1 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -306,6 +306,14 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult break; } + if ($type instanceof self && !$type instanceof TemplateType) { + // A member accepts the other union only when it accepts each of its members, which + // this union then accepts too - the member-by-member or() below cannot come out + // yes unless the compound answer does, and that answer replaces any other. Asking + // both would walk the members' nested types twice at every level. + return $type->isAcceptedBy($this, $strictTypes); + } + $innerAccepts = []; $result = AcceptsResult::createNo(); foreach ($this->getSortedTypes() as $i => $innerType) { diff --git a/tests/bench/data/nested-nullable-shape-accepts.php b/tests/bench/data/nested-nullable-shape-accepts.php new file mode 100644 index 00000000000..72e4fdced6d --- /dev/null +++ b/tests/bench/data/nested-nullable-shape-accepts.php @@ -0,0 +1,39 @@ + $p */ +function h(array $p): void +{ +} + +/** @param array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: array{a: string}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null}|null $p */ +function k($p): void +{ +} diff --git a/turbo-ext/src/UnionType.cpp b/turbo-ext/src/UnionType.cpp index 1e6c36967e6..2a84e9de948 100644 --- a/turbo-ext/src/UnionType.cpp +++ b/turbo-ext/src/UnionType.cpp @@ -645,6 +645,18 @@ class UnionType break; } + /* $type instanceof self && !$type instanceof TemplateType: the + * member-by-member or() comes out yes only when the compound answer + * does, which replaces any other */ + if (instanceof_function(Z_OBJCE_P(type), pt_ce_union_type)) { + bool isTemplate; + if (UNEXPECTED(!isInstance(type, PT_CLASS_TEMPLATE_TYPE, isTemplate))) return zv::Val(); + if (!isTemplate) { + zv::Args args{&selfZv, &strictZv}; + return pt_type_call(Z_OBJ_P(type), PT_LC("isacceptedby"), 2, args); + } + } + zv::Val sorted = thisGetSortedTypes(); if (UNEXPECTED(sorted.isUndef())) return zv::Val(); zv::Arr innerAccepts = zv::Arr::create(zv::ArrRef(sorted.raw()).size()); From d2339be09360c98a2c1aac10e706c4d8abd6c147 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:27:54 +0000 Subject: [PATCH 6/8] Skip unread conditions when deriving conditional expressions over arithmetic When the assigned expression is arithmetic, concatenation or bitwise operators over variables, literals and constants, conditions on variables it does not read are not assigned, and the derived type of a consequent without remaining conditions is computed once. Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Analyser/ExprHandler/AssignHandler.php | 70 +++- tests/bench/data/loop-derived-assignments.php | 317 ++++++++++++++++++ turbo-ext/src/AssignHandler.cpp | 73 +++- 3 files changed, 451 insertions(+), 9 deletions(-) create mode 100644 tests/bench/data/loop-derived-assignments.php diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index dfdd499dea0..c8a90353565 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -2396,6 +2396,19 @@ private function processDerivedConditionalExpressionsAfterAssign( $seenReadExprStrings = []; /** @var Variable[] $readVariables */ $readVariables = (new NodeFinder())->findInstanceOf([$assignedExpr], Variable::class); + // arithmetic over variables reads nothing but their types: a condition on another + // variable cannot change what it evaluates to and is not assigned, and with no + // condition left the result depends on the consequent alone + $readVariableNames = null; + if (self::readsOnlyItsVariables($pricedExpr)) { + $readVariableNames = []; + foreach ($readVariables as $readVariable) { + if (!is_string($readVariable->name)) { + continue; + } + $readVariableNames['$' . $readVariable->name] = true; + } + } foreach ($readVariables as $readVariable) { if (!is_string($readVariable->name) || $readVariable->name === $variableName) { continue; @@ -2406,6 +2419,7 @@ private function processDerivedConditionalExpressionsAfterAssign( } $seenReadExprStrings[$readExprString] = true; + $derivedTypesByConsequent = []; foreach ($scopeConditionalExpressions[$readExprString] ?? [] as $holder) { $consequent = $holder->getTypeHolder(); if (!$consequent->getCertainty()->yes()) { @@ -2413,21 +2427,42 @@ private function processDerivedConditionalExpressionsAfterAssign( } $conditionHolders = $holder->getConditionExpressionTypeHolders(); - $evalScope = $scope; + $assignedConditionHolders = []; foreach ($conditionHolders as $conditionExprString => $conditionHolder) { if ($conditionExprString === $targetExprString || !$conditionHolder->getCertainty()->yes()) { // a condition on the just-overwritten variable is stale continue 2; } - $evalScope = $evalScope->assignExpression($conditionHolder->getExpr(), $conditionHolder->getType(), $conditionHolder->getType()); + if ($readVariableNames !== null && $conditionHolder->getExpr() instanceof Variable && !isset($readVariableNames[$conditionExprString])) { + continue; + } + $assignedConditionHolders[] = $conditionHolder; } if (++$evaluations > self::DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT) { return $conditionalExpressions; } - $evalScope = $evalScope->assignExpression($consequent->getExpr(), $consequent->getType(), $consequent->getType()); - $derivedType = $nodeScopeResolver->readScopeStateOrSyntheticType($pricedExpr, $evalScope); + $derivedType = null; + if ($readVariableNames !== null && count($assignedConditionHolders) === 0) { + foreach ($derivedTypesByConsequent as [$consequentType, $consequentDerivedType]) { + if ($consequentType->equals($consequent->getType())) { + $derivedType = $consequentDerivedType; + break; + } + } + } + if ($derivedType === null) { + $evalScope = $scope; + foreach ($assignedConditionHolders as $conditionHolder) { + $evalScope = $evalScope->assignExpression($conditionHolder->getExpr(), $conditionHolder->getType(), $conditionHolder->getType()); + } + $evalScope = $evalScope->assignExpression($consequent->getExpr(), $consequent->getType(), $consequent->getType()); + $derivedType = $nodeScopeResolver->readScopeStateOrSyntheticType($pricedExpr, $evalScope); + if ($readVariableNames !== null && count($assignedConditionHolders) === 0) { + $derivedTypesByConsequent[] = [$consequent->getType(), $derivedType]; + } + } if ($derivedType->equals($assignedType)) { continue; } @@ -2443,6 +2478,33 @@ private function processDerivedConditionalExpressionsAfterAssign( return $conditionalExpressions; } + /** + * Whether the type of $expr is read from the types of the variables it mentions alone: + * arithmetic, concatenation and bitwise operators over named variables, literals and + * constants - nothing in it is read in a narrowed scope or calls anything that could + * look up another variable. + */ + private static function readsOnlyItsVariables(Expr $expr): bool + { + if ($expr instanceof Variable) { + return is_string($expr->name); + } + if ($expr instanceof Node\Scalar) { + return !$expr instanceof Node\Scalar\InterpolatedString; + } + if ($expr instanceof Expr\ConstFetch) { + return true; + } + if ($expr instanceof Expr\UnaryMinus || $expr instanceof Expr\UnaryPlus || $expr instanceof Expr\Cast) { + return self::readsOnlyItsVariables($expr->expr); + } + if ($expr instanceof Expr\BinaryOp && !$expr instanceof Expr\BinaryOp\Coalesce && in_array($expr::class, self::ASSIGN_OP_BINARY_OPS, true)) { + return self::readsOnlyItsVariables($expr->left) && self::readsOnlyItsVariables($expr->right); + } + + return false; + } + /** * Records the reverse implication of a `$var = in_array($needle, [...known values])` * assignment: a needle that is one of the haystack's always-present values makes the diff --git a/tests/bench/data/loop-derived-assignments.php b/tests/bench/data/loop-derived-assignments.php new file mode 100644 index 00000000000..28fc03573b8 --- /dev/null +++ b/tests/bench/data/loop-derived-assignments.php @@ -0,0 +1,317 @@ + #include +#include zend_class_entry *pt_ce_assign_handler = nullptr; @@ -2682,6 +2683,23 @@ class AssignHandler } /* (twin 2267) */ + /* readsOnlyItsVariables(): arithmetic, concatenation and bitwise + * operators (the AssignOp ones but ??) over named variables, literals and + * constants */ + static bool readsOnlyItsVariables(zval *expr) + { + if (ahIs(expr, PT_CLASS_VARIABLE)) return ahVariableName(expr) != NULL; + if (ahIs(expr, PT_CLASS_SCALAR)) return !ahIs(expr, PT_CLASS_INTERPOLATED_STRING); + if (ahIs(expr, PT_CLASS_CONST_FETCH)) return true; + if (ahIs(expr, PT_CLASS_UNARY_MINUS) || ahIs(expr, PT_CLASS_UNARY_PLUS) || ahIs(expr, PT_CLASS_CAST_EXPR)) return readsOnlyItsVariables(AH_PROP(expr, expr)); + if (Z_TYPE_P(expr) != IS_OBJECT) return false; + for (const pt_ah_assign_op_binary_op &entry : pt_ah_assign_op_binary_ops) { + if (entry.binaryOpClass == PT_CLASS_COALESCE_EXPR || Z_OBJCE_P(expr) != pt_class(entry.binaryOpClass)) continue; + return readsOnlyItsVariables(AH_PROP(expr, left)) && readsOnlyItsVariables(AH_PROP(expr, right)); + } + return false; + } + [[nodiscard]] static bool processDerivedConditionalExpressionsAfterAssign(zval *nsr, zval *scope, zend_string *variableName, zv::Val &conditionalExpressions, zval *assignedExpr, zval *assignedType, zval *rhsImpurePoints) { if (zend_hash_num_elements(Z_ARRVAL_P(rhsImpurePoints)) > 0) return true; @@ -2712,6 +2730,21 @@ class AssignHandler zend_long evaluations = 0; zv::Val seenReadExprStrings = ahEmptyArray(); AH_VALB(readVariables, nodeFinderFindVariables(assignedExpr)); + // arithmetic over variables reads nothing but their types: a condition on another + // variable cannot change what it evaluates to and is not assigned, and with no + // condition left the result depends on the consequent alone + zv::Val readVariableNames; /* UNDEF = every condition is assigned */ + if (readsOnlyItsVariables(pricedExpr.raw())) { + readVariableNames = ahEmptyArray(); + for (auto readEntry : zv::TableRef(Z_ARRVAL_P(readVariables.raw()))) { + zend_string *readName = ahVariableName(readEntry.value().deref().raw()); + if (readName == NULL) continue; + zv::Str readExprString = zv::Str::adopt(zend_string_concat2(PT_LC("$"), ZSTR_VAL(readName), ZSTR_LEN(readName))); + zval trueZv; + ZVAL_TRUE(&trueZv); + ahSetKey(readVariableNames, readExprString.get(), &trueZv); + } + } for (auto readEntry : zv::TableRef(Z_ARRVAL_P(readVariables.raw()))) { zval *readVariable = readEntry.value().deref().raw(); zend_string *readName = ahVariableName(readVariable); @@ -2726,13 +2759,15 @@ class AssignHandler if (holders == NULL) continue; ZVAL_DEREF(holders); if (Z_TYPE_P(holders) != IS_ARRAY) continue; + /* $derivedTypesByConsequent: [consequent type, derived type] pairs */ + std::vector> derivedTypesByConsequent; for (auto holderEntry : zv::TableRef(Z_ARRVAL_P(holders))) { zval *holder = holderEntry.value().deref().raw(); zval *consequent = holderTypeHolder(holder); if (pt_holder_certainty_value(Z_OBJ_P(consequent)) != PT_TRI_YES) continue; zval *conditionHolders = holderConditions(holder); - zv::Val evalScope = zv::Val::copyOf(zv::Ref(scope)); + std::vector assignedConditionHolders; bool stale = false; for (auto conditionEntry : zv::TableRef(Z_ARRVAL_P(conditionHolders))) { zval *conditionHolder = conditionEntry.value().deref().raw(); @@ -2742,16 +2777,44 @@ class AssignHandler stale = true; break; } - zval *conditionType = OBJ_PROP_NUM(Z_OBJ_P(conditionHolder), PT_ETH_PROP_TYPE); - AH_SETB(evalScope, pt_mutating_scope_assign_expression(Z_OBJ_P(evalScope.raw()), Z_OBJ_P(OBJ_PROP_NUM(Z_OBJ_P(conditionHolder), PT_ETH_PROP_EXPR)), conditionType, conditionType)); + if (!readVariableNames.isUndef() + && ahIs(OBJ_PROP_NUM(Z_OBJ_P(conditionHolder), PT_ETH_PROP_EXPR), PT_CLASS_VARIABLE) + && (conditionExprString != NULL + ? zend_symtable_find(Z_ARRVAL_P(readVariableNames.raw()), conditionExprString) + : zend_hash_index_find(Z_ARRVAL_P(readVariableNames.raw()), conditionEntry.indexKey())) == NULL) { + continue; + } + assignedConditionHolders.push_back(conditionHolder); } if (stale) continue; if (++evaluations > PT_AH_DERIVED_CONDITIONAL_EXPRESSIONS_LIMIT) return true; zval *consequentType = OBJ_PROP_NUM(Z_OBJ_P(consequent), PT_ETH_PROP_TYPE); - AH_SETB(evalScope, pt_mutating_scope_assign_expression(Z_OBJ_P(evalScope.raw()), Z_OBJ_P(OBJ_PROP_NUM(Z_OBJ_P(consequent), PT_ETH_PROP_EXPR)), consequentType, consequentType)); - AH_VALB(derivedType, nsrReadScopeStateOrSyntheticType(nsr, pricedExpr.raw(), evalScope.raw())); + const bool memoizable = !readVariableNames.isUndef() && assignedConditionHolders.empty(); + zv::Val derivedType; + if (memoizable) { + for (auto &memo : derivedTypesByConsequent) { + bool sameConsequent; + AH_OKB(ahEquals(memo.first, consequentType, sameConsequent)); + if (sameConsequent) { + derivedType = zv::Val::copyOf(zv::Ref(memo.second.raw())); + break; + } + } + } + if (derivedType.isUndef()) { + zv::Val evalScope = zv::Val::copyOf(zv::Ref(scope)); + for (zval *conditionHolder : assignedConditionHolders) { + zval *conditionType = OBJ_PROP_NUM(Z_OBJ_P(conditionHolder), PT_ETH_PROP_TYPE); + AH_SETB(evalScope, pt_mutating_scope_assign_expression(Z_OBJ_P(evalScope.raw()), Z_OBJ_P(OBJ_PROP_NUM(Z_OBJ_P(conditionHolder), PT_ETH_PROP_EXPR)), conditionType, conditionType)); + } + AH_SETB(evalScope, pt_mutating_scope_assign_expression(Z_OBJ_P(evalScope.raw()), Z_OBJ_P(OBJ_PROP_NUM(Z_OBJ_P(consequent), PT_ETH_PROP_EXPR)), consequentType, consequentType)); + AH_SETB(derivedType, nsrReadScopeStateOrSyntheticType(nsr, pricedExpr.raw(), evalScope.raw())); + if (memoizable) { + derivedTypesByConsequent.emplace_back(consequentType, zv::Val::copyOf(zv::Ref(derivedType.raw()))); + } + } bool equal; AH_OKB(ahEquals(derivedType.raw(), assignedType, equal)); if (equal) continue; From 2715e8920975b2557060ff199018013a039d1d5b Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:44:03 +0000 Subject: [PATCH 7/8] fixup! Factor shared prefixes out of flow choices and memoize nullsafe non-null narrowing Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Analyser/ExprHandler/NullsafeMethodCallHandler.php | 3 ++- src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php index 92f2eb7c541..f019850a348 100644 --- a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php +++ b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php @@ -32,6 +32,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use function array_merge; +use function get_class; /** * @implements ExprHandler @@ -204,7 +205,7 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc return (new SpecifiedTypes())->setRootExpr($expr); } - $isNotNull = $type::class === NullType::class && $context === TypeSpecifierContext::createFalse(); + $isNotNull = get_class($type) === NullType::class && $context === TypeSpecifierContext::createFalse(); if ($isNotNull && isset($notNullTypes[(int) $nativeTypesPromoted])) { return $notNullTypes[(int) $nativeTypesPromoted]; } diff --git a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php index 76c1b34ded2..77d6d9ec7ae 100644 --- a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php +++ b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php @@ -31,6 +31,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use function array_merge; +use function get_class; /** * @implements ExprHandler @@ -182,7 +183,7 @@ static function () use ($beforeScope, $leftTypes, &$leftFalseyScope): MutatingSc return (new SpecifiedTypes())->setRootExpr($expr); } - $isNotNull = $type::class === NullType::class && $context === TypeSpecifierContext::createFalse(); + $isNotNull = get_class($type) === NullType::class && $context === TypeSpecifierContext::createFalse(); if ($isNotNull && isset($notNullTypes[(int) $nativeTypesPromoted])) { return $notNullTypes[(int) $nativeTypesPromoted]; } From 5f80130cb97132bc397b11c3175ca48bfbd7cf68 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 14:44:04 +0000 Subject: [PATCH 8/8] fixup! Skip unread conditions when deriving conditional expressions over arithmetic Amp-Thread-ID: https://ampcode.com/threads/T-01a1debc-f897-7034-af27-e4609439c823 --- src/Analyser/ExprHandler/AssignHandler.php | 3 +- turbo-ext/src/generated/AssignHandler.h | 164 +++++++++++---------- 2 files changed, 86 insertions(+), 81 deletions(-) diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index c8a90353565..426d4aa65f1 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -103,6 +103,7 @@ use function array_reverse; use function array_slice; use function count; +use function get_class; use function in_array; use function is_float; use function is_int; @@ -2498,7 +2499,7 @@ private static function readsOnlyItsVariables(Expr $expr): bool if ($expr instanceof Expr\UnaryMinus || $expr instanceof Expr\UnaryPlus || $expr instanceof Expr\Cast) { return self::readsOnlyItsVariables($expr->expr); } - if ($expr instanceof Expr\BinaryOp && !$expr instanceof Expr\BinaryOp\Coalesce && in_array($expr::class, self::ASSIGN_OP_BINARY_OPS, true)) { + if ($expr instanceof Expr\BinaryOp && !$expr instanceof Expr\BinaryOp\Coalesce && in_array(get_class($expr), self::ASSIGN_OP_BINARY_OPS, true)) { return self::readsOnlyItsVariables($expr->left) && self::readsOnlyItsVariables($expr->right); } diff --git a/turbo-ext/src/generated/AssignHandler.h b/turbo-ext/src/generated/AssignHandler.h index 0a543efddc3..fb74810fef9 100644 --- a/turbo-ext/src/generated/AssignHandler.h +++ b/turbo-ext/src/generated/AssignHandler.h @@ -191,43 +191,44 @@ inline constexpr char strings[] = "processMatchForConditionalExpressionsAfterAssign\0" /* 2973 */ "assignedType\0" /* 3022 */ "processDerivedConditionalExpressionsAfterAssign\0" /* 3035 */ - "PhpParser\\Node\\Expr\\FuncCall\0" /* 3083 */ - "processInArrayForConditionalExpressionsAfterAssign\0" /* 3112 */ - "isExprSafeToProjectThroughVariable\0" /* 3163 */ - "dimFetchStack\0" /* 3198 */ - "PHPStan\\Analyser\\Scope\0" /* 3212 */ - "isImplicitArrayCreation\0" /* 3235 */ - "rootVarName\0" /* 3259 */ - "arrayExpr\0" /* 3271 */ - "PhpParser\\Node\\Expr\\Array_\0" /* 3281 */ - "parentExpr\0" /* 3308 */ - "processArrayByRefItems\0" /* 3319 */ - "implicitIndex\0" /* 3342 */ - "processArrayByRefItemsWithImplicitIndex\0" /* 3356 */ - "isFlattenableUnpackedArray\0" /* 3396 */ - "index\0" /* 3423 */ - "advanceImplicitIndex\0" /* 3429 */ - "offsetTypes\0" /* 3450 */ - "offsetValueType\0" /* 3462 */ - "valueToWrite\0" /* 3478 */ - "produceArrayDimFetchAssignValueToWrite\0" /* 3491 */ - "arrayDimFetch\0" /* 3530 */ - "PhpParser\\Node\\Expr\\ArrayDimFetch\0" /* 3544 */ - "shouldKeepList\0" /* 3578 */ - "offsetType\0" /* 3593 */ - "trackedLinkImpliesOffset\0" /* 3604 */ - "a\0" /* 3629 */ - "b\0" /* 3631 */ - "isSameVariable\0" /* 3633 */ - "isSameOffset\0" /* 3648 */ - "getLiteralArrayKeyType\0" /* 3661 */ - "propertyFetch\0" /* 3684 */ - "PhpParser\\Node\\Expr\\PropertyFetch|PhpParser\\Node\\Expr\\StaticPropertyFetch\0" /* 3698 */ - "getOriginalPropertyType\0" /* 3772 */ - "array\0" /* 3796 */ - "hasArrayReference\0" /* 3802 */ - "rhs\0" /* 3820 */ - "redundant"; /* 3824 */ + "readsOnlyItsVariables\0" /* 3083 */ + "PhpParser\\Node\\Expr\\FuncCall\0" /* 3105 */ + "processInArrayForConditionalExpressionsAfterAssign\0" /* 3134 */ + "isExprSafeToProjectThroughVariable\0" /* 3185 */ + "dimFetchStack\0" /* 3220 */ + "PHPStan\\Analyser\\Scope\0" /* 3234 */ + "isImplicitArrayCreation\0" /* 3257 */ + "rootVarName\0" /* 3281 */ + "arrayExpr\0" /* 3293 */ + "PhpParser\\Node\\Expr\\Array_\0" /* 3303 */ + "parentExpr\0" /* 3330 */ + "processArrayByRefItems\0" /* 3341 */ + "implicitIndex\0" /* 3364 */ + "processArrayByRefItemsWithImplicitIndex\0" /* 3378 */ + "isFlattenableUnpackedArray\0" /* 3418 */ + "index\0" /* 3445 */ + "advanceImplicitIndex\0" /* 3451 */ + "offsetTypes\0" /* 3472 */ + "offsetValueType\0" /* 3484 */ + "valueToWrite\0" /* 3500 */ + "produceArrayDimFetchAssignValueToWrite\0" /* 3513 */ + "arrayDimFetch\0" /* 3552 */ + "PhpParser\\Node\\Expr\\ArrayDimFetch\0" /* 3566 */ + "shouldKeepList\0" /* 3600 */ + "offsetType\0" /* 3615 */ + "trackedLinkImpliesOffset\0" /* 3626 */ + "a\0" /* 3651 */ + "b\0" /* 3653 */ + "isSameVariable\0" /* 3655 */ + "isSameOffset\0" /* 3670 */ + "getLiteralArrayKeyType\0" /* 3683 */ + "propertyFetch\0" /* 3706 */ + "PhpParser\\Node\\Expr\\PropertyFetch|PhpParser\\Node\\Expr\\StaticPropertyFetch\0" /* 3720 */ + "getOriginalPropertyType\0" /* 3794 */ + "array\0" /* 3818 */ + "hasArrayReference\0" /* 3824 */ + "rhs\0" /* 3842 */ + "redundant"; /* 3846 */ inline constexpr reg::PackedArg args[] = { reg::packed(0, 0, 25), /* __construct $templateArgumentObserver */ reg::packed(76, 0, 99), /* __construct $varAnnotationProcessor */ @@ -405,11 +406,13 @@ inline constexpr reg::PackedArg args[] = { reg::packed(3022, 0, 2030), /* processDerivedConditionalExpressionsAfterAssign $assignedType */ reg::packed(2538, MAY_BE_ARRAY), /* processDerivedConditionalExpressionsAfterAssign $rhsImpurePoints */ reg::packed(1388, MAY_BE_ARRAY), /* processDerivedConditionalExpressionsAfterAssign return */ + reg::packed(1363, 0, 1368), /* readsOnlyItsVariables $expr */ + reg::packed(1388, MAY_BE_BOOL), /* readsOnlyItsVariables return */ reg::packed(1398, 0, 1416), /* processInArrayForConditionalExpressionsAfterAssign $nodeScopeResolver */ reg::packed(1476, 0, 1482), /* processInArrayForConditionalExpressionsAfterAssign $scope */ reg::packed(2474, MAY_BE_STRING), /* processInArrayForConditionalExpressionsAfterAssign $variableName */ reg::packed(2487, MAY_BE_ARRAY), /* processInArrayForConditionalExpressionsAfterAssign $conditionalExpressions */ - reg::packed(1664, 0, 3083), /* processInArrayForConditionalExpressionsAfterAssign $assignedExpr */ + reg::packed(1664, 0, 3105), /* processInArrayForConditionalExpressionsAfterAssign $assignedExpr */ reg::packed(3022, 0, 2030), /* processInArrayForConditionalExpressionsAfterAssign $assignedType */ reg::packed(2538, MAY_BE_ARRAY), /* processInArrayForConditionalExpressionsAfterAssign $rhsImpurePoints */ reg::packed(1388, MAY_BE_ARRAY), /* processInArrayForConditionalExpressionsAfterAssign return */ @@ -418,60 +421,60 @@ inline constexpr reg::PackedArg args[] = { reg::packed(2538, MAY_BE_ARRAY), /* isExprSafeToProjectThroughVariable $rhsImpurePoints */ reg::packed(1664, 0, 1368), /* isExprSafeToProjectThroughVariable $assignedExpr */ reg::packed(1388, MAY_BE_BOOL), /* isExprSafeToProjectThroughVariable return */ - reg::packed(3198, MAY_BE_ARRAY), /* isImplicitArrayCreation $dimFetchStack */ - reg::packed(1476, 0, 3212), /* isImplicitArrayCreation $scope */ + reg::packed(3220, MAY_BE_ARRAY), /* isImplicitArrayCreation $dimFetchStack */ + reg::packed(1476, 0, 3234), /* isImplicitArrayCreation $scope */ reg::packed(1388, 0, 2840), /* isImplicitArrayCreation return */ reg::packed(1398, 0, 1416), /* processArrayByRefItems $nodeScopeResolver */ reg::packed(1476, 0, 1482), /* processArrayByRefItems $scope */ reg::packed(1513, 0, 1521), /* processArrayByRefItems $storage */ - reg::packed(3259, MAY_BE_STRING), /* processArrayByRefItems $rootVarName */ - reg::packed(3271, 0, 3281), /* processArrayByRefItems $arrayExpr */ - reg::packed(3308, 0, 1368), /* processArrayByRefItems $parentExpr */ + reg::packed(3281, MAY_BE_STRING), /* processArrayByRefItems $rootVarName */ + reg::packed(3293, 0, 3303), /* processArrayByRefItems $arrayExpr */ + reg::packed(3330, 0, 1368), /* processArrayByRefItems $parentExpr */ reg::packed(1388, 0, 1482), /* processArrayByRefItems return */ reg::packed(1398, 0, 1416), /* processArrayByRefItemsWithImplicitIndex $nodeScopeResolver */ reg::packed(1476, 0, 1482), /* processArrayByRefItemsWithImplicitIndex $scope */ reg::packed(1513, 0, 1521), /* processArrayByRefItemsWithImplicitIndex $storage */ - reg::packed(3259, MAY_BE_STRING), /* processArrayByRefItemsWithImplicitIndex $rootVarName */ - reg::packed(3271, 0, 3281), /* processArrayByRefItemsWithImplicitIndex $arrayExpr */ - reg::packed(3308, 0, 1368), /* processArrayByRefItemsWithImplicitIndex $parentExpr */ - reg::packed(3342, MAY_BE_NULL | MAY_BE_LONG), /* processArrayByRefItemsWithImplicitIndex $implicitIndex */ + reg::packed(3281, MAY_BE_STRING), /* processArrayByRefItemsWithImplicitIndex $rootVarName */ + reg::packed(3293, 0, 3303), /* processArrayByRefItemsWithImplicitIndex $arrayExpr */ + reg::packed(3330, 0, 1368), /* processArrayByRefItemsWithImplicitIndex $parentExpr */ + reg::packed(3364, MAY_BE_NULL | MAY_BE_LONG), /* processArrayByRefItemsWithImplicitIndex $implicitIndex */ reg::packed(1388, MAY_BE_ARRAY), /* processArrayByRefItemsWithImplicitIndex return */ - reg::packed(3271, 0, 3281), /* isFlattenableUnpackedArray $arrayExpr */ + reg::packed(3293, 0, 3303), /* isFlattenableUnpackedArray $arrayExpr */ reg::packed(1388, MAY_BE_BOOL), /* isFlattenableUnpackedArray return */ - reg::packed(3423, MAY_BE_LONG), /* advanceImplicitIndex $index */ + reg::packed(3445, MAY_BE_LONG), /* advanceImplicitIndex $index */ reg::packed(1388, MAY_BE_NULL | MAY_BE_LONG), /* advanceImplicitIndex return */ reg::packed(1398, 0, 1416), /* produceArrayDimFetchAssignValueToWrite $nodeScopeResolver */ - reg::packed(3198, MAY_BE_ARRAY), /* produceArrayDimFetchAssignValueToWrite $dimFetchStack */ - reg::packed(3450, MAY_BE_ARRAY), /* produceArrayDimFetchAssignValueToWrite $offsetTypes */ - reg::packed(3462, 0, 2030), /* produceArrayDimFetchAssignValueToWrite $offsetValueType */ - reg::packed(3478, 0, 2030), /* produceArrayDimFetchAssignValueToWrite $valueToWrite */ + reg::packed(3220, MAY_BE_ARRAY), /* produceArrayDimFetchAssignValueToWrite $dimFetchStack */ + reg::packed(3472, MAY_BE_ARRAY), /* produceArrayDimFetchAssignValueToWrite $offsetTypes */ + reg::packed(3484, 0, 2030), /* produceArrayDimFetchAssignValueToWrite $offsetValueType */ + reg::packed(3500, 0, 2030), /* produceArrayDimFetchAssignValueToWrite $valueToWrite */ reg::packed(1476, 0, 1482), /* produceArrayDimFetchAssignValueToWrite $scope */ reg::packed(1513, 0, 1521), /* produceArrayDimFetchAssignValueToWrite $storage */ reg::packed(1388, MAY_BE_ARRAY), /* produceArrayDimFetchAssignValueToWrite return */ reg::packed(1398, 0, 1416), /* shouldKeepList $nodeScopeResolver */ - reg::packed(3530, 0, 3544), /* shouldKeepList $arrayDimFetch */ + reg::packed(3552, 0, 3566), /* shouldKeepList $arrayDimFetch */ reg::packed(1476, 0, 1482), /* shouldKeepList $scope */ reg::packed(1513, 0, 1521), /* shouldKeepList $storage */ - reg::packed(3462, 0, 2030), /* shouldKeepList $offsetValueType */ + reg::packed(3484, 0, 2030), /* shouldKeepList $offsetValueType */ reg::packed(1388, MAY_BE_BOOL), /* shouldKeepList return */ - reg::packed(3462, 0, 2030), /* trackedLinkImpliesOffset $offsetValueType */ - reg::packed(3593, 0, 2030), /* trackedLinkImpliesOffset $offsetType */ + reg::packed(3484, 0, 2030), /* trackedLinkImpliesOffset $offsetValueType */ + reg::packed(3615, 0, 2030), /* trackedLinkImpliesOffset $offsetType */ reg::packed(1388, MAY_BE_BOOL), /* trackedLinkImpliesOffset return */ - reg::packed(3629, 0, 1368), /* isSameVariable $a */ - reg::packed(3631, 0, 1368), /* isSameVariable $b */ + reg::packed(3651, 0, 1368), /* isSameVariable $a */ + reg::packed(3653, 0, 1368), /* isSameVariable $b */ reg::packed(1388, MAY_BE_BOOL), /* isSameVariable return */ - reg::packed(3629, 0, 1368), /* isSameOffset $a */ - reg::packed(3631, 0, 1368), /* isSameOffset $b */ + reg::packed(3651, 0, 1368), /* isSameOffset $a */ + reg::packed(3653, 0, 1368), /* isSameOffset $b */ reg::packed(1388, MAY_BE_BOOL), /* isSameOffset return */ reg::packed(1363, 0, 1368), /* getLiteralArrayKeyType $expr */ reg::packed(1388, MAY_BE_NULL, 2030), /* getLiteralArrayKeyType return */ reg::packed(1398, 0, 1416), /* getOriginalPropertyType $nodeScopeResolver */ - reg::packed(3684, 0, 3698), /* getOriginalPropertyType $propertyFetch */ + reg::packed(3706, 0, 3720), /* getOriginalPropertyType $propertyFetch */ reg::packed(1476, 0, 1482), /* getOriginalPropertyType $scope */ reg::packed(1388, 0, 2030), /* getOriginalPropertyType return */ - reg::packed(3796, 0, 3281), /* hasArrayReference $array */ + reg::packed(3818, 0, 3303), /* hasArrayReference $array */ reg::packed(1388, MAY_BE_BOOL), /* hasArrayReference return */ - reg::packed(3820, 0, 1618), /* redundant $rhs */ + reg::packed(3842, 0, 1618), /* redundant $rhs */ reg::packed(1967, 0, 1368), /* redundant $target */ reg::packed(1513, 0, 1521), /* redundant $storage */ reg::packed(1388, MAY_BE_NULL, 2030), /* redundant return */ @@ -506,22 +509,23 @@ inline constexpr sigtab::Sig addConditionalExpressionHolder = { { 2861 /* addCon inline constexpr sigtab::Sig mergeConditionalExpressions = { { 2918 /* mergeConditionalExpressions */, 2, 161, 2, 163, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig processMatchForConditionalExpressionsAfterAssign = { { 2973 /* processMatchForConditionalExpressionsAfterAssign */, 3, 164, 3, 167, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig processDerivedConditionalExpressionsAfterAssign = { { 3035 /* processDerivedConditionalExpressionsAfterAssign */, 7, 168, 7, 175, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig processInArrayForConditionalExpressionsAfterAssign = { { 3112 /* processInArrayForConditionalExpressionsAfterAssign */, 7, 176, 7, 183, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isExprSafeToProjectThroughVariable = { { 3163 /* isExprSafeToProjectThroughVariable */, 4, 184, 4, 188, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isImplicitArrayCreation = { { 3235 /* isImplicitArrayCreation */, 2, 189, 2, 191, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig processArrayByRefItems = { { 3319 /* processArrayByRefItems */, 6, 192, 6, 198, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig processArrayByRefItemsWithImplicitIndex = { { 3356 /* processArrayByRefItemsWithImplicitIndex */, 7, 199, 7, 206, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isFlattenableUnpackedArray = { { 3396 /* isFlattenableUnpackedArray */, 1, 207, 1, 208, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig advanceImplicitIndex = { { 3429 /* advanceImplicitIndex */, 1, 209, 1, 210, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig produceArrayDimFetchAssignValueToWrite = { { 3491 /* produceArrayDimFetchAssignValueToWrite */, 7, 211, 7, 218, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig shouldKeepList = { { 3578 /* shouldKeepList */, 5, 219, 5, 224, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig trackedLinkImpliesOffset = { { 3604 /* trackedLinkImpliesOffset */, 2, 225, 2, 227, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isSameVariable = { { 3633 /* isSameVariable */, 2, 228, 2, 230, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig isSameOffset = { { 3648 /* isSameOffset */, 2, 231, 2, 233, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getLiteralArrayKeyType = { { 3661 /* getLiteralArrayKeyType */, 1, 234, 1, 235, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getOriginalPropertyType = { { 3772 /* getOriginalPropertyType */, 3, 236, 3, 239, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig hasArrayReference = { { 3802 /* hasArrayReference */, 1, 240, 1, 241, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; -inline constexpr sigtab::Sig redundant = { { 3824 /* redundant */, 3, 242, 3, 245, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig readsOnlyItsVariables = { { 3083 /* readsOnlyItsVariables */, 1, 176, 1, 177, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig processInArrayForConditionalExpressionsAfterAssign = { { 3134 /* processInArrayForConditionalExpressionsAfterAssign */, 7, 178, 7, 185, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isExprSafeToProjectThroughVariable = { { 3185 /* isExprSafeToProjectThroughVariable */, 4, 186, 4, 190, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isImplicitArrayCreation = { { 3257 /* isImplicitArrayCreation */, 2, 191, 2, 193, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig processArrayByRefItems = { { 3341 /* processArrayByRefItems */, 6, 194, 6, 200, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig processArrayByRefItemsWithImplicitIndex = { { 3378 /* processArrayByRefItemsWithImplicitIndex */, 7, 201, 7, 208, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isFlattenableUnpackedArray = { { 3418 /* isFlattenableUnpackedArray */, 1, 209, 1, 210, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig advanceImplicitIndex = { { 3451 /* advanceImplicitIndex */, 1, 211, 1, 212, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig produceArrayDimFetchAssignValueToWrite = { { 3513 /* produceArrayDimFetchAssignValueToWrite */, 7, 213, 7, 220, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig shouldKeepList = { { 3600 /* shouldKeepList */, 5, 221, 5, 226, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig trackedLinkImpliesOffset = { { 3626 /* trackedLinkImpliesOffset */, 2, 227, 2, 229, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isSameVariable = { { 3655 /* isSameVariable */, 2, 230, 2, 232, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig isSameOffset = { { 3670 /* isSameOffset */, 2, 233, 2, 235, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getLiteralArrayKeyType = { { 3683 /* getLiteralArrayKeyType */, 1, 236, 1, 237, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getOriginalPropertyType = { { 3794 /* getOriginalPropertyType */, 3, 238, 3, 241, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig hasArrayReference = { { 3824 /* hasArrayReference */, 1, 242, 1, 243, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig redundant = { { 3846 /* redundant */, 3, 244, 3, 247, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; } // namespace sig } // namespace ptdecl::AssignHandler