Bug #70768 [Opn->Nab]: Null coalescing operator inconsistencies

From: Date: Thu, 22 Oct 2015 12:19:06 +0000
Subject: Bug #70768 [Opn->Nab]: Null coalescing operator inconsistencies
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196743@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70768&edit=1 ID: 70768 Updated by: bwoebi@php.net Reported by: phplists at stanvassilev dot com Summary: Null coalescing operator inconsistencies -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: All PHP Version: 7.0.0RC5 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php I think you're misparsing the expression. & $foo ?? 'default' isn't a valid expression by itself, only $bar = & $foo is. Hence the code is parsed the only possible way, namely: ($bar = &$foo) ?? 'default'; which basically just will assign the reference of $foo to $bar. In general also, the null-coalesce returns an expression, which doesn't have the same semantics as a variable. Hence is an invalid return value on by-ref functions nor can it be passed as by-ref arg. Previous Comments: ------------------------------------------------------------------------ [2015-10-22 12:07:49] phplists at stanvassilev dot com 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 this bug report at https://bugs.php.net/bug.php?id=70768&edit=1

« previous php.bugs (#196743) next »