diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index a2c0e5adb02..32e84343d1c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -561,6 +561,7 @@ private static function applyCatchContextsForFinallyContext( $context->vars_in_scope[$var_id] = Type::combineUnionTypeArray($types, $codebase); $context->vars_in_scope[$var_id]->possibly_undefined = $possibly_undefined; $context->vars_in_scope[$var_id]->possibly_undefined_from_try = false; + $context->vars_possibly_in_scope[$var_id] = true; } } @@ -630,6 +631,7 @@ private static function applyCatchContextsForOuterContext( $context->vars_in_scope[$var_id] = Type::combineUnionTypeArray($types, $codebase); $context->vars_in_scope[$var_id]->possibly_undefined = $possibly_undefined; $context->vars_in_scope[$var_id]->possibly_undefined_from_try = false; + $context->vars_possibly_in_scope[$var_id] = true; } if (count($leaving_catches) < $catch_block_count) { @@ -645,6 +647,7 @@ private static function applyCatchContextsForOuterContext( // Set all variables to their types at the end of the `try` block, since // it would have to complete successfully for execution to continue. $context->vars_in_scope = $end_of_try_context->vars_in_scope; + $context->vars_possibly_in_scope = $end_of_try_context->vars_possibly_in_scope; $context->references_in_scope = $end_of_try_context->references_in_scope; } } diff --git a/tests/TryCatchTest.php b/tests/TryCatchTest.php index bba89cb602d..ac027890693 100644 --- a/tests/TryCatchTest.php +++ b/tests/TryCatchTest.php @@ -1082,6 +1082,22 @@ function maybeThrow(): void {} '$foo===' => '1|2', ], ], + 'tryInsideIfPossiblyUndefined' => [ + 'code' => '