Skip to content

Implement ??= operator #3747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions Zend/tests/assign_coalesce_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
--TEST--
Coalesce assign (??=): Basic behavior
--FILE--
<?php

// Refcounted values
$foo = "fo";
$foo .= "o";
$bar = "ba";
$bar .= "r";

// Identity function used to track single-evaluation
function id($arg) {
echo "id($arg)\n";
return $arg;
}

echo "Simple variables:\n";
$a = 123;
$a ??= 456;
var_dump($a);

$b = null;
$b ??= $foo;
var_dump($b);

$c = $foo;
$c ??= $bar;
var_dump($c);

$d ??= $foo;
var_dump($c);

echo "\nArrays:\n";
$ary = [];
$ary["foo"] ??= 123;
$ary[$foo] ??= $bar;
$ary[$bar] ??= $foo;
var_dump($ary);

$ary = [];
$ary[id($foo)] ??= 123;
$ary[id($foo)] ??= $bar;
$ary[id($bar)] ??= $foo;
var_dump($ary);

echo "\nObjects:\n";
$obj = new stdClass;
$obj->foo ??= 123;
$obj->$foo ??= $bar;
$obj->$bar ??= $foo;
var_dump($obj);

$obj = new stdClass;
$obj->{id($foo)} ??= 123;
$obj->{id($foo)} ??= $bar;
$obj->{id($bar)} ??= $foo;
var_dump($obj);

class Test {
public static $foo;
public static $bar;
}

echo "\nStatic props:\n";
Test::$foo ??= 123;
Test::$$foo ??= $bar;
Test::$$bar ??= $foo;
var_dump(Test::$foo, Test::$bar);

Test::$foo = null;
Test::$bar = null;
Test::${id($foo)} ??= 123;
Test::${id($foo)} ??= $bar;
Test::${id($bar)} ??= $foo;
var_dump(Test::$foo, Test::$bar);

?>
--EXPECT--
Simple variables:
int(123)
string(3) "foo"
string(3) "foo"
string(3) "foo"

Arrays:
array(2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}
id(foo)
id(foo)
id(bar)
array(2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}

Objects:
object(stdClass)#1 (2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}
id(foo)
id(foo)
id(bar)
object(stdClass)#2 (2) {
["foo"]=>
int(123)
["bar"]=>
string(3) "foo"
}

Static props:
int(123)
string(3) "foo"
id(foo)
id(foo)
id(bar)
int(123)
string(3) "foo"
84 changes: 84 additions & 0 deletions Zend/tests/assign_coalesce_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--TEST--
Coalesce assign (??=): Exception handling
--FILE--
<?php

$foo = "fo";
$foo .= "o";
$bar = "ba";
$bar .= "r";

function id($arg) {
echo "id($arg)\n";
return $arg;
}

function do_throw($msg) {
throw new Exception($msg);
}

$ary = [];
try {
$ary[id($foo)] ??= do_throw("ex1");
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($ary);

class AA implements ArrayAccess {
public function offsetExists($k) {
return true;
}
public function &offsetGet($k) {
$var = ["foo" => "bar"];
return $var;
}
public function offsetSet($k,$v) {}
public function offsetUnset($k) {}
}

class Dtor {
public function __destruct() {
throw new Exception("dtor");
}
}

$ary = new AA;
try {
$ary[new Dtor][id($foo)] ??= $bar;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($foo);

class AA2 implements ArrayAccess {
public function offsetExists($k) {
return false;
}
public function offsetGet($k) {
return null;
}
public function offsetSet($k,$v) {}
public function offsetUnset($k) {}
}

$ary = ["foo" => new AA2];
try {
$ary[id($foo)][new Dtor] ??= $bar;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
var_dump($foo);

?>
--EXPECT--
id(foo)
ex1
array(0) {
}
id(foo)
dtor
string(3) "foo"
id(foo)
dtor
string(3) "foo"
70 changes: 70 additions & 0 deletions Zend/tests/assign_coalesce_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--TEST--
Coalesce assign (??=): ArrayAccess handling
--FILE--
<?php

function id($arg) {
echo "id($arg)\n";
return $arg;
}

class AA implements ArrayAccess {
public $data;
public function __construct($data = []) {
$this->data = $data;
}
public function &offsetGet($k) {
echo "offsetGet($k)\n";
return $this->data[$k];
}
public function offsetExists($k) {
echo "offsetExists($k)\n";
return array_key_exists($k, $this->data);
}
public function offsetSet($k,$v) {
echo "offsetSet($k,$v)\n";
$this->data[$k] = $v;
}
public function offsetUnset($k) { }
}

$ary = new AA(["foo" => new AA, "null" => null]);

echo "[foo]\n";
$ary["foo"] ??= "bar";

echo "[bar]\n";
$ary["bar"] ??= "foo";

echo "[null]\n";
$ary["null"] ??= "baz";

echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "abc";

echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "def";

?>
--EXPECT--
[foo]
offsetExists(foo)
offsetGet(foo)
[bar]
offsetExists(bar)
offsetSet(bar,foo)
[null]
offsetExists(null)
offsetGet(null)
offsetSet(null,baz)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(foo)
offsetSet(bar,abc)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(bar)
11 changes: 11 additions & 0 deletions Zend/tests/assign_coalesce_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Coalesce assign (??=): Non-writable variable
--FILE--
<?php

function foo() { return 123; }
foo() ??= 456;

?>
--EXPECTF--
Fatal error: Can't use function return value in write context in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/assign_coalesce_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Coalesce assign (??=): Cannot reassign $this
--FILE--
<?php

class Test {
public function foobar() {
$this ??= 123;
}
}

?>
--EXPECTF--
Fatal error: Cannot re-assign $this in %s on line %d
1 change: 1 addition & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
EMPTY_SWITCH_DEFAULT_CASE();
}
break;
case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90);
case ZEND_AST_BINARY_OP:
switch (ast->attr) {
case ZEND_ADD: BINARY_OP(" + ", 200, 200, 201);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum _zend_ast_kind {
ZEND_AST_INSTANCEOF,
ZEND_AST_YIELD,
ZEND_AST_COALESCE,
ZEND_AST_ASSIGN_COALESCE,

ZEND_AST_STATIC,
ZEND_AST_WHILE,
Expand Down
Loading