]> granicus.if.org Git - php/commitdiff
Make nested ternary without parentheses a compile error
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 24 Jul 2020 08:35:03 +0000 (10:35 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 24 Jul 2020 08:35:42 +0000 (10:35 +0200)
This was deprecated in PHP 7.4.

UPGRADING
Zend/tests/ternary_associativity.phpt
Zend/tests/ternary_associativity_1.phpt [new file with mode: 0644]
Zend/tests/ternary_associativity_2.phpt [new file with mode: 0644]
Zend/tests/ternary_associativity_3.phpt [new file with mode: 0644]
Zend/zend_compile.c

index 85931d11cf1ef27dd54139e116c69cbea0476517..bf4584bb8ece06b8c44fda8a4a8bf11708f48b07 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -208,6 +208,8 @@ PHP 8.0 UPGRADE NOTES
     recognized as a namespaced name, `Foo \ Bar` will not. Conversely, reserved
     keywords are now permitted as namespace segments.
     RFC: https://wiki.php.net/rfc/namespaced_names_as_token
+  . Nested ternaries now require explicit parentheses.
+    RFC: https://wiki.php.net/rfc/ternary_associativity
 
 - COM:
   . Removed the ability to import case-insensitive constants from type
index 32326513ca8a1a6bc2654db7d93ef817cc74f031..edc04991437d668e4645fa1a52d546259e68d9dd 100644 (file)
@@ -1,30 +1,22 @@
 --TEST--
-Using ternary associativity is deprecated
+Allowed nested ternary cases
 --FILE--
 <?php
 
-1 ? 2 : 3 ? 4 : 5;   // deprecated
 (1 ? 2 : 3) ? 4 : 5; // ok
 1 ? 2 : (3 ? 4 : 5); // ok
 
-// While the associativity of ?: is also incorrect, it will not cause a
-// functional difference, only some unnecessary checks.
 1 ?: 2 ?: 3;   // ok
 (1 ?: 2) ?: 3; // ok
 1 ?: (2 ?: 3); // ok
 
-1 ?: 2 ? 3 : 4;   // deprecated
 (1 ?: 2) ? 3 : 4; // ok
 1 ?: (2 ? 3 : 4); // ok
 
-1 ? 2 : 3 ?: 4;   // deprecated
 (1 ? 2 : 3) ?: 4; // ok
 1 ? 2 : (3 ?: 4); // ok
 
 ?>
---EXPECTF--
-Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in %s on line 3
-
-Deprecated: Unparenthesized `a ?: b ? c : d` is deprecated. Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)` in %s on line 13
-
-Deprecated: Unparenthesized `a ? b : c ?: d` is deprecated. Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)` in %s on line 17
+===DONE===
+--EXPECT--
+===DONE===
diff --git a/Zend/tests/ternary_associativity_1.phpt b/Zend/tests/ternary_associativity_1.phpt
new file mode 100644 (file)
index 0000000..87dc397
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Forbidden nested ternary, case 1
+--FILE--
+<?php
+
+1 ? 2 : 3 ? 4 : 5;
+
+?>
+--EXPECTF--
+Fatal error: Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in %s on line %d
diff --git a/Zend/tests/ternary_associativity_2.phpt b/Zend/tests/ternary_associativity_2.phpt
new file mode 100644 (file)
index 0000000..c311e7c
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Forbidden nested ternary, case 2
+--FILE--
+<?php
+
+1 ?: 2 ? 3 : 4;
+
+?>
+--EXPECTF--
+Fatal error: Unparenthesized `a ?: b ? c : d` is not supported. Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)` in %s on line %d
diff --git a/Zend/tests/ternary_associativity_3.phpt b/Zend/tests/ternary_associativity_3.phpt
new file mode 100644 (file)
index 0000000..c519e1f
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Forbidden nested ternary, case 3
+--FILE--
+<?php
+
+1 ? 2 : 3 ?: 4;
+
+?>
+--EXPECTF--
+Fatal error: Unparenthesized `a ? b : c ?: d` is not supported. Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)` in %s on line %d
index c21fb419b9f802fb27918c40c203e62b741aae4f..51849a2f9439d8de61207ab6e9a214c648d0d92d 100644 (file)
@@ -8234,18 +8234,18 @@ void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
                        && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
                if (cond_ast->child[1]) {
                        if (true_ast) {
-                               zend_error(E_DEPRECATED,
-                                       "Unparenthesized `a ? b : c ? d : e` is deprecated. "
+                               zend_error(E_COMPILE_ERROR,
+                                       "Unparenthesized `a ? b : c ? d : e` is not supported. "
                                        "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
                        } else {
-                               zend_error(E_DEPRECATED,
-                                       "Unparenthesized `a ? b : c ?: d` is deprecated. "
+                               zend_error(E_COMPILE_ERROR,
+                                       "Unparenthesized `a ? b : c ?: d` is not supported. "
                                        "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
                        }
                } else {
                        if (true_ast) {
-                               zend_error(E_DEPRECATED,
-                                       "Unparenthesized `a ?: b ? c : d` is deprecated. "
+                               zend_error(E_COMPILE_ERROR,
+                                       "Unparenthesized `a ?: b ? c : d` is not supported. "
                                        "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
                        } else {
                                /* This case is harmless:  (a ?: b) ?: c always produces the same result