c: [PR84900] cast of compound literal does not cause the code to become a non-lvalue
authorAndrew Pinski <apinski@marvell.com>
Thu, 23 Mar 2023 02:10:11 +0000 (02:10 +0000)
committerAndrew Pinski <apinski@marvell.com>
Thu, 23 Mar 2023 17:24:59 +0000 (17:24 +0000)
The problem here is after r0-92187-g2ec5deb5c3146c, maybe_lvalue_p would
return false for compound literals which causes non_lvalue_loc not
to wrap the expression with a NON_LVALUE_EXPR unlike before when it
return true as it returns true for all language specific tree codes.

This fixes that oversight and fixes the testcase to have the cast as
a non-lvalue.

Committed to the trunk as obvious after a bootstrap/test on x86_64-linux-gnu.

PR c/84900

gcc/ChangeLog:

* fold-const.cc (maybe_lvalue_p): Treat COMPOUND_LITERAL_EXPR
as a lvalue.

gcc/testsuite/ChangeLog:

* gcc.dg/compound-literal-cast-lvalue-1.c: New test.

gcc/fold-const.cc
gcc/testsuite/gcc.dg/compound-literal-cast-lvalue-1.c [new file with mode: 0644]

index 02a24c5..5b9982e 100644 (file)
@@ -2646,6 +2646,7 @@ maybe_lvalue_p (const_tree x)
   case LABEL_DECL:
   case FUNCTION_DECL:
   case SSA_NAME:
+  case COMPOUND_LITERAL_EXPR:
 
   case COMPONENT_REF:
   case MEM_REF:
diff --git a/gcc/testsuite/gcc.dg/compound-literal-cast-lvalue-1.c b/gcc/testsuite/gcc.dg/compound-literal-cast-lvalue-1.c
new file mode 100644 (file)
index 0000000..729bae2
--- /dev/null
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c99" } */
+/* PR c/84900; casts from compound literals
+   were not considered a non-lvalue. */
+
+int main() {
+        int *p = &(int) (int) {0}; /* { dg-error "lvalue" } */
+        return 0;
+}