Skip to content
Permalink
Browse files

Use the correct types for AssignmentRef (#2078)

This change assigns the type of the rhs expression to the variable that
will become a reference, as well as preventing clobbering of the rhs if
it is an already-typed variable.

Fixes: #2077
  • Loading branch information...
lhchavez authored and muglug committed Aug 28, 2019
1 parent 095ea1a commit 1f0aca00b3194b6643c92d89dcb7659f670db796
@@ -855,14 +855,15 @@ public static function analyzeAssignmentRef(
PhpParser\Node\Expr\AssignRef $stmt,
Context $context
) {
if (self::analyze(
$assignment_type = self::analyze(
$statements_analyzer,
$stmt->var,
$stmt->expr,
null,
$context,
$stmt->getDocComment()
) === false) {
);
if ($assignment_type === false) {
return false;
}
@@ -879,11 +880,11 @@ public static function analyzeAssignmentRef(
);
if ($lhs_var_id) {
$context->vars_in_scope[$lhs_var_id] = Type::getMixed();
$context->vars_in_scope[$lhs_var_id] = $assignment_type;
$context->hasVariable($lhs_var_id, $statements_analyzer);
}
if ($rhs_var_id) {
if ($rhs_var_id && !isset($context->vars_in_scope[$rhs_var_id])) {
$context->vars_in_scope[$rhs_var_id] = Type::getMixed();
}
}
@@ -58,6 +58,27 @@ function foo2(?string &$u, ?string &$v): void {}
/** @var mixed */
$b = $a;',
],
'referenceAssignmentArray' => [
'<?php
$matrix = [
[1, 0],
[0, 1],
];
$row =& $matrix[0];
echo $row[0];',
],
'referenceAssignmentLhs' => [
'<?php
$a = 1;
$b =& $a;
echo $b;',
],
'referenceAssignmentRhs' => [
'<?php
$a = 1;
$b =& $a;
echo $a;',
],
];
}
@@ -74,7 +74,7 @@ function foo() {
$var = 0;
($a =& $var) ?? "hello";',
'assertions' => [
'$a' => 'mixed',
'$a' => 'int',
],
],
'spaceship' => [

0 comments on commit 1f0aca0

Please sign in to comment.
You can’t perform that action at this time.