This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR c/12446


Hi,

This is a (minor) regression from GCC 3.0.4 present on 3.3 branch and 
mainline, introduced by this patch:
   
   http://gcc.gnu.org/ml/gcc-patches/2001-11/msg00319.html

Before the patch, GCC emitted "error: invalid use of non-lvalue array" for

   struct s { char c[1]; };

   extern struct s foo(void);

   void bar(void)
   {
     char *ptr = foo().c;
   }

in C90 mode. After, it ICEs when trying to expand a BLKmode move.


The patch added the comment

/* Before C99, non-lvalue arrays do not decay to pointers.
+  Normally, using such an array would be invalid; but it can
+  be used correctly inside sizeof or as a statement expression.
+  Thus, do not give an error here; an error will result later.  */

The problem is that we don't give any error later for the testcase. Moreover, 
for the slightly tweaked testcase:

   void bar(void)
   {
     char *ptr;

     ptr = foo().c;
   }

GCC emitted the same error message as above before the patch and now gives 
"incompatible types in assignment".


The patch adds explicit checks for both situations and restores the original 
error message. Bootstrapped/regtested on i586-redhat-linux-gnu (3.3 branch).


2003-10-03 Eric Botcazou  <ebotcazou@libertysurf.fr>

        PR c/12446
        * c-typeck.c (convert_for_assignment): Issue an error for
	array to pointer assignment after default conversion.
	(digest_init): Likewise.


2003-10-03  Eric Botcazou  <ebotcazou@libertysurf.fr>

	* gcc.dg/c90-array-lval-1.c: Adjust expected error texts.
	* gcc.dg/c90-array-lval-3.c: Likewise.
        * gcc.dg/c90-array-lval-6.c: New test.

--
Eric Botcazou
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.213.2.8
diff -u -p -r1.213.2.8 c-typeck.c
--- c-typeck.c	19 Aug 2003 01:42:35 -0000	1.213.2.8
+++ c-typeck.c	3 Oct 2003 11:22:28 -0000
@@ -4247,6 +4247,11 @@ convert_for_assignment (type, rhs, errty
 			     errtype, funname, parmnum);
       return convert (type, rhs);
     }
+  else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
+    {
+      error ("invalid use of non-lvalue array");
+      return error_mark_node;
+    }
   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
     {
       /* An explicit constant 0 can convert to a pointer,
@@ -4785,8 +4790,16 @@ digest_init (type, init, require_constan
 			    TREE_TYPE (type)))))
     {
       if (code == POINTER_TYPE)
-	inside_init = default_function_array_conversion (inside_init);
+	{
+	  inside_init = default_function_array_conversion (inside_init);
 
+	  if (TREE_CODE ( TREE_TYPE (inside_init)) == ARRAY_TYPE)
+	    {
+	      error_init ("invalid use of non-lvalue array");
+	      return error_mark_node;
+	    }
+	 }
+    
       if (require_constant && !flag_isoc99
 	  && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
 	{
Index: gcc.dg/c90-array-lval-1.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/c90-array-lval-1.c,v
retrieving revision 1.2
diff -u -r1.2 c90-array-lval-1.c
--- gcc.dg/c90-array-lval-1.c	6 Nov 2001 12:39:35 -0000	1.2
+++ gcc.dg/c90-array-lval-1.c	3 Oct 2003 18:57:55 -0000
@@ -16,6 +16,6 @@
   (foo ()).c + 1; /* { dg-bogus "warning" "warning in place of error" } */
 }
 /* { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 14 }
-   { dg-error "non-lvalue|incompatible" "array not decaying to lvalue" { target *-*-* } 15 }
+   { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 15 }
    { dg-error "non-lvalue|invalid" "array not decaying to lvalue" { target *-*-* } 16 }
 */
Index: gcc.dg/c90-array-lval-3.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/gcc.dg/c90-array-lval-3.c,v
retrieving revision 1.1
diff -u -r1.1 c90-array-lval-3.c
--- gcc.dg/c90-array-lval-3.c	6 Nov 2001 12:39:36 -0000	1.1
+++ gcc.dg/c90-array-lval-3.c	3 Oct 2003 18:57:55 -0000
@@ -25,9 +25,9 @@
 /* { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 15 }
    { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 16 }
    { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 17 }
-   { dg-error "non-lvalue|incompatible" "array not decaying to lvalue" { target *-*-* } 18 }
-   { dg-error "non-lvalue|incompatible" "array not decaying to lvalue" { target *-*-* } 19 }
-   { dg-error "non-lvalue|incompatible" "array not decaying to lvalue" { target *-*-* } 20 }
+   { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 18 }
+   { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 19 }
+   { dg-error "non-lvalue" "array not decaying to lvalue" { target *-*-* } 20 }
    { dg-error "non-lvalue|invalid" "array not decaying to lvalue" { target *-*-* } 21 }
    { dg-error "non-lvalue|invalid" "array not decaying to lvalue" { target *-*-* } 22 }
    { dg-error "non-lvalue|invalid" "array not decaying to lvalue" { target *-*-* } 23 }
/* PR c/12446 */
/* Origin: Keith Thompson <kst@cts.com> */

/* { dg-do compile } */
/* { dg-options "-std=iso9899:1990" } */


struct s { char c[1]; };

extern struct s foo(void);

void bar(void)
{
  char *ptr = foo().c;  /* { dg-error "non-lvalue" } */
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]