Bug #70768 [NEW]: Null coalescing operator inconsistencies

From: Date: Thu, 22 Oct 2015 12:07:51 +0000
Subject: Bug #70768 [NEW]: Null coalescing operator inconsistencies
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196742@lists.php.net to get a copy of this message
From: phplists at stanvassilev dot com Operating system: All PHP version: 7.0.0RC5 Package: Scripting Engine problem Bug Type: Bug Bug description:Null coalescing operator inconsistencies Description: ------------ There appear to be some edge cases in how the null coalescing operator is treated with PHP references. See test scripts. Test 2 seems to be demonstrating an actual engine bug. The last two can be treated as related enhancements (?? is technically an operator forming an expression, but it would be nice to treat it as the special construct that it is to allow these use cases). IMPORTANT: Please run every script one by one to avoid one affecting the next. Test script: --------------- // Test 1. Assign by reference for existing var. $foo = 'foo'; $bar = & $foo ?? 'default'; // No error (AS EXPECTED) var_dump($bar); // string "foo" (AS EXPECTED) $bar = 'bar'; var_dump($foo); // string "bar" (AS EXPECTED) // Test 2. Assign by reference for non-existing var. Results are identical if we set $foo to NULL initially. $bar = & $foo ?? 'default'; // No error (AS EXPECTED) var_dump($bar); // NULL (*NOT* EXPECTED, string "default" expected) $bar = 'bar'; var_dump($foo); // string 'bar' (AS EXPECTED) // Test 3. Return by reference with null coalescing operator. function & foo() { $x = 123; return $x ?? 'default'; } foo(); // Notice: Only variable references should be returned by reference (NOT EXPECTED, preferably this should "just work" like assignment does). // Test 4. Pass by reference with null coalescing operator. function foo(& $x) {} $x = 123; foo($x ?? 'default'); // Fatal error: Only variables can be passed by reference (NOT EXPECTED, preferably this should "just work" like assignment does) Expected result: ---------------- See test code comments for expected results. Actual result: -------------- See test code comments for actual results. -- Edit bug report at https://bugs.php.net/bug.php?id=70768&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70768&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70768&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70768&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70768&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70768&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70768&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70768&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70768&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70768&r=support Expected behavior: https://bugs.php.net/fix.php?id=70768&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70768&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70768&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70768&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70768&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70768&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70768&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70768&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70768&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70768&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70768&r=mysqlcfg

« previous php.bugs (#196742) next »