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]

[C++ PATCH] Fix PR18100 (invalid nested class)


Hi

This patch fixes PR18100.  We forget to diagnose invalid
nested class such as those in the following example:

 class C {     // #1
   class C {}; // #2
 };

In the patch, we detected the error during name lookup at the line #2. The name 'C' is lookup'ed there using TS_CURRENT (i.e., look only in current scope with intention to declare a name here). Name lookup find the TYPE_DECL 'C' from line #1 that is injected into class body. We can detect this situation by
checking its DECL_SELF_REFERENCE_P.


Tested on i686-pc-linux-gnu. OK for mainline?

--Kriang


2004-12-05  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	PR c++/18100
	* decl.c (lookup_and_check_tag): Diagnose nested class with 
	the same name as enclosing class.

2004-12-05  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	PR c++/18100
	* g++.dg/lookup/name-clash4.C: New test.


diff -cprN gcc-main-save/gcc/cp/decl.c gcc-main-new/gcc/cp/decl.c
*** gcc-main-save/gcc/cp/decl.c	Sat Dec  4 16:48:59 2004
--- gcc-main-new/gcc/cp/decl.c	Sun Dec  5 00:32:31 2004
*************** lookup_and_check_tag (enum tag_types tag
*** 9194,9199 ****
--- 9194,9211 ----
  
    if (decl && TREE_CODE (decl) == TYPE_DECL)
      {
+       /* Look for invalid nested type:
+ 	    class C {
+ 	      class C {};
+ 	    };  */
+       if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
+ 	{
+ 	  error ("%qD has the same name as the class in which it is "
+ 		 "declared",
+ 		 decl);
+ 	  return error_mark_node;
+ 	}
+ 
        /* Two cases we need to consider when deciding if a class
  	 template is allowed as an elaborated type specifier:
  	 1. It is a self reference to its own class.
diff -cprN gcc-main-save/gcc/testsuite/g++.dg/lookup/name-clash4.C gcc-main-new/gcc/testsuite/g++.dg/lookup/name-clash4.C
*** gcc-main-save/gcc/testsuite/g++.dg/lookup/name-clash4.C	Thu Jan  1 07:00:00 1970
--- gcc-main-new/gcc/testsuite/g++.dg/lookup/name-clash4.C	Sun Dec  5 00:26:22 2004
***************
*** 0 ****
--- 1,12 ----
+ // { dg-do compile }
+ 
+ // Origin: Volker Reichelt <reichelt@gcc.gnu.org>
+ 
+ // PR c++/18100: Invalid nested type.
+ 
+ struct A
+ {
+     template<int> struct A {};	// { dg-error "nested type" }
+ };
+ 
+ A::A<0> a;			// { dg-error "not a template" }

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