From 07c47c068d4c8cc49e30e1a65d8c2cdda0e72746 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Thu, 3 Dec 2026 12:41:24 +0000 Subject: [PATCH 1/3] Widen unions holding more than 16 array shapes Exact unions keep shapes that do not merge into one, so a list appended to in N mutually exclusive branches joined N shapes of up to N keys - quadratic in the branches, while staying far below ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT values. In a loop, every convergence pass joined such a growing union: 160 conditional appends took 4.9 s and 681 MB. TypeCombinator::widen() now also widens a union with more than ArrayWidening::UNION_SHAPES_LIMIT shapes, counting a list with optional keys once per length it can have. The 160 appends take 0.18 s and 38 MB. Amp-Thread-ID: https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46 --- src/Type/Combinator/ArrayWidening.php | 35 +- src/Type/TypeCombinator.php | 7 +- tests/PHPStan/Analyser/nsrt/bug-13509.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-15293.php | 4 +- .../PHPStan/Analyser/nsrt/bug-6936-limit.php | 6 +- .../Analyser/nsrt/oversized-array-stages.php | 2 +- tests/bench/data/conditional-list-appends.php | 344 ++++++++++++++++++ .../data/instanceof-list-appends-in-loop.php | 91 +++++ 8 files changed, 478 insertions(+), 13 deletions(-) create mode 100644 tests/bench/data/conditional-list-appends.php create mode 100644 tests/bench/data/instanceof-list-appends-in-loop.php diff --git a/src/Type/Combinator/ArrayWidening.php b/src/Type/Combinator/ArrayWidening.php index ef866d99d14..ddd98409ab5 100644 --- a/src/Type/Combinator/ArrayWidening.php +++ b/src/Type/Combinator/ArrayWidening.php @@ -9,12 +9,13 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; +use function count; /** * Widening of array shapes, what TypeCombinator::widen() does to the arrays of * a union and to the values merged into a general array: while the types hold - * more than ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT constant array values, - * the next step runs - the general arrays merge first, then the shapes fold, + * more than ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT constant array values + * or more than UNION_SHAPES_LIMIT shapes, the next step runs - the general arrays merge first, then the shapes fold, * keeping their keys, and then degrade to oversized general arrays. The steps * see the members of the unions of arrays among the types. * @@ -23,6 +24,14 @@ final class ArrayWidening { + /** + * The most shapes a union keeps apart. Exact unions keep shapes that do not + * merge into one, so a list appended to in N branches would join N shapes of + * up to N keys - quadratic in the branches, while each stays far below + * ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT values. + */ + public const UNION_SHAPES_LIMIT = 16; + /** @var list|null */ private static ?array $steps = null; @@ -69,7 +78,27 @@ public static function widen(array $types): array /** @param list $types */ public static function isWithinLimit(array $types): bool { - return TypeCombinator::countConstantArrayValueTypes($types) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT; + return self::countShapes($types) <= self::UNION_SHAPES_LIMIT + && TypeCombinator::countConstantArrayValueTypes($types) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT; + } + + /** + * A list with optional keys counts once per length it can have: appending + * to it gives a shape of each length, so folding the lists of a union into + * one with optional keys does not make it smaller to compute with. + * + * @param list $types + */ + private static function countShapes(array $types): int + { + $count = 0; + foreach ($types as $type) { + foreach ($type->getConstantArrays() as $shape) { + $count += $shape->isList()->yes() ? count($shape->getOptionalKeys()) + 1 : 1; + } + } + + return $count; } /** diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 15a8a4aa162..a375006ac4f 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -7,6 +7,7 @@ use PHPStan\Turbo\TurboExtensionEnabler; use PHPStan\Type\Accessory\AccessoryType; use PHPStan\Type\Combinator\AbsorbedArrayUnionRule; +use PHPStan\Type\Combinator\ArrayWidening; use PHPStan\Type\Combinator\AtOnceUnionRemoveRule; use PHPStan\Type\Combinator\BooleanUnionRule; use PHPStan\Type\Combinator\DisjointFiniteUnionRemoveRule; @@ -37,7 +38,6 @@ use PHPStan\Type\Combinator\UnionRemoveRule; use PHPStan\Type\Combinator\UnionRule; use PHPStan\Type\Constant\ConstantArrayType; -use PHPStan\Type\Constant\ConstantArrayTypeBuilder; use PHPStan\Type\Generic\TemplateBenevolentUnionType; use PHPStan\Type\Generic\TemplateMixedType; use PHPStan\Type\Generic\TemplateType; @@ -204,7 +204,8 @@ public static function containsNull(Type $type): bool /** * The type, generalized where it holds more constant array values than - * ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT: shapes with the same keys + * ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT or more shapes than + * ArrayWidening::UNION_SHAPES_LIMIT: shapes with the same keys * merged, shapes sharing a required key folded into one, and what is still * too big degraded to an oversized array. * @@ -235,7 +236,7 @@ public static function widen(Type $type): Type $bleedingEdge = BleedingEdgeToggle::isBleedingEdge(); if ( - self::countConstantArrayValueTypes($arrayTypes) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT + ArrayWidening::isWithinLimit($arrayTypes) && (!$bleedingEdge || count(GeneralArrayUnionRule::getGeneralArrays($arrayTypes)) <= GeneralArrayUnionRule::GENERAL_ARRAYS_LIMIT) ) { return $type; diff --git a/tests/PHPStan/Analyser/nsrt/bug-13509.php b/tests/PHPStan/Analyser/nsrt/bug-13509.php index be1f0937fb8..f9bd97d4032 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-13509.php +++ b/tests/PHPStan/Analyser/nsrt/bug-13509.php @@ -80,7 +80,7 @@ function alert(): ?array return null; } - assertType("list{0: array{message: 'Foo', details: 'bar', duration: int<1, max>|null, severity: 100}|array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 2?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 3?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 4?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 5?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 6?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 7?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}", $alerts); + assertType("array{array{message: 'Idle', duration: int<1, max>|null, severity: 23}}|list{0: array{message: 'Foo', details: 'bar', duration: int<1, max>|null, severity: 100}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 2?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 3?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 4?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 5?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 6?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 7?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}|list{0: array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}", $alerts); usort($alerts, fn ($a, $b) => $b['severity'] <=> $a['severity']); diff --git a/tests/PHPStan/Analyser/nsrt/bug-15293.php b/tests/PHPStan/Analyser/nsrt/bug-15293.php index f970f4dac0a..a6c43ae92af 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-15293.php +++ b/tests/PHPStan/Analyser/nsrt/bug-15293.php @@ -208,8 +208,8 @@ class Foo public function doFoo(string $key): void { - assertType('array{}|(non-empty-array&oversized-array)', self::MAP[$key]); - assertType('non-empty-array&oversized-array', self::MAP_WITHOUT_EMPTY[$key]); + assertType('array{}|(non-empty-array&oversized-array)', self::MAP[$key]); + assertType('non-empty-array&oversized-array', self::MAP_WITHOUT_EMPTY[$key]); } } diff --git a/tests/PHPStan/Analyser/nsrt/bug-6936-limit.php b/tests/PHPStan/Analyser/nsrt/bug-6936-limit.php index 65b06f51b5c..edf7463ed4b 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-6936-limit.php +++ b/tests/PHPStan/Analyser/nsrt/bug-6936-limit.php @@ -36,16 +36,16 @@ public function testLimits():void $arr[] = 'g'; } - assertType("array{'e', 2|'f'|'g', 3|'g'}|array{'f', 2|'g', 3}|array{1|'g', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g', 2: 3|'d'|'e'|'f'|'g', 3?: 'e'|'f'|'g', 4?: 'f'|'g', 5?: 'g'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g', 2: 3|'e'|'f'|'g', 3?: 'f'|'g', 4?: 'g'}|list{0: 'd', 1: 2|'e'|'f'|'g', 2: 3|'f'|'g', 3?: 'g'}", $arr + $arr2); + assertType("array{1|'g', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}", $arr + $arr2); if (rand(0,1)) { $arr[] = 'h'; } - assertType("array{'f', 2|'g'|'h', 3|'h'}|array{'g', 2|'h', 3}|array{1|'h', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'d'|'e'|'f'|'g'|'h', 3?: 'e'|'f'|'g'|'h', 4?: 'f'|'g'|'h', 5?: 'g'|'h', 6?: 'h'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h', 2: 3|'e'|'f'|'g'|'h', 3?: 'f'|'g'|'h', 4?: 'g'|'h', 5?: 'h'}|list{0: 'd', 1: 2|'e'|'f'|'g'|'h', 2: 3|'f'|'g'|'h', 3?: 'g'|'h', 4?: 'h'}|list{0: 'e', 1: 2|'f'|'g'|'h', 2: 3|'g'|'h', 3?: 'h'}", $arr + $arr2); + assertType("array{'g', 2|'h', 3}|array{1|'h', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}", $arr + $arr2); if (rand(0,1)) { $arr[] = 'i'; } - assertType("array{'g', 2|'h'|'i', 3|'i'}|array{'h', 2|'i', 3}|array{1|'i', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'e'|'f'|'g'|'h'|'i', 4?: 'f'|'g'|'h'|'i', 5?: 'g'|'h'|'i', 6?: 'h'|'i', 7?: 'i'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'e'|'f'|'g'|'h'|'i', 3?: 'f'|'g'|'h'|'i', 4?: 'g'|'h'|'i', 5?: 'h'|'i', 6?: 'i'}|list{0: 'd', 1: 2|'e'|'f'|'g'|'h'|'i', 2: 3|'f'|'g'|'h'|'i', 3?: 'g'|'h'|'i', 4?: 'h'|'i', 5?: 'i'}|list{0: 'e', 1: 2|'f'|'g'|'h'|'i', 2: 3|'g'|'h'|'i', 3?: 'h'|'i', 4?: 'i'}|list{0: 'f', 1: 2|'g'|'h'|'i', 2: 3|'h'|'i', 3?: 'i'}", $arr + $arr2); + assertType("array{'g', 2|'h'|'i', 3|'i'}|array{'h', 2|'i', 3}|array{1|'i', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}", $arr + $arr2); } } diff --git a/tests/PHPStan/Analyser/nsrt/oversized-array-stages.php b/tests/PHPStan/Analyser/nsrt/oversized-array-stages.php index fa77aa600d4..3a56d63696e 100644 --- a/tests/PHPStan/Analyser/nsrt/oversized-array-stages.php +++ b/tests/PHPStan/Analyser/nsrt/oversized-array-stages.php @@ -68,7 +68,7 @@ function phase2TriangularCollapse(): array return []; } - assertType("array{array{kind: 'k8', value: 8, opts: array{a: 8}}}|list{0: array{kind: 'k1', value: 1, opts: array{a: 1}}|array{kind: 'k2', value: 2, opts: array{a: 2}}|array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}, 1?: array{kind: 'k2', value: 2, opts: array{a: 2}}|array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 2?: array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 3?: array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 4?: array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 5?: array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 6?: array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 7?: array{kind: 'k8', value: 8, opts: array{a: 8}}}", $items); + assertType("array{array{kind: 'k8', value: 8, opts: array{a: 8}}}|list{0: array{kind: 'k1', value: 1, opts: array{a: 1}}|array{kind: 'k2', value: 2, opts: array{a: 2}}|array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}, 1?: array{kind: 'k2', value: 2, opts: array{a: 2}}|array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 2?: array{kind: 'k3', value: 3, opts: array{a: 3}}|array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 3?: array{kind: 'k4', value: 4, opts: array{a: 4}}|array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 4?: array{kind: 'k5', value: 5, opts: array{a: 5}}|array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 5?: array{kind: 'k6', value: 6, opts: array{a: 6}}|array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 6?: array{kind: 'k7', value: 7, opts: array{a: 7}}|array{kind: 'k8', value: 8, opts: array{a: 8}}, 7?: array{kind: 'k8', value: 8, opts: array{a: 8}}}|list{0: array{kind: 'k7', value: 7, opts: array{a: 7}}, 1?: array{kind: 'k8', value: 8, opts: array{a: 8}}}", $items); return $items; } diff --git a/tests/bench/data/conditional-list-appends.php b/tests/bench/data/conditional-list-appends.php new file mode 100644 index 00000000000..3ba4f15c046 --- /dev/null +++ b/tests/bench/data/conditional-list-appends.php @@ -0,0 +1,344 @@ + + */ + public function collect( + bool $p0, + bool $p1, + bool $p2, + bool $p3, + bool $p4, + bool $p5, + bool $p6, + bool $p7, + bool $p8, + bool $p9, + bool $p10, + bool $p11, + bool $p12, + bool $p13, + bool $p14, + bool $p15, + bool $p16, + bool $p17, + bool $p18, + bool $p19, + bool $p20, + bool $p21, + bool $p22, + bool $p23, + bool $p24, + bool $p25, + bool $p26, + bool $p27, + bool $p28, + bool $p29, + bool $p30, + bool $p31, + bool $p32, + bool $p33, + bool $p34, + bool $p35, + bool $p36, + bool $p37, + bool $p38, + bool $p39, + bool $p40, + bool $p41, + bool $p42, + bool $p43, + bool $p44, + bool $p45, + bool $p46, + bool $p47, + bool $p48, + bool $p49, + bool $p50, + bool $p51, + bool $p52, + bool $p53, + bool $p54, + bool $p55, + bool $p56, + bool $p57, + bool $p58, + bool $p59, + bool $p60, + bool $p61, + bool $p62, + bool $p63, + bool $p64, + bool $p65, + bool $p66, + bool $p67, + bool $p68, + bool $p69, + bool $p70, + bool $p71, + bool $p72, + bool $p73, + bool $p74, + bool $p75, + bool $p76, + bool $p77, + bool $p78, + bool $p79, + ): array + { + $items = []; + if ($p0) { + $items[] = 0; + } + if ($p1) { + $items[] = 1; + } + if ($p2) { + $items[] = 2; + } + if ($p3) { + $items[] = 3; + } + if ($p4) { + $items[] = 4; + } + if ($p5) { + $items[] = 5; + } + if ($p6) { + $items[] = 6; + } + if ($p7) { + $items[] = 7; + } + if ($p8) { + $items[] = 8; + } + if ($p9) { + $items[] = 9; + } + if ($p10) { + $items[] = 10; + } + if ($p11) { + $items[] = 11; + } + if ($p12) { + $items[] = 12; + } + if ($p13) { + $items[] = 13; + } + if ($p14) { + $items[] = 14; + } + if ($p15) { + $items[] = 15; + } + if ($p16) { + $items[] = 16; + } + if ($p17) { + $items[] = 17; + } + if ($p18) { + $items[] = 18; + } + if ($p19) { + $items[] = 19; + } + if ($p20) { + $items[] = 20; + } + if ($p21) { + $items[] = 21; + } + if ($p22) { + $items[] = 22; + } + if ($p23) { + $items[] = 23; + } + if ($p24) { + $items[] = 24; + } + if ($p25) { + $items[] = 25; + } + if ($p26) { + $items[] = 26; + } + if ($p27) { + $items[] = 27; + } + if ($p28) { + $items[] = 28; + } + if ($p29) { + $items[] = 29; + } + if ($p30) { + $items[] = 30; + } + if ($p31) { + $items[] = 31; + } + if ($p32) { + $items[] = 32; + } + if ($p33) { + $items[] = 33; + } + if ($p34) { + $items[] = 34; + } + if ($p35) { + $items[] = 35; + } + if ($p36) { + $items[] = 36; + } + if ($p37) { + $items[] = 37; + } + if ($p38) { + $items[] = 38; + } + if ($p39) { + $items[] = 39; + } + if ($p40) { + $items[] = 40; + } + if ($p41) { + $items[] = 41; + } + if ($p42) { + $items[] = 42; + } + if ($p43) { + $items[] = 43; + } + if ($p44) { + $items[] = 44; + } + if ($p45) { + $items[] = 45; + } + if ($p46) { + $items[] = 46; + } + if ($p47) { + $items[] = 47; + } + if ($p48) { + $items[] = 48; + } + if ($p49) { + $items[] = 49; + } + if ($p50) { + $items[] = 50; + } + if ($p51) { + $items[] = 51; + } + if ($p52) { + $items[] = 52; + } + if ($p53) { + $items[] = 53; + } + if ($p54) { + $items[] = 54; + } + if ($p55) { + $items[] = 55; + } + if ($p56) { + $items[] = 56; + } + if ($p57) { + $items[] = 57; + } + if ($p58) { + $items[] = 58; + } + if ($p59) { + $items[] = 59; + } + if ($p60) { + $items[] = 60; + } + if ($p61) { + $items[] = 61; + } + if ($p62) { + $items[] = 62; + } + if ($p63) { + $items[] = 63; + } + if ($p64) { + $items[] = 64; + } + if ($p65) { + $items[] = 65; + } + if ($p66) { + $items[] = 66; + } + if ($p67) { + $items[] = 67; + } + if ($p68) { + $items[] = 68; + } + if ($p69) { + $items[] = 69; + } + if ($p70) { + $items[] = 70; + } + if ($p71) { + $items[] = 71; + } + if ($p72) { + $items[] = 72; + } + if ($p73) { + $items[] = 73; + } + if ($p74) { + $items[] = 74; + } + if ($p75) { + $items[] = 75; + } + if ($p76) { + $items[] = 76; + } + if ($p77) { + $items[] = 77; + } + if ($p78) { + $items[] = 78; + } + if ($p79) { + $items[] = 79; + } + + return $items; + } + +} diff --git a/tests/bench/data/instanceof-list-appends-in-loop.php b/tests/bench/data/instanceof-list-appends-in-loop.php new file mode 100644 index 00000000000..0e48cecbd06 --- /dev/null +++ b/tests/bench/data/instanceof-list-appends-in-loop.php @@ -0,0 +1,91 @@ + $claims + */ + public function check(array $claims, bool $a, bool $b, bool $c, bool $d, bool $e, bool $f, bool $g): void + { + $marked = []; + foreach ($claims as $claim) { + if ($claim instanceof ClaimA && $a) { + $marked[] = $claim; + } + if ($claim instanceof ClaimB && $b) { + $marked[] = $claim; + } + if ($claim instanceof ClaimC && $c) { + $marked[] = $claim; + } + if ($claim instanceof ClaimD && $d) { + $marked[] = $claim; + } + if ($claim instanceof ClaimE && $e) { + $marked[] = $claim; + } + if ($claim instanceof ClaimF && $f) { + $marked[] = $claim; + } + if ($claim instanceof ClaimG && $g) { + $marked[] = $claim; + } + } + foreach ($marked as $claim) { + $this->drop($claim); + } + } + + private function drop(Claim $claim): void + { + } + +} From 2c836a1479b92603f3f6791feaa1f76b7fe08664 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Thu, 3 Dec 2026 12:41:24 +0000 Subject: [PATCH 2/3] Unroll a constant-array foreach in loop passes only when it writes with the loop variable Inside an enclosing loop's convergence passes, a foreach over a constant array was unrolled key by key in every pass. Unrolling there is needed only to keep the offsets the body writes with the loop's key or value required, such as `$ids[$kind] = ...` (bug-15906); other bodies now converge like any nested loop. A 16-key foreach in a loop drops from 0.38 s to 0.01 s. The native ForeachHandler mirrors the check. Amp-Thread-ID: https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46 --- src/Analyser/StmtHandler/ForeachHandler.php | 40 +++++++++ .../nsrt/loop-in-unrolled-foreach-in-loop.php | 29 +++++++ .../data/constant-array-foreach-in-loop.php | 26 ++++++ turbo-ext/src/ForeachHandler.cpp | 85 +++++++++++++++++++ turbo-ext/src/generated/ForeachHandler.h | 38 +++++---- 5 files changed, 201 insertions(+), 17 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/loop-in-unrolled-foreach-in-loop.php create mode 100644 tests/bench/data/constant-array-foreach-in-loop.php diff --git a/src/Analyser/StmtHandler/ForeachHandler.php b/src/Analyser/StmtHandler/ForeachHandler.php index 6e08774f201..54969157242 100644 --- a/src/Analyser/StmtHandler/ForeachHandler.php +++ b/src/Analyser/StmtHandler/ForeachHandler.php @@ -8,6 +8,8 @@ use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\AssignOp; +use PhpParser\Node\Expr\AssignRef; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\List_; @@ -17,6 +19,7 @@ use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Continue_; use PhpParser\Node\Stmt\Foreach_; +use PhpParser\NodeFinder; use PHPStan\Analyser\ConditionalExpressionHolder; use PHPStan\Analyser\ConvergedLoopHeads; use PHPStan\Analyser\ExpressionContext; @@ -776,6 +779,38 @@ private function addIterateeMemberConditionalHolders(MutatingScope $scope, Expr return $scope; } + /** + * Whether the body writes an offset whose key mentions the loop's key or + * value variable, as `$a[$value] = ...` or `$a[$x][$key][] = ...`. + */ + private static function writesOffsetWithLoopVariable(Foreach_ $stmt): bool + { + $names = []; + foreach ([$stmt->keyVar, $stmt->valueVar] as $var) { + if (!$var instanceof Variable || !is_string($var->name)) { + continue; + } + $names[$var->name] = true; + } + + return (new NodeFinder())->findFirst($stmt->stmts, static function (Node $node) use ($names): bool { + if (!$node instanceof Assign && !$node instanceof AssignRef && !$node instanceof AssignOp) { + return false; + } + for ($target = $node->var; $target instanceof ArrayDimFetch; $target = $target->var) { + if ($target->dim === null) { + continue; + } + $mentions = (new NodeFinder())->findFirst([$target->dim], static fn (Node $dimNode): bool => $dimNode instanceof Variable && is_string($dimNode->name) && isset($names[$dimNode->name])); + if ($mentions !== null) { + return true; + } + } + + return false; + }) !== null; + } + /** * @return array{bodyScope: MutatingScope, endScope: MutatingScope|null, totalKeys: int}|null */ @@ -819,6 +854,11 @@ private function tryProcessUnrolledConstantArrayForeach( if ($totalKeys === 0 || $totalKeys > self::FOREACH_UNROLL_LIMIT) { return null; } + // in an enclosing loop's passes, unrolling is worth its cost only to keep + // the offsets the body writes with the key or the value required + if (!$context->isTopLevel() && !self::writesOffsetWithLoopVariable($stmt)) { + return null; + } $foreachUnrollFactor = $context->getForeachUnrollFactor(); if ($foreachUnrollFactor > 1 && $foreachUnrollFactor * $totalKeys > self::FOREACH_UNROLL_NESTED_LIMIT) { return null; diff --git a/tests/PHPStan/Analyser/nsrt/loop-in-unrolled-foreach-in-loop.php b/tests/PHPStan/Analyser/nsrt/loop-in-unrolled-foreach-in-loop.php new file mode 100644 index 00000000000..28a97297c80 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/loop-in-unrolled-foreach-in-loop.php @@ -0,0 +1,29 @@ +, declarations?: list}> $dependentsById + */ +function collect(array $dependentsById): void +{ + $ids = []; + $paths = []; + foreach ($dependentsById as $dependents) { + $dependentIds = []; + foreach (['analysis', 'declarations'] as $kind) { + $pathIds = []; + foreach ($dependents[$kind] ?? [] as $dependentFile) { + if (!isset($ids[$dependentFile])) { + $ids[$dependentFile] = count($paths); + $paths[] = $dependentFile; + } + $pathIds[] = $ids[$dependentFile]; + } + $dependentIds[$kind] = $pathIds; + } + assertType('array{analysis: list>, declarations: list>}', $dependentIds); + } +} diff --git a/tests/bench/data/constant-array-foreach-in-loop.php b/tests/bench/data/constant-array-foreach-in-loop.php new file mode 100644 index 00000000000..88fac0b9216 --- /dev/null +++ b/tests/bench/data/constant-array-foreach-in-loop.php @@ -0,0 +1,26 @@ + $input + * @param list $variants + * @return list + */ +function collectEntries(array $input, array $variants): array +{ + $entries = []; + foreach ($variants as $variant) { + foreach ([':k0', ':k1', ':k2', ':k3', ':k4', ':k5', ':k6', ':k7', ':k8', ':k9', ':k10', ':k11', ':k12', ':k13', ':k14', ':k15'] as $g) { + if (isset($input[$g])) { + $entries[] = ['p' => $g, 'q' => $variant]; + } + } + } + + return $entries; +} diff --git a/turbo-ext/src/ForeachHandler.cpp b/turbo-ext/src/ForeachHandler.cpp index 95b537c1834..e67697221cf 100644 --- a/turbo-ext/src/ForeachHandler.cpp +++ b/turbo-ext/src/ForeachHandler.cpp @@ -35,6 +35,7 @@ namespace sigs = ptdecl::ForeachHandler::sig; #include "AnalyserValues.h" #include "StmtHandlerCalls.h" #include "LoopHandlerCalls.h" +#include "ParserVisitors.h" #include "zend_smart_str.h" @@ -73,6 +74,81 @@ zend_string *pt_feh_traversable_class = nullptr; zend_string *pt_feh_iterator_aggregate_class = nullptr; zend_string *pt_feh_get_iterator = nullptr; +phpstanturbo::visitors::NodeProp pt_feh_assign_var = PT_NODE_PROP(PT_CLASS_ASSIGN_EXPR, "var"); +phpstanturbo::visitors::NodeProp pt_feh_assign_ref_var = PT_NODE_PROP(PT_CLASS_ASSIGN_REF_EXPR, "var"); +phpstanturbo::visitors::NodeProp pt_feh_assign_op_var = PT_NODE_PROP(PT_CLASS_ASSIGN_OP_EXPR, "var"); +phpstanturbo::visitors::NodeProp pt_feh_dim_fetch_var = PT_NODE_PROP(PT_CLASS_ARRAY_DIM_FETCH, "var"); +phpstanturbo::visitors::NodeProp pt_feh_dim_fetch_dim = PT_NODE_PROP(PT_CLASS_ARRAY_DIM_FETCH, "dim"); +phpstanturbo::visitors::NodeProp pt_feh_loop_variable_name = PT_NODE_PROP(PT_CLASS_VARIABLE, "name"); + +/* the state of one writesOffsetWithLoopVariable() walk; `base` first, as + * pt_find_first_recursive() reads its `failed` flag through it */ +struct LoopVariableOffsetWriteSearch +{ + pt_find_ctx base; + zend_string *keyVarName; + zend_string *valueVarName; +}; + +/* a Variable named like the foreach key or value variable */ +bool loopVariableMatcher(zend_object *node, void *ctx) +{ + LoopVariableOffsetWriteSearch *search = static_cast(ctx); + if (!phpstanturbo::visitors::isInstanceOf(node, PT_CLASS_VARIABLE)) return false; + zval *name = pt_feh_loop_variable_name.of(node); + if (name == NULL || Z_TYPE_P(name) != IS_STRING) return false; + return (search->keyVarName != NULL && zend_string_equals(Z_STR_P(name), search->keyVarName)) + || (search->valueVarName != NULL && zend_string_equals(Z_STR_P(name), search->valueVarName)); +} + +/* an assignment whose ArrayDimFetch chain has a dim mentioning the foreach + * key or value variable */ +bool loopVariableOffsetWriteMatcher(zend_object *node, void *ctx) +{ + zval *target; + if (phpstanturbo::visitors::isInstanceOf(node, PT_CLASS_ASSIGN_EXPR)) { + target = pt_feh_assign_var.of(node); + } else if (phpstanturbo::visitors::isInstanceOf(node, PT_CLASS_ASSIGN_REF_EXPR)) { + target = pt_feh_assign_ref_var.of(node); + } else if (phpstanturbo::visitors::isInstanceOf(node, PT_CLASS_ASSIGN_OP_EXPR)) { + target = pt_feh_assign_op_var.of(node); + } else { + return false; + } + while (target != NULL && Z_TYPE_P(target) == IS_OBJECT && phpstanturbo::visitors::isInstanceOf(Z_OBJ_P(target), PT_CLASS_ARRAY_DIM_FETCH)) { + zend_object *fetch = Z_OBJ_P(target); + zval *dim = pt_feh_dim_fetch_dim.of(fetch); + if (dim != NULL && Z_TYPE_P(dim) == IS_OBJECT) { + if (pt_find_first_recursive(Z_OBJ_P(dim), loopVariableMatcher, ctx) != NULL) return true; + if (UNEXPECTED(static_cast(ctx)->base.failed)) return false; + } + target = pt_feh_dim_fetch_var.of(fetch); + } + return false; +} + +/* Mirrors the private writesOffsetWithLoopVariable(): whether the body writes + * an offset whose key mentions the loop's key or value variable; -1 = + * pending exception */ +int writesOffsetWithLoopVariable(zval *stmts, zend_string *keyVarName, zend_string *valueVarName) +{ + if (Z_TYPE_P(stmts) != IS_ARRAY) return 0; + zend_class_entry *nodeCe = pt_class(PT_CLASS_NODE); + if (UNEXPECTED(nodeCe == NULL)) return -1; + LoopVariableOffsetWriteSearch search; + memset(&search.base, 0, sizeof(search.base)); + search.keyVarName = keyVarName; + search.valueVarName = valueVarName; + zval *stmt; + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stmts), stmt) { + ZVAL_DEREF(stmt); + if (Z_TYPE_P(stmt) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(stmt), nodeCe)) continue; + if (pt_find_first_recursive(Z_OBJ_P(stmt), loopVariableOffsetWriteMatcher, &search) != NULL) return 1; + if (UNEXPECTED(search.base.failed)) return -1; + } ZEND_HASH_FOREACH_END(); + return 0; +} + /* $node->getAttributes() / ->setAttributes($attributes) */ zv::Val getAttributes(zval *node) { @@ -1637,6 +1713,15 @@ class ForeachHandler hasUnsealed = true; } if (totalKeys == 0 || totalKeys > PT_FEH_FOREACH_UNROLL_LIMIT) return true; + // in an enclosing loop's passes, unrolling is worth its cost only to keep + // the offsets the body writes with the key or the value required + bool isTopLevel; + if (UNEXPECTED(!pt_statement_context_is_top_level(context, isTopLevel))) return false; + if (!isTopLevel) { + int writes = writesOffsetWithLoopVariable(parts.stmts.raw(), parts.keyVarIsVariable ? parts.keyVarName : NULL, parts.valueVarName); + if (UNEXPECTED(writes < 0)) return false; + if (writes == 0) return true; + } zend_long foreachUnrollFactor; if (UNEXPECTED(!pt_statement_context_get_foreach_unroll_factor(context, foreachUnrollFactor))) return false; if (foreachUnrollFactor > 1 && foreachUnrollFactor * totalKeys > PT_FEH_FOREACH_UNROLL_NESTED_LIMIT) return true; diff --git a/turbo-ext/src/generated/ForeachHandler.h b/turbo-ext/src/generated/ForeachHandler.h index 4c9303cecac..e5823a228c0 100644 --- a/turbo-ext/src/generated/ForeachHandler.h +++ b/turbo-ext/src/generated/ForeachHandler.h @@ -71,15 +71,16 @@ inline constexpr char strings[] = "keyVarName\0" /* 650 */ "valueVarName\0" /* 661 */ "addIterateeMemberConditionalHolders\0" /* 674 */ - "originalStorage\0" /* 710 */ - "tryProcessUnrolledConstantArrayForeach\0" /* 726 */ - "exprType\0" /* 765 */ - "PHPStan\\Analyser\\InternalThrowPoint\0" /* 774 */ - "getTraversableForeachThrowPoint\0" /* 810 */ - "iterableValueType\0" /* 842 */ - "list\0" /* 860 */ - "PhpParser\\Node\\Expr\\List_\0" /* 865 */ - "addDestructureTaggedUnionConditionalHolders"; /* 891 */ + "writesOffsetWithLoopVariable\0" /* 710 */ + "originalStorage\0" /* 739 */ + "tryProcessUnrolledConstantArrayForeach\0" /* 755 */ + "exprType\0" /* 794 */ + "PHPStan\\Analyser\\InternalThrowPoint\0" /* 803 */ + "getTraversableForeachThrowPoint\0" /* 839 */ + "iterableValueType\0" /* 871 */ + "list\0" /* 889 */ + "PhpParser\\Node\\Expr\\List_\0" /* 894 */ + "addDestructureTaggedUnionConditionalHolders"; /* 920 */ inline constexpr reg::PackedArg args[] = { reg::packed(0, 0, 10), /* __construct $container */ reg::packed(48, MAY_BE_BOOL), /* __construct $implicitThrows */ @@ -110,21 +111,23 @@ inline constexpr reg::PackedArg args[] = { reg::packed(650, MAY_BE_STRING), /* addIterateeMemberConditionalHolders $keyVarName */ reg::packed(661, MAY_BE_STRING), /* addIterateeMemberConditionalHolders $valueVarName */ reg::packed(258, 0, 327), /* addIterateeMemberConditionalHolders return */ + reg::packed(233, 0, 529), /* writesOffsetWithLoopVariable $stmt */ + reg::packed(258, MAY_BE_BOOL), /* writesOffsetWithLoopVariable return */ reg::packed(268, 0, 286), /* tryProcessUnrolledConstantArrayForeach $nodeScopeResolver */ reg::packed(233, 0, 529), /* tryProcessUnrolledConstantArrayForeach $stmt */ reg::packed(515, 0, 327), /* tryProcessUnrolledConstantArrayForeach $originalScope */ - reg::packed(710, 0, 366), /* tryProcessUnrolledConstantArrayForeach $originalStorage */ + reg::packed(739, 0, 366), /* tryProcessUnrolledConstantArrayForeach $originalStorage */ reg::packed(420, 0, 428), /* tryProcessUnrolledConstantArrayForeach $context */ reg::packed(558, 0, 571), /* tryProcessUnrolledConstantArrayForeach $iterateeType */ reg::packed(589, 0, 571), /* tryProcessUnrolledConstantArrayForeach $nativeIterateeType */ reg::packed(258, MAY_BE_NULL | MAY_BE_ARRAY), /* tryProcessUnrolledConstantArrayForeach return */ reg::packed(321, 0, 327), /* getTraversableForeachThrowPoint $scope */ reg::packed(621, 0, 630), /* getTraversableForeachThrowPoint $iteratee */ - reg::packed(765, 0, 571), /* getTraversableForeachThrowPoint $exprType */ - reg::packed(258, MAY_BE_NULL, 774), /* getTraversableForeachThrowPoint return */ + reg::packed(794, 0, 571), /* getTraversableForeachThrowPoint $exprType */ + reg::packed(258, MAY_BE_NULL, 803), /* getTraversableForeachThrowPoint return */ reg::packed(321, 0, 327), /* addDestructureTaggedUnionConditionalHolders $scope */ - reg::packed(842, 0, 571), /* addDestructureTaggedUnionConditionalHolders $iterableValueType */ - reg::packed(860, 0, 865), /* addDestructureTaggedUnionConditionalHolders $list */ + reg::packed(871, 0, 571), /* addDestructureTaggedUnionConditionalHolders $iterableValueType */ + reg::packed(889, 0, 894), /* addDestructureTaggedUnionConditionalHolders $list */ reg::packed(258, 0, 327), /* addDestructureTaggedUnionConditionalHolders return */ }; using Sig = reg::Sig; @@ -137,9 +140,10 @@ inline constexpr sigtab::Sig supports = { { 259 /* supports */, 1, 5, 1, 6, ZEND inline constexpr sigtab::Sig processStmt = { { 503 /* processStmt */, 6, 7, 6, 13, ZEND_ACC_PUBLIC } }; inline constexpr sigtab::Sig enterForeach = { { 608 /* enterForeach */, 8, 14, 8, 22, ZEND_ACC_PRIVATE } }; inline constexpr sigtab::Sig addIterateeMemberConditionalHolders = { { 674 /* addIterateeMemberConditionalHolders */, 5, 23, 5, 28, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig tryProcessUnrolledConstantArrayForeach = { { 726 /* tryProcessUnrolledConstantArrayForeach */, 7, 29, 7, 36, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig getTraversableForeachThrowPoint = { { 810 /* getTraversableForeachThrowPoint */, 3, 37, 3, 40, ZEND_ACC_PRIVATE } }; -inline constexpr sigtab::Sig addDestructureTaggedUnionConditionalHolders = { { 891 /* addDestructureTaggedUnionConditionalHolders */, 3, 41, 3, 44, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig writesOffsetWithLoopVariable = { { 710 /* writesOffsetWithLoopVariable */, 1, 29, 1, 30, ZEND_ACC_PRIVATE | ZEND_ACC_STATIC } }; +inline constexpr sigtab::Sig tryProcessUnrolledConstantArrayForeach = { { 755 /* tryProcessUnrolledConstantArrayForeach */, 7, 31, 7, 38, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig getTraversableForeachThrowPoint = { { 839 /* getTraversableForeachThrowPoint */, 3, 39, 3, 42, ZEND_ACC_PRIVATE } }; +inline constexpr sigtab::Sig addDestructureTaggedUnionConditionalHolders = { { 920 /* addDestructureTaggedUnionConditionalHolders */, 3, 43, 3, 46, ZEND_ACC_PRIVATE } }; } // namespace sig } // namespace ptdecl::ForeachHandler From c40499feb11ef4c7c4b6a3977922e7b82fc58476 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Thu, 3 Dec 2026 13:03:20 +0000 Subject: [PATCH 3/3] Test that shapes not known to be lists count once toward the union shapes limit Amp-Thread-ID: https://ampcode.com/threads/T-01a23b0c-2623-7217-8dd2-922690c49d46 --- .../Analyser/nsrt/union-shapes-limit.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/PHPStan/Analyser/nsrt/union-shapes-limit.php diff --git a/tests/PHPStan/Analyser/nsrt/union-shapes-limit.php b/tests/PHPStan/Analyser/nsrt/union-shapes-limit.php new file mode 100644 index 00000000000..bd9d0807925 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/union-shapes-limit.php @@ -0,0 +1,18 @@ +