Since a cast does not yield an lvalue, we shouldn't accept invalid
code as in the attached testcase.

The problem was that we weren't setting NON_LVALUE_EXPR properly;
for comparing expressions we shouldn't use ==, because then a cast
in an expression might confuse us.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-06-03  Marek Polacek  <pola...@redhat.com>

        PR c/66341
        * c-typeck.c (build_c_cast): Use c_tree_equal rather than a plain
        comparison.

        * gcc.dg/lvalue-8.c: New test.

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index f55d4c6..f13251b 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -5195,7 +5195,7 @@ build_c_cast (location_t loc, tree type, tree expr)
     }
 
   /* Don't let a cast be an lvalue.  */
-  if (value == expr)
+  if (c_tree_equal (value, expr))
     value = non_lvalue_loc (loc, value);
 
   /* Don't allow the results of casting to floating-point or complex
diff --git gcc/testsuite/gcc.dg/lvalue-8.c gcc/testsuite/gcc.dg/lvalue-8.c
index e69de29..085ce95 100644
--- gcc/testsuite/gcc.dg/lvalue-8.c
+++ gcc/testsuite/gcc.dg/lvalue-8.c
@@ -0,0 +1,19 @@
+/* PR c/66341 */
+/* { dg-do compile } */
+
+void
+foo (int *p)
+{
+  p = 0;
+  /* A cast does not yield an lvalue.  */
+  (int *) p = 0; /* { dg-error "lvalue required as left operand of assignment" 
} */
+  /* A cast to a qualified type has the same effect as a cast
+     to the unqualified version of the type.  */
+  (const int *) p = 0; /* { dg-error "lvalue required as left operand of 
assignment" } */
+  (int *) (char *) p = 0; /* { dg-error "lvalue required as left operand of 
assignment" } */
+  (int *) (char *) (int *) p = 0; /* { dg-error "lvalue required as left 
operand of assignment" } */
+  (int *) (char *) (int *) (char *) p = 0; /* { dg-error "lvalue required as 
left operand of assignment" } */
+  (int *) (double *) p = 0; /* { dg-error "lvalue required as left operand of 
assignment" } */
+  (int *) (int *) p = 0; /* { dg-error "lvalue required as left operand of 
assignment" } */
+  (int *) (const int *) p = 0; /* { dg-error "lvalue required as left operand 
of assignment" } */
+}

        Marek

Reply via email to