Skip to content

Commit 040f65e

Browse files
ondrejmirtesclaude
andcommitted
Pin the node in object-id keyed per-file caches
PHP hands out the spl_object_id() of a freed object again. The file's own AST nodes live for the whole analysis, but a synthetic node built and dropped mid-file frees its id, and the next node allocated may get it: ClosureTypeResolver then answered an arrow function's cached generator return type for an unrelated closure sharing the id (ExpressionResultTest #23 under the paratest wrapper, where the arrow function of an earlier data set had been freed). Each entry now pins the node it was built for and answers for that very node only; the ternary and match capture maps keyed the same way get the same check. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R7pqAkCx4xP6WYxBo2nMJE
1 parent f061789 commit 040f65e

3 files changed

Lines changed: 41 additions & 18 deletions

File tree

‎src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ final class ClosureTypeResolver implements PerFileAnalysisResettable
7171
* file's analysis ends - the per-file reset releases the Types and
7272
* throw/impure points with the rest of the file's result graph.
7373
*
74-
* Keyed by the closure node's spl_object_id() - see
75-
* TernaryHandler::$capturedResults for the lifetime/collision reasoning.
74+
* Keyed by the closure node's spl_object_id(), which PHP hands out again
75+
* once a node is freed - a synthetic closure built and dropped mid-file
76+
* (see TernaryHandler::$capturedResults) - so each entry pins the node it
77+
* was built for and findCachedTypes() answers for that very node only.
7678
*
77-
* @var array<int, array<string, array{returnType: Type, throwPoints: SimpleThrowPoint[], impurePoints: SimpleImpurePoint[], invalidateExpressions: InvalidateExprNode[], usedVariables: string[]}>>
79+
* @var array<int, array{expr: Node\Expr\Closure|ArrowFunction, types: array<string, array{returnType: Type, throwPoints: SimpleThrowPoint[], impurePoints: SimpleImpurePoint[], invalidateExpressions: InvalidateExprNode[], usedVariables: string[]}>}>
7880
*/
7981
private array $cachedTypes = [];
8082

@@ -90,6 +92,19 @@ public function resetFileAnalysisState(): void
9092
$this->cachedTypes = [];
9193
}
9294

95+
/**
96+
* @return array<string, array{returnType: Type, throwPoints: SimpleThrowPoint[], impurePoints: SimpleImpurePoint[], invalidateExpressions: InvalidateExprNode[], usedVariables: string[]}>
97+
*/
98+
private function findCachedTypes(Node\Expr\Closure|ArrowFunction $expr): array
99+
{
100+
$entry = $this->cachedTypes[spl_object_id($expr)] ?? null;
101+
if ($entry === null || $entry['expr'] !== $expr) {
102+
return [];
103+
}
104+
105+
return $entry['types'];
106+
}
107+
93108
/**
94109
* Resolves a closure/arrow function type by walking its body itself. Used by
95110
* the paths that have NO prior walk to read return/yield types from - a
@@ -127,7 +142,7 @@ public function getClosureType(
127142
);
128143
}
129144

130-
$cachedTypes = $this->cachedTypes[spl_object_id($expr)] ?? [];
145+
$cachedTypes = $this->findCachedTypes($expr);
131146
$cacheKey = $this->closureContextCacheKey($scope, $expr, $callableParameters, $parameters);
132147
if (array_key_exists($cacheKey, $cachedTypes)) {
133148
return $this->createClosureTypeFromCache($expr, $parameters, $isVariadic, $cachedTypes[$cacheKey]);
@@ -903,7 +918,7 @@ private function assembleClosureType(
903918
$throwPointsForClosureType = array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? SimpleThrowPoint::createExplicit($throwPoint->getType(), $throwPoint->canContainAnyThrowable()) : SimpleThrowPoint::createImplicit(), $throwPoints);
904919
$impurePointsForClosureType = array_map(static fn (ImpurePoint $impurePoint) => new SimpleImpurePoint($impurePoint->getIdentifier(), $impurePoint->getDescription(), $impurePoint->isCertain()), $impurePoints);
905920

906-
$cachedTypes = $this->cachedTypes[spl_object_id($expr)] ?? [];
921+
$cachedTypes = $this->findCachedTypes($expr);
907922
$cacheKey ??= $this->closureContextCacheKey($scope, $expr, null, $parameters);
908923
$cachedTypes[$cacheKey] = [
909924
'returnType' => $returnType,
@@ -912,7 +927,7 @@ private function assembleClosureType(
912927
'invalidateExpressions' => $invalidateExpressions,
913928
'usedVariables' => $usedVariables,
914929
];
915-
$this->cachedTypes[spl_object_id($expr)] = $cachedTypes;
930+
$this->cachedTypes[spl_object_id($expr)] = ['expr' => $expr, 'types' => $cachedTypes];
916931

917932
$mustUseReturnValue = TrinaryLogic::createNo();
918933
foreach ($expr->attrGroups as $attrGroup) {

‎src/Analyser/ExprHandler/MatchHandler.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ final class MatchHandler implements ExprHandler, PerFileAnalysisResettable
6161

6262
/**
6363
* Keyed by the match node's spl_object_id() - see
64-
* TernaryHandler::$capturedResults for the lifetime/collision reasoning.
64+
* TernaryHandler::$capturedResults for the lifetime/collision reasoning;
65+
* the entry pins the node it was captured for.
6566
*
66-
* @var array<int, list<array{ExpressionResult, MutatingScope, Expr}>>
67+
* @var array<int, array{Match_, list<array{ExpressionResult, MutatingScope, Expr}>}>
6768
*/
6869
private array $capturedArmResults = [];
6970

@@ -101,12 +102,13 @@ public function supports(Expr $expr): bool
101102
*/
102103
public function getCapturedArmScopesAndTypes(Match_ $expr): ?array
103104
{
104-
if (!isset($this->capturedArmResults[spl_object_id($expr)])) {
105+
$entry = $this->capturedArmResults[spl_object_id($expr)] ?? null;
106+
if ($entry === null || $entry[0] !== $expr) {
105107
return null;
106108
}
107109

108110
$pairs = [];
109-
foreach ($this->capturedArmResults[spl_object_id($expr)] as [$armResult, $bodyScope]) {
111+
foreach ($entry[1] as [$armResult, $bodyScope]) {
110112
$pairs[] = [$bodyScope, $armResult->getType()];
111113
}
112114

@@ -490,7 +492,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
490492
$expr->cond = $expr->cond->getExpr();
491493
}
492494

493-
$this->capturedArmResults[spl_object_id($expr)] = $armTypeResults;
495+
$this->capturedArmResults[spl_object_id($expr)] = [$expr, $armTypeResults];
494496

495497
return $this->expressionResultFactory->create(
496498
$scope,

‎src/Analyser/ExprHandler/TernaryHandler.php‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ final class TernaryHandler implements ExprHandler, PerFileAnalysisResettable
3535
{
3636

3737
/**
38-
* Keyed by the ternary node's spl_object_id(). The keys are AST nodes that
39-
* live for the whole file's analysis (the parser cache retains them), so
40-
* ids of live entries never collide; the per-file reset empties the map
41-
* before another file could reuse them.
38+
* Keyed by the ternary node's spl_object_id(). The file's own AST nodes
39+
* live for the whole analysis (the parser cache retains them), but a
40+
* synthetic node built and dropped mid-file frees its id for reuse - so
41+
* each entry pins the node it was captured for and answers for that very
42+
* node only; the per-file reset empties the map.
4243
*
43-
* @var array<int, array{ExpressionResult, ExpressionResult, ExpressionResult}>
44+
* @var array<int, array{Ternary, ExpressionResult, ExpressionResult, ExpressionResult}>
4445
*/
4546
private array $capturedResults = [];
4647

@@ -70,7 +71,12 @@ public function supports(Expr $expr): bool
7071
*/
7172
public function getCapturedResults(Ternary $expr): ?array
7273
{
73-
return $this->capturedResults[spl_object_id($expr)] ?? null;
74+
$entry = $this->capturedResults[spl_object_id($expr)] ?? null;
75+
if ($entry === null || $entry[0] !== $expr) {
76+
return null;
77+
}
78+
79+
return [$entry[1], $entry[2], $entry[3]];
7480
}
7581

7682
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
@@ -108,7 +114,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
108114
}
109115

110116
if ($ifResult !== null) {
111-
$this->capturedResults[spl_object_id($expr)] = [$ternaryCondResult, $ifResult, $elseResult];
117+
$this->capturedResults[spl_object_id($expr)] = [$expr, $ternaryCondResult, $ifResult, $elseResult];
112118
}
113119

114120
$condType = $ternaryCondResult->getType();

0 commit comments

Comments
 (0)