fix(compiler): 支持属性和数组元素作为赋值表达式右值

pull/8/head
Yurun 2 months ago
parent df5dd2a5f6
commit 8684d1652c
  1. 4
      src/Php/CompilerBase.php
  2. 6
      src/Php/Parser/AssignOpTrait.php
  3. 50
      tests/aot/basic/multi-var-assign.phpt

@ -1375,8 +1375,8 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
return $this->parseConstFetch($expr);
case 'Expr_Assign':
case 'Expr_AssignRef':
if (!$this->isVarExpr($expr->var)) {
$this->fatalError($expr, 'When an assignment expression serves as an rvalue, it must be an assignment of a variable');
if (!$this->isVarExpr($expr->var) && !$this->isPropertyFetch($expr->var) && !$this->isArrayDimFetch($expr->var)) {
$this->fatalError($expr, 'When an assignment expression serves as an rvalue, it must be an assignment of a variable, property, or array element');
}
return $this->parseExpr($expr);
default:

@ -85,13 +85,13 @@ trait AssignOpTrait
$chain = array_reverse($chain);
$list = [];
$list[] = $this->getIndent() . $tmpVar . ' = ' . $this->parseExpr($next);
$list[] = $tmpVar . ' = ' . $this->parseExpr($next);
$rightVar = new Variable($tmpVar);
foreach ($chain as $var) {
$list[] = $this->getIndent() . $this->parseAssignFinally($var, $rightVar);
$list[] = $this->parseAssignFinally($var, $rightVar);
}
return implode(";\n" . $this->getIndent(), $list);
return '(' . implode(', ', $list) . ')';
}
protected function parseAssign(Expr\Assign $v): string

@ -0,0 +1,50 @@
--TEST--
Multi-variable assignment: property assignment as rvalue in constructor argument expression
--FILE--
<?php
class Test2
{
public function __construct($value2)
{
var_dump($value2);
}
}
class Test
{
public $value;
public $value2;
public $value3;
public $arr = [1];
public function __construct()
{
$this->value = new Test2($this->arr[0] = $this->value2 = 123);
var_dump($this->value2, $this->arr);
$this->value = new Test2($this->arr[0] = $this->value3 = $this->value2 = 456);
var_dump($this->value2, $this->value3, $this->arr);
}
}
function main()
{
new Test;
}
?>
--EXPECT--
int(123)
int(123)
array(1) {
[0]=>
int(123)
}
int(456)
int(456)
int(456)
array(1) {
[0]=>
int(456)
}
Loading…
Cancel
Save