Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()) {
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15757.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace Bug15757Nsrt;

use function PHPStan\Testing\assertType;

/**
* @template K of array-key
* @template T
* @template U
* @param array<K, T> $array
* @param callable(T, K): U $fn
* @return array<K, U>
*/
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);
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'], [
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-15757.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug15757;

/**
* @template K of array-key
* @template T
* @template U
* @param array<K, T> $array
* @param callable(T, K): U $fn
* @return array<K, U>
*/
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));
}
12 changes: 6 additions & 6 deletions turbo-ext/src/ClosureTypeResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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++;

Expand All @@ -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)) {
Expand Down
Loading