From: Nikita Popov Date: Fri, 24 Jul 2020 08:35:03 +0000 (+0200) Subject: Make nested ternary without parentheses a compile error X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a5b7c81b705869618755ed2d559702be6f9d6e4;p=php Make nested ternary without parentheses a compile error This was deprecated in PHP 7.4. --- diff --git a/UPGRADING b/UPGRADING index 85931d11cf..bf4584bb8e 100644 --- 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 diff --git a/Zend/tests/ternary_associativity.phpt b/Zend/tests/ternary_associativity.phpt index 32326513ca..edc0499143 100644 --- a/Zend/tests/ternary_associativity.phpt +++ b/Zend/tests/ternary_associativity.phpt @@ -1,30 +1,22 @@ --TEST-- -Using ternary associativity is deprecated +Allowed nested ternary cases --FILE-- ---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 index 0000000000..87dc3972d2 --- /dev/null +++ b/Zend/tests/ternary_associativity_1.phpt @@ -0,0 +1,10 @@ +--TEST-- +Forbidden nested ternary, case 1 +--FILE-- + +--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 index 0000000000..c311e7cab4 --- /dev/null +++ b/Zend/tests/ternary_associativity_2.phpt @@ -0,0 +1,10 @@ +--TEST-- +Forbidden nested ternary, case 2 +--FILE-- + +--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 index 0000000000..c519e1fc0f --- /dev/null +++ b/Zend/tests/ternary_associativity_3.phpt @@ -0,0 +1,10 @@ +--TEST-- +Forbidden nested ternary, case 3 +--FILE-- + +--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/zend_compile.c b/Zend/zend_compile.c index c21fb419b9..51849a2f94 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -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