Skip to content

Commit 3ac002b

Browse files
ondrejmirtesclaude
andcommitted
Carry the assigned call's declared return type for by-ref closure uses
`$x = $this->foo(function () use (&$x) {...})` types the by-ref use from the call being assigned - a forward reference read from inside the call's own arguments, which priced the enclosing call on demand (a nested walk of the closure itself). The call handlers now hand the context the declared return type of the acceptor the call was normalized with (template types resolved to their bounds) before processing the arguments, and the by-ref use reads it; a closure right side still resolves through the closure type resolver. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R7pqAkCx4xP6WYxBo2nMJE
1 parent a6b862e commit 3ac002b

5 files changed

Lines changed: 61 additions & 6 deletions

File tree

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,29 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
306306
}
307307

308308
$scopeBeforeArgs = $scope;
309+
if ($parametersAcceptor !== null && $context->getInAssignRightSideExpr() === $expr) {
310+
$context = $context->enterAssignRightSideCallArgs($parametersAcceptor);
311+
}
309312
if ($argsGatherer !== null) {
310313
$nodeScopeResolver->pushNodeGatherer($argsGatherer);
311314
}
312315
try {
313-
$argsResult = $nodeScopeResolver->processArgs($stmt, $functionReflection, null, $variants, $namedArgumentsVariants, $normalizedExpr, $scope, $storage, $nodeCallback, $context);
316+
$argsResult = $argumentsWalkedAhead !== null
317+
// the arguments are processed; the call resolves from the walked
318+
// closure's acceptor, which is the sole variant of its type
319+
? $argumentsWalkedAhead->withResolvedParametersAcceptor($parametersAcceptor)
320+
: $nodeScopeResolver->processArgs($stmt, $functionReflection, null, $variants, $namedArgumentsVariants, $normalizedExpr, $scope, $storage, $nodeCallback, $context);
314321
} finally {
315322
if ($argsGatherer !== null) {
316323
$nodeScopeResolver->popNodeGatherer();
317324
}
318325
}
319326
$resolvedParametersAcceptor = $argsResult->getResolvedParametersAcceptor();
320-
$scope = $argsResult->getScope();
327+
// arguments walked ahead of the callee: the scope already carries them
328+
// and the callee walk's own effects (a closure's by-ref uses) on top
329+
if ($argumentsWalkedAhead === null) {
330+
$scope = $argsResult->getScope();
331+
}
321332
$nodeScopeResolver->processDroppedArgs($stmt, $expr, $normalizedExpr, $scope, $storage, $context);
322333
$hasYield = $argsResult->hasYield();
323334
$throwPoints = array_merge($throwPoints, $argsResult->getThrowPoints());

‎src/Analyser/ExprHandler/MethodCallHandler.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
150150
}
151151

152152
$scopeBeforeArgs = $scope;
153+
if ($parametersAcceptor !== null && $context->getInAssignRightSideExpr() === $expr) {
154+
$context = $context->enterAssignRightSideCallArgs($parametersAcceptor);
155+
}
153156
$argsResult = $nodeScopeResolver->processArgs(
154157
$stmt,
155158
$methodReflection,

‎src/Analyser/ExprHandler/StaticCallHandler.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
248248
$isAlwaysTerminating = $isAlwaysTerminating || ($returnType instanceof NeverType && $returnType->isExplicit());
249249
}
250250
$scopeBeforeArgs = $scope;
251+
if ($parametersAcceptor !== null && $context->getInAssignRightSideExpr() === $expr) {
252+
$context = $context->enterAssignRightSideCallArgs($parametersAcceptor);
253+
}
251254
$argsResult = $nodeScopeResolver->processArgs($stmt, $methodReflection, null, $variants, $namedArgumentsVariants, $normalizedExpr, $scope, $storage, $nodeCallback, $context, $closureBindScopeFactory);
252255
$resolvedParametersAcceptor = $argsResult->getResolvedParametersAcceptor();
253256
$scope = $argsResult->getScope();

‎src/Analyser/ExpressionContext.php‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace PHPStan\Analyser;
44

55
use PhpParser\Node\Expr;
6+
use PHPStan\Reflection\ExtendedParametersAcceptor;
7+
use PHPStan\Reflection\ParametersAcceptor;
8+
use PHPStan\Type\Generic\TemplateTypeHelper;
9+
use PHPStan\Type\Type;
610

711
final class ExpressionContext
812
{
@@ -12,6 +16,8 @@ private function __construct(
1216
private ?string $inAssignRightSideVariableName,
1317
private ?Expr $inAssignRightSideExpr,
1418
private bool $inThrow = false,
19+
private ?Type $inAssignRightSideType = null,
20+
private ?Type $inAssignRightSideNativeType = null,
1521
)
1622
{
1723
}
@@ -32,7 +38,7 @@ public function enterDeep(): self
3238
return $this;
3339
}
3440

35-
return new self(true, $this->inAssignRightSideVariableName, $this->inAssignRightSideExpr, $this->inThrow);
41+
return new self(true, $this->inAssignRightSideVariableName, $this->inAssignRightSideExpr, $this->inThrow, $this->inAssignRightSideType, $this->inAssignRightSideNativeType);
3642
}
3743

3844
public function isDeep(): bool
@@ -42,7 +48,7 @@ public function isDeep(): bool
4248

4349
public function enterThrow(): self
4450
{
45-
return new self($this->isDeep, $this->inAssignRightSideVariableName, $this->inAssignRightSideExpr, true);
51+
return new self($this->isDeep, $this->inAssignRightSideVariableName, $this->inAssignRightSideExpr, true, $this->inAssignRightSideType, $this->inAssignRightSideNativeType);
4652
}
4753

4854
public function isInThrow(): bool
@@ -65,4 +71,33 @@ public function getInAssignRightSideExpr(): ?Expr
6571
return $this->inAssignRightSideExpr;
6672
}
6773

74+
/**
75+
* The call that is the assignment's right side is about to process its
76+
* arguments. A closure argument's by-ref use of the variable being
77+
* assigned reads the call's type from inside those very arguments - a
78+
* forward reference the acceptor's declared return type answers without
79+
* pricing the enclosing call (template types resolve to their bounds).
80+
*/
81+
public function enterAssignRightSideCallArgs(ParametersAcceptor $acceptor): self
82+
{
83+
return new self(
84+
$this->isDeep,
85+
$this->inAssignRightSideVariableName,
86+
$this->inAssignRightSideExpr,
87+
$this->inThrow,
88+
TemplateTypeHelper::resolveToBounds($acceptor->getReturnType()),
89+
TemplateTypeHelper::resolveToBounds($acceptor instanceof ExtendedParametersAcceptor ? $acceptor->getNativeReturnType() : $acceptor->getReturnType()),
90+
);
91+
}
92+
93+
public function getInAssignRightSideType(): ?Type
94+
{
95+
return $this->inAssignRightSideType;
96+
}
97+
98+
public function getInAssignRightSideNativeType(): ?Type
99+
{
100+
return $this->inAssignRightSideNativeType;
101+
}
102+
68103
}

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,10 @@ private function processClosureNodeInternal(
16001600
$inAssignRightSideVariableName === $use->var->name
16011601
&& $inAssignRightSideExpr !== null
16021602
) {
1603-
$inAssignRightSideType = $this->resolveCallableTypeForScope($inAssignRightSideExpr, $scope);
1603+
// a call's type is carried by the context (see
1604+
// ExpressionContext::enterAssignRightSideCallArgs()); a closure
1605+
// right side resolves through the closure type resolver
1606+
$inAssignRightSideType = $context->getInAssignRightSideType() ?? $this->resolveCallableTypeForScope($inAssignRightSideExpr, $scope);
16041607
if ($inAssignRightSideType instanceof ClosureType) {
16051608
$variableType = $inAssignRightSideType;
16061609
} else {
@@ -1611,7 +1614,7 @@ private function processClosureNodeInternal(
16111614
$variableType = TypeCombinator::union($scope->getVariableType($inAssignRightSideVariableName), $inAssignRightSideType);
16121615
}
16131616
}
1614-
$inAssignRightSideNativeType = $this->resolveCallableTypeForScope($inAssignRightSideExpr, $scope->doNotTreatPhpDocTypesAsCertain());
1617+
$inAssignRightSideNativeType = $context->getInAssignRightSideNativeType() ?? $this->resolveCallableTypeForScope($inAssignRightSideExpr, $scope->doNotTreatPhpDocTypesAsCertain());
16151618
if ($inAssignRightSideNativeType instanceof ClosureType) {
16161619
$variableNativeType = $inAssignRightSideNativeType;
16171620
} else {

0 commit comments

Comments
 (0)