C patch to deprecate conditional expressions as lvalues

Joseph S. Myers jsm@polyomino.org.uk
Tue Nov 4 23:06:00 GMT 2003


This patch does the second part of deprecating extended lvalues for C:
deprecating conditional expressions as lvalues.  As with other
extended lvalues, these unnecessarily complicate the C front end.  The
simple cases have trivial substitutions (for example, changing (a ? b
: c) to *(a ? &b : &c) if b and c are of the same type and can have
their addresses taken, or (a ? b : c) = d to (a ? (b = d) : (c = d))
in any case of a simple assignment); the hard cases (e.g., different
types in the output of an asm) have no well-defined semantics and the
notion that they should be supported was expressly rejected in PR
11564.  (C++ only makes the expressions lvalues in restricted cases,
[expr.cond]p4, when the operands have the same or very similar types;
but then the expression gets a different type from that in C if the
integral promotions would apply to that type, so the C++ rules can't
simply be brought over to give well-defined rules for C.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  I'll apply
this to mainline soon in the absence of any clear reasons why we
should continue to support this extension.

2003-11-04  Joseph S. Myers  <jsm@polyomino.org.uk>

	* c-typeck.c (pedantic_lvalue_warning): Deprecate use of
	conditional expressions as lvalues.

testsuite:
2003-11-04  Joseph S. Myers  <jsm@polyomino.org.uk>

	* gcc.dg/cond-lvalue-1.c: New test.

--- GCC/gcc/c-typeck.c.orig	2003-10-22 22:22:52.000000000 +0000
+++ GCC/gcc/c-typeck.c	2003-11-04 16:57:26.000000000 +0000
@@ -2588,8 +2588,7 @@ pedantic_lvalue_warning (enum tree_code 
   switch (code)
     {
     case COND_EXPR:
-      if (pedantic)
-	pedwarn ("ISO C forbids use of conditional expressions as lvalues");
+      pedwarn ("use of conditional expressions as lvalues is deprecated");
       break;
     case COMPOUND_EXPR:
       if (pedantic)
--- GCC/gcc/testsuite/gcc.dg/cond-lvalue-1.c	2002-08-26 16:21:36.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/cond-lvalue-1.c	2003-11-04 16:55:57.000000000 +0000
@@ -0,0 +1,12 @@
+/* Test for deprecation of conditional expressions as lvalues.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int x, y, z;
+
+void
+foo (void)
+{
+  (x ? y : z) = 1; /* { dg-warning "lvalue" "conditional expression as lvalue deprecated" } */
+}

--- changes.html.orig	2003-11-01 10:15:35.000000000 +0000
+++ changes.html	2003-11-04 17:48:34.000000000 +0000
@@ -113,6 +113,15 @@
         </pre>
         <p>is no longer accepted for C++ and will not be accepted for
         C and Objective-C in a future version.</p></li>
+    <li>The conditional-expression-as-lvalue extension has been
+        deprecated for C and Objective-C.  In particular, code like
+        this:
+        <pre>
+        int a, b, c;
+        (a ? b : c) = 2;
+        </pre>
+        <p>will not be accepted for C and Objective-C in a future
+        version.</p></li>
 
   </ul>
 

-- 
Joseph S. Myers
jsm@polyomino.org.uk



More information about the Gcc-patches mailing list