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]

Re: reject vars whose types have no linkage


On Feb 20, 2005, Jason Merrill <jason@redhat.com> wrote:

> On 17 Feb 2005 22:49:32 -0200, Alexandre Oliva <aoliva@redhat.com> wrote:
>> On Feb 17, 2005, Alexandre Oliva <aoliva@redhat.com> wrote:
>> 
>>> The relevant DRs seem to all point towards rejecting such code.  Since
>>> -fpermissive is always an option to let pedwarns through, I thought
>>> we'd be better off rejecting both, as per the current DR resolutions.
>> 
>> FWIW, this patch regressed g++.dg/lookup/anon3.C, which introduces the
>> conflicting requirement with anon2.C.  I suppose we should use warning
>> instead of pedwarn, giving users early warning that the code is
>> nonsensical and almost certainly will become ill-formed, although
>> still acceptable by the current standard.  Then we'll be
>> forward-compatible with the revised standard, since we'll issue a
>> diagnostic, and also compatible with the current standard, since the
>> code is accepted.  Comments?

> Yes, I think that's the right approach.

Ok to install?

Index: gcc/cp/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* decl.c (grokvardecl): Don't exempt anonymous types from having
	linkage for variables that have linkage other than "C".

Index: gcc/cp/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1366
diff -u -p -r1.1366 decl.c
--- gcc/cp/decl.c 18 Feb 2005 23:20:00 -0000 1.1366
+++ gcc/cp/decl.c 21 Feb 2005 21:19:56 -0000
@@ -5938,8 +5938,7 @@ grokvardecl (tree type,
 	 declare an entity with linkage.
 
 	 Only check this for public decls for now.  */
-      tree t1 = TREE_TYPE (decl);
-      tree t = no_linkage_check (t1, /*relaxed_p=*/false);
+      tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
       if (t)
 	{
 	  if (TYPE_ANONYMOUS_P (t))
@@ -5947,29 +5946,22 @@ grokvardecl (tree type,
 	      if (DECL_EXTERN_C_P (decl))
 		/* Allow this; it's pretty common in C.  */
 		  ;
-	      else if (same_type_ignoring_top_level_qualifiers_p(t1, t))
-	        /* This is something like "enum { a = 3 } x;", which is
-		   well formed.  The enum doesn't have "a name with no
-		   linkage", because it has no name.  See closed CWG issue
-		   132.
-
-		   Note that while this construct is well formed in C++03
-		   it is likely to become ill formed in C++0x.  See open
-		   CWG issue 389 and related issues.  */
-		;
 	      else
 		{
-		  /* It's a typedef referring to an anonymous type.  */
-		  pedwarn ("non-local variable %q#D uses anonymous type",
+		  /* DRs 132, 319 and 389 seem to indicate types with
+		     no linkage can only be used to declare extern "C"
+		     entities.  Since it's not always an error in the
+		     ISO C++ 90 Standard, we only issue a warning.  */
+		  warning ("non-local variable %q#D uses anonymous type",
 			   decl);
 		  if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
-		    cp_pedwarn_at ("%q#D does not refer to the unqualified "
-                                   "type, so it is not used for linkage",
+		    cp_warning_at ("%q#D does not refer to the unqualified "
+				   "type, so it is not used for linkage",
 				   TYPE_NAME (t));
 		}
 	    }
 	  else
-	    pedwarn ("non-local variable %q#D uses local type %qT", decl, t);
+	    warning ("non-local variable %q#D uses local type %qT", decl, t);
 	}
     }
 
Index: gcc/testsuite/ChangeLog
from  Alexandre Oliva  <aoliva@redhat.com>

	* g++.dg/lookup/anon2.C: Don't let access checks make it look like
	the test passes.
	* g++.dg/other/anon3.C: Accept a warning.

Index: gcc/testsuite/g++.dg/lookup/anon2.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/lookup/anon2.C,v
retrieving revision 1.2
diff -u -p -r1.2 anon2.C
--- gcc/testsuite/g++.dg/lookup/anon2.C 4 Mar 2004 22:42:59 -0000 1.2
+++ gcc/testsuite/g++.dg/lookup/anon2.C 21 Feb 2005 21:20:10 -0000
@@ -1,6 +1,9 @@
 // { dg-do compile }
 // { dg-options "" }
 
-class { int i; } a; // { dg-error "private|anonymous type" }
-void foo() { a.i; } // { dg-error "context" }
+// Make sure we issue a diagnostic if a type with no linkage is used
+// to declare a a variable that has linkage.
 
+struct { int i; } a; // { dg-warning "anonymous type" }
+
+void foo() { a.i; }
Index: gcc/testsuite/g++.dg/other/anon3.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/other/anon3.C,v
retrieving revision 1.1
diff -u -p -r1.1 anon3.C
--- gcc/testsuite/g++.dg/other/anon3.C 21 Sep 2004 17:24:44 -0000 1.1
+++ gcc/testsuite/g++.dg/other/anon3.C 21 Feb 2005 21:20:10 -0000
@@ -4,4 +4,4 @@
 
 // { dg-do compile }
 
-enum { a = 3 } x;
+enum { a = 3 } x; // { dg-warning "anonymous type" }
-- 
Alexandre Oliva             http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer   aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist  oliva@{lsd.ic.unicamp.br, gnu.org}

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