Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/Analyser/StmtHandler/TryCatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function processStmt(
$branchScopeResult = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $scope, $storage, $nodeCallback, $context);
$branchScope = $branchScopeResult->getScope();
$finalScope = $branchScopeResult->isAlwaysTerminating() ? null : $branchScope;
// what the template argument observation collected in every block,
// including the ones that end by leaving the try-catch
$blockScopes = [$branchScope];

$exitPoints = [];
$finallyExitPoints = [];
Expand Down Expand Up @@ -246,6 +249,7 @@ public function processStmt(

$catchScopeResult = $nodeScopeResolver->processStmtNodesInternal($catchNode, $catchNode->stmts, $catchScope->enterCatchType($catchType, $variableName), $storage, $nodeCallback, $context);
$catchScopeForFinally = $catchScopeResult->getScope();
$blockScopes[] = $catchScopeForFinally;
$catchFlows[] = [$originalCatchType, VariableFlow::sequence($catchNode->var !== null ? VariableFlowBuilder::targetWrite($catchNode->var, VariableWrite::KIND_CATCH, $catchScopeForFinally, $storage) : null, $catchScopeResult->getVariableFlow())];

$finalScope = $catchScopeResult->isAlwaysTerminating() ? $finalScope : $catchScopeResult->getScope()->mergeWith($finalScope);
Expand Down Expand Up @@ -297,6 +301,7 @@ public function processStmt(
$throwPointsForLater = array_merge($throwPointsForLater, $finallyResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $finallyResult->getImpurePoints());
$finallyScope = $finallyResult->getScope();
$blockScopes[] = $finallyScope;
$finalScope = $finallyResult->isAlwaysTerminating() ? $finalScope : $finalScope->processFinallyScope($finallyScope, $originalFinallyScope);
if (!$finallyResult->isAlwaysTerminating()) {
// the finally block runs after the exit point, so its changes are
Expand Down Expand Up @@ -329,6 +334,10 @@ public function processStmt(
$exitPoints = array_merge($exitPoints, $finallyResult->getExitPoints());
}

foreach ($blockScopes as $blockScope) {
$finalScope = $finalScope->addTemplateArgumentConstraints($blockScope->getTemplateArgumentConstraints());
}

return new InternalStatementResult($finalScope, hasYield: $hasYield, isAlwaysTerminating: $alwaysTerminating, exitPoints: $exitPoints, throwPoints: array_merge($throwPoints, $throwPointsForLater), impurePoints: $impurePoints, variableFlow: VariableFlow::tryCatch($branchScopeResult->getVariableFlow(), $catchFlows, $finallyFlow));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
final class TurboExtensionEnabler
{

public const EXPECTED_EXTENSION_VERSION = '252c8dc';
public const EXPECTED_EXTENSION_VERSION = 'c3d80bb';

private static bool $active = false;

Expand Down
71 changes: 71 additions & 0 deletions tests/PHPStan/Analyser/nsrt/template-argument-try-catch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace TemplateArgumentTryCatch;

use function PHPStan\Testing\assertType;

/** @template T */
class Collection
{

/** @param list<T> $items */
public function __construct(array $items)
{
}

}

/** @param Collection<int> $collection */
function takeInts(Collection $collection): void
{
}

function mayThrow(): void
{
}

function catchThatThrows(): void
{
try {
mayThrow();
} catch (\Exception $e) {
$collection = new Collection([1]);
takeInts($collection);
assertType('TemplateArgumentTryCatch\Collection<int>', $collection);
throw $e;
}
}

function catchThatReturns(): void
{
try {
mayThrow();
} catch (\Exception $e) {
$collection = new Collection([1]);
takeInts($collection);
assertType('TemplateArgumentTryCatch\Collection<int>', $collection);
return;
}
}

function finallyBlock(): void
{
try {
mayThrow();
} finally {
$collection = new Collection([1]);
takeInts($collection);
assertType('TemplateArgumentTryCatch\Collection<int>', $collection);
}
}

function tryThatThrows(): void
{
try {
$collection = new Collection([1]);
takeInts($collection);
assertType('TemplateArgumentTryCatch\Collection<int>', $collection);
throw new \Exception();
} catch (\Exception $e) {
}
}
13 changes: 13 additions & 0 deletions turbo-ext/src/TryCatchHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class TryCatchHandler
bool alwaysTerminating;
if (UNEXPECTED(!pt_internal_statement_result_is_always_terminating(branch, alwaysTerminating))) return zv::Val();
zv::Val finalScope = alwaysTerminating ? zv::Val::null() : zv::Val::copyOf(branchScope.ref());
// what the template argument observation collected in every block,
// including the ones that end by leaving the try-catch
zv::Arr blockScopes = zv::Arr::empty();
blockScopes.push(zv::Val::copyOf(branchScope.ref()));

zv::Arr exitPoints = zv::Arr::empty();
zv::Arr finallyExitPoints = zv::Arr::empty();
Expand Down Expand Up @@ -489,6 +493,7 @@ class TryCatchHandler
if (UNEXPECTED(resultScope == NULL)) return zv::Val();
catchScopeForFinally = zv::Val::copyOf(zv::Ref(resultScope));
}
blockScopes.push(zv::Val::copyOf(catchScopeForFinally.ref()));
{
zv::Val writeFlow = zv::Val::null();
zval *catchVar = ptsh::readNodeProperty(pt_tch_catch_var_site, catchNode, PT_LC("var"));
Expand Down Expand Up @@ -626,6 +631,7 @@ class TryCatchHandler
if (UNEXPECTED(resultScope == NULL)) return zv::Val();
finallyScope = zv::Val::copyOf(zv::Ref(resultScope));
}
blockScopes.push(zv::Val::copyOf(finallyScope.ref()));
if (!finallyAlwaysTerminating) {
zv::Val processed = pt_mutating_scope_process_finally_scope(Z_OBJ_P(finalScope.raw()), Z_OBJ_P(finallyScope.raw()), Z_OBJ_P(originalFinallyScope.raw()));
if (UNEXPECTED(processed.isUndef())) return zv::Val();
Expand Down Expand Up @@ -695,6 +701,13 @@ class TryCatchHandler
variableFlow = pt_variable_flow_try_catch(bodyFlow, catchFlows.raw(), finallyFlow.raw());
if (UNEXPECTED(variableFlow.isUndef())) return zv::Val();
}
for (auto entry : blockScopes.arrRef()) {
zv::Val constraints = pt_mutating_scope_get_template_argument_constraints(Z_OBJ_P(entry.value().raw()));
if (UNEXPECTED(constraints.isUndef())) return zv::Val();
zv::Val joined = pt_mutating_scope_add_template_argument_constraints(Z_OBJ_P(finalScope.raw()), constraints.raw());
if (UNEXPECTED(joined.isUndef())) return zv::Val();
finalScope = std::move(joined);
}
return pt_internal_statement_result_new(finalScope.raw(), hasYield, alwaysTerminating, exitPoints.raw(), throwPoints.raw(), impurePoints.raw(), NULL, variableFlow.raw());
}

Expand Down
Loading