1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Ensure we flush out re-assigned vars also referenced

Fixes #4488
This commit is contained in:
Matt Brown 2020-11-06 12:51:41 -05:00 committed by Daniil Gentili
parent 57cd29ee9d
commit 578e65820a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 31 additions and 1 deletions

View File

@ -513,7 +513,7 @@ class Context
* @param Clause[] $clauses * @param Clause[] $clauses
* @param array<string, bool> $changed_var_ids * @param array<string, bool> $changed_var_ids
* *
* @return array{0: list<Clause>, list<Clause>} * @return array{list<Clause>, list<Clause>}
* *
* @psalm-pure * @psalm-pure
*/ */

View File

@ -250,6 +250,9 @@ class OrAnalyzer
$pre_referenced_var_ids = $right_context->referenced_var_ids; $pre_referenced_var_ids = $right_context->referenced_var_ids;
$right_context->referenced_var_ids = []; $right_context->referenced_var_ids = [];
$pre_assigned_var_ids = $right_context->assigned_var_ids;
$right_context->assigned_var_ids = [];
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $right_context) === false) { if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->right, $right_context) === false) {
return false; return false;
} }
@ -257,6 +260,9 @@ class OrAnalyzer
$right_referenced_var_ids = $right_context->referenced_var_ids; $right_referenced_var_ids = $right_context->referenced_var_ids;
$right_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $right_referenced_var_ids); $right_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $right_referenced_var_ids);
$right_assigned_var_ids = $right_context->assigned_var_ids;
$right_context->assigned_var_ids = array_merge($pre_assigned_var_ids, $right_assigned_var_ids);
$right_cond_id = \spl_object_id($stmt->right); $right_cond_id = \spl_object_id($stmt->right);
$right_clauses = FormulaGenerator::getFormula( $right_clauses = FormulaGenerator::getFormula(
@ -268,6 +274,11 @@ class OrAnalyzer
$codebase $codebase
); );
$clauses_for_right_analysis = Context::removeReconciledClauses(
$clauses_for_right_analysis,
$right_assigned_var_ids
)[0];
$combined_right_clauses = Algebra::simplifyCNF( $combined_right_clauses = Algebra::simplifyCNF(
array_merge($clauses_for_right_analysis, $right_clauses) array_merge($clauses_for_right_analysis, $right_clauses)
); );

View File

@ -430,6 +430,25 @@ class AssignmentInConditionalTest extends \Psalm\Tests\TestCase
echo $a; echo $a;
}', }',
], ],
'assignmentInBranchWithReference' => [
'<?php
class A {}
function getAOrFalse(bool $b) : A|false {
return false;
}
function foo(A|false $a): void
{
if ($a instanceof A
|| ($a = getAOrFalse($a))
) {
}
}',
[],
[],
'8.0'
],
]; ];
} }