Do not propagate isAlwaysTerminating from immediately-invoked callable arguments to the outer call#5604
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…ble arguments to the outer call - Remove `isAlwaysTerminating` propagation from closure, arrow function, and other callable arguments in `NodeScopeResolver::processArgs()` when the parameter is marked as immediately-invoked - Remove now-unused `ProcessClosureResult::isAlwaysTerminating()` method and its backing field - Update `ExpressionResultTest` expectations for `call_user_func` with never-returning callbacks (minor false negative trade-off) - "Immediately invoked" means the callback runs during the function's execution, not that it is unconditionally called on every code path - Throw points and impure points still propagate correctly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a closure or arrow function that always terminates (returns
never, e.g.fn() => throw new Error()) was passed as an@param-immediately-invoked-callableparameter, PHPStan incorrectly concluded the entire outer function call always terminates. This caused false positive "Unreachable statement" errors on code following the call.The fix stops propagating
isAlwaysTerminatingfrom callback arguments to the outer call inNodeScopeResolver::processArgs(). "Immediately invoked" means the callback runs during the function's execution (not stored for later), but it does NOT guarantee the callback is called on every code path —array_filteronly calls the callback per element (empty array = never called),Choice::proceedmay call only one of two callbacks, etc.Changes
$isAlwaysTerminatingpropagation insrc/Analyser/NodeScopeResolver.phpfor all three callable argument branches:ProcessClosureResult::isAlwaysTerminating()method and$isAlwaysTerminatingfield fromsrc/Analyser/ProcessClosureResult.php$isAlwaysTerminatingcomputation inprocessClosureNode()(was line 2792)tests/PHPStan/Analyser/ExpressionResultTest.phpexpectations forcall_user_func(fn() => exit())andcall_user_func(function() { exit(); })— these are nowfalse(minor false negative, acceptable trade-off)Root cause
NodeScopeResolver::processArgs()was propagating theisAlwaysTerminatingflag from immediately-invoked callback results to the outer function call'sExpressionResult. When any callback argument was always-terminating (returningnever), the entire function call was marked as always-terminating. This is incorrect because "immediately invoked" does not mean "always invoked" — the function may only call the callback conditionally.Three parallel code paths had the same bug:
$closureResult->isAlwaysTerminating()$arrowFunctionResult->isAlwaysTerminating()$returnType instanceof NeverType && $returnType->isExplicit()Throw points and impure points are still correctly propagated (the callback CAN throw, so the function call might throw), only the "always terminates" assertion was wrong.
Analogous cases probed
NeverType, not callback arguments — already correct.(fn() => exit())()): These are processed asFuncCallwith the closure as the callee expression, not as callback arguments — not affected by this fix.@param-later-invoked-callable:callCallbackImmediately()returnsfalsefor these, so nothing propagates — already correct.set_error_handler/pcntl_signal: Not marked as immediately-invoked — already correct (verified by existing tests bug-13288, bug-13311, bug-13331).Test
tests/PHPStan/Rules/DeadCode/data/bug-14582.phpcovering:Choice::proceed()pattern with@param-immediately-invoked-callablearray_filterwith never-returning arrow functionarray_mapwith never-returning arrow functionusortwith never-returning arrow functionarray_filterwith never-returning closuretests/PHPStan/Analyser/nsrt/bug-14582.phpverifying type inference works correctly afterarray_filter/array_mapwith never-returning callbacksFixes phpstan/phpstan#14582