Permalink
Browse files

Fix #665: Ensure that the ternary operator promotes temporary variables

  • Loading branch information...
steffen
steffen committed Jun 1, 2015
1 parent 5296015 commit 1329b386a0126c864d2efbe28c3f9da01b1d3fca
Showing with 19 additions and 1 deletion.
  1. +4 −1 Library/Operators/Other/TernaryOperator.php
  2. +14 −0 test/ternary.zep
  3. +1 −0 unit-tests/Extension/TernaryTest.php
@@ -44,9 +44,12 @@ class TernaryOperator extends BaseOperator
public function compile($expression, CompilationContext $compilationContext)
{
/**
* This variable is used to check if the compound and expression is evaluated as true or false
* This variable is used to check if the compound and expression is evaluated as true or false:
* Ensure that newly allocated variables are local-only (setReadOnly)
*/
$this->setReadOnly(false);
$returnVariable = $this->getExpected($compilationContext, $expression, false);
/* Make sure that passed variables (passed symbol variables) are promoted */
$returnVariable->setLocalOnly(false);
if ($returnVariable->getType() != 'variable' || $returnVariable->getName() == 'return_value') {
@@ -32,6 +32,20 @@ class Ternary
//return gettype(typeof a == "resource" ? "unknown": false);
}
/**
* @link https://github.com/phalcon/zephir/issues/665
*/
public function testTernaryWithPromotedTemporaryVariable()
{
var var2, var3;
let var2 = ["_b_","_c_"];
let var3 = explode("_", isset(var2[1]) ? var2[1] : "");
return var3;
}
/**
* @link https://github.com/phalcon/zephir/issues/297
*/
@@ -28,6 +28,7 @@ public function testTernary()
$this->assertEquals('foo', $t->testTernary2(true));
$this->assertEquals('bar', $t->testTernary2(false));
$this->assertEquals(3, $t->testTernaryAfterLetVariable());
$this->assertEquals(array('', 'c', ''), $t->testTernaryWithPromotedTemporaryVariable());
}
/*public function testComplex()

0 comments on commit 1329b38

Please sign in to comment.