From 7e825c235fbc5a98f10e32e2d8f6fff9d87a51e6 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Mon, 16 Nov 2026 15:47:11 +0100 Subject: [PATCH] Walk a closure body for its type on a duplicate of the caller's storage ClosureTypeResolver::getClosureType() walked the body on an empty ExpressionResultStorage, even when the caller handed it the storage of the analysis in progress. An invocation of a closure whose signature is inferred from its usages (closureSignaturesFromUsages) finds the closure's creation result in that storage to return what the body returns for narrower arguments. With an empty storage it found nothing, so the walk typed `$doubleFn(1)` as the joined signature's return type (`0..18`) while the walk of the argument itself typed it as `2`. The callable parameter check then compared two different types of the same arrow function. The walk now runs on a duplicate of the given storage, as on-demand processing does: its own results stay in the duplicate, and nodes outside the body resolve through the fallback. The turbo-ext mirror gets the same change. Co-Authored-By: Claude Opus 5.5 --- .../Helper/ClosureTypeResolver.php | 4 +- tests/PHPStan/Analyser/nsrt/bug-15757.php | 44 +++++++++++++++++++ .../CallToFunctionParametersRuleTest.php | 5 +++ .../Rules/Functions/data/bug-15757.php | 28 ++++++++++++ turbo-ext/src/ClosureTypeResolver.cpp | 12 ++--- 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15757.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15757.php diff --git a/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php b/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php index 88fe7a353ea..a48fa164bd5 100644 --- a/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php +++ b/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php @@ -175,7 +175,7 @@ public function getClosureType( $arrowFunctionImpurePoints = []; $invalidateExpressions = []; self::$resolveClosureTypeDepth++; - $walkStorage = new ExpressionResultStorage(); + $walkStorage = $storage !== null ? $storage->duplicate() : new ExpressionResultStorage(); $arrowScope->pushExpressionResultStorage($walkStorage); try { $arrowFunctionExprResult = $this->nodeScopeResolver->processExprNode( @@ -232,7 +232,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu $invalidateExpressions = []; try { - $walkStorage = new ExpressionResultStorage(); + $walkStorage = $storage !== null ? $storage->duplicate() : new ExpressionResultStorage(); $closureScope->pushExpressionResultStorage($walkStorage); $closureStatementResult = $this->nodeScopeResolver->processStmtNodesInternal($expr, $expr->stmts, $closureScope, $walkStorage, static function (Node $node, Scope $scope) use ($closureScope, &$closureReturnStatements, &$closureYieldStatements, &$closureExecutionEnds, &$closureImpurePoints, &$invalidateExpressions): void { if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) { diff --git a/tests/PHPStan/Analyser/nsrt/bug-15757.php b/tests/PHPStan/Analyser/nsrt/bug-15757.php new file mode 100644 index 00000000000..815dd578f44 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15757.php @@ -0,0 +1,44 @@ + $array + * @param callable(T, K): U $fn + * @return array + */ +function mapx(array $array, callable $fn): array +{ + $result = []; + foreach ($array as $key => $value) { + $result[$key] = $fn($value, $key); + } + return $result; +} + +/** + * @template T + * @param callable(): T $fn + * @return T + */ +function call(callable $fn) +{ + return $fn(); +} + +function probe(): void +{ + $indexes = range(0, 9); + $doubleFn = static fn (int $value): int => $value * 2; + assertType('array<0|1|2|3|4|5|6|7|8|9, 0|2|4|6|8|10|12|14|16|18>', mapx($indexes, static fn (int $index): int => $doubleFn($index))); + assertType('2', $doubleFn(1)); + assertType('2', call(static fn () => $doubleFn(1))); + assertType('2', call(static function () use ($doubleFn) { + return $doubleFn(1); + })); +} diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 8b60dce77cd..e7ba7825aa2 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -3272,6 +3272,11 @@ public function testBug15168(): void $this->analyse([__DIR__ . '/data/bug-15168.php'], []); } + public function testBug15757(): void + { + $this->analyse([__DIR__ . '/data/bug-15757.php'], []); + } + public function testBug6732(): void { $this->analyse([__DIR__ . '/data/bug-6732.php'], [ diff --git a/tests/PHPStan/Rules/Functions/data/bug-15757.php b/tests/PHPStan/Rules/Functions/data/bug-15757.php new file mode 100644 index 00000000000..8f1c33d67af --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15757.php @@ -0,0 +1,28 @@ + $array + * @param callable(T, K): U $fn + * @return array + */ +function mapx(array $array, callable $fn): array +{ + $result = []; + foreach ($array as $key => $value) { + $result[$key] = $fn($value, $key); + } + return $result; +} + +function probe(): void +{ + $indexes = range(0, 9); + $doubleFn = static fn (int $value): int => $value * 2; + mapx($indexes, static fn (int $index): int => $doubleFn($index)); + mapx($indexes, static fn (int $index): int => $index - $doubleFn(1)); +} diff --git a/turbo-ext/src/ClosureTypeResolver.cpp b/turbo-ext/src/ClosureTypeResolver.cpp index ff537665633..2fa208bc71e 100644 --- a/turbo-ext/src/ClosureTypeResolver.cpp +++ b/turbo-ext/src/ClosureTypeResolver.cpp @@ -362,8 +362,8 @@ class ClosureTypeResolver if (UNEXPECTED(!countClosureTypeBodyWalk())) return zv::Val(); int isArrowFunction = ptclosure::instanceOf(expr, PT_CLASS_ARROW_FUNCTION); if (UNEXPECTED(isArrowFunction < 0)) return zv::Val(); - if (isArrowFunction) return walkArrowFunction(scope, expr, p, cacheKey.get()); - return walkClosure(scope, expr, p, cacheKey.get()); + if (isArrowFunction) return walkArrowFunction(scope, expr, p, cacheKey.get(), storage); + return walkClosure(scope, expr, p, cacheKey.get(), storage); } /* Mirrors buildClosureTypeForClosure() (the nullable ones NULL for null) */ @@ -1339,7 +1339,7 @@ class ClosureTypeResolver } /* the arrow-function body walk of getClosureType() */ - zv::Val walkArrowFunction(zval *scope, zval *expr, Parameters &p, zend_string *cacheKey) + zv::Val walkArrowFunction(zval *scope, zval *expr, Parameters &p, zend_string *cacheKey, zval *storage) { zval *body = ptclosure::prop(ptclosure::arrowExprSite, expr, PT_LC("expr")); if (UNEXPECTED(body == NULL)) return zv::Val(); @@ -1356,7 +1356,7 @@ class ClosureTypeResolver zv::Val arrowFunctionImpurePoints = ptsh::newArrayReference(); zv::Val invalidateExpressions = ptsh::newArrayReference(); pt_ctr_resolve_closure_type_depth++; - zv::Val walkStorage = pt_expression_result_storage_new(); + zv::Val walkStorage = storage != NULL ? pt_expression_result_storage_duplicate(storage) : pt_expression_result_storage_new(); if (UNEXPECTED(walkStorage.isUndef())) return zv::Val(); if (UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(arrowScope.raw()), walkStorage.raw()))) return zv::Val(); zv::Val exprResult; @@ -1400,7 +1400,7 @@ class ClosureTypeResolver } /* the closure body walk of getClosureType() */ - zv::Val walkClosure(zval *scope, zval *expr, Parameters &p, zend_string *cacheKey) + zv::Val walkClosure(zval *scope, zval *expr, Parameters &p, zend_string *cacheKey, zval *storage) { pt_ctr_resolve_closure_type_depth++; @@ -1421,7 +1421,7 @@ class ClosureTypeResolver zv::Val walkStorage; zv::Val closureStatementResult; { - walkStorage = pt_expression_result_storage_new(); + walkStorage = storage != NULL ? pt_expression_result_storage_duplicate(storage) : pt_expression_result_storage_new(); if (EXPECTED(!walkStorage.isUndef()) && EXPECTED(pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(closureScope.raw()), walkStorage.raw()))) { zval *stmts = ptclosure::prop(ptclosure::stmtsSite, expr, PT_LC("stmts")); if (EXPECTED(stmts != NULL)) {