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 for c++/11987 (defining a member of a dependent typedef)


Dodji's work to represent typedefs in templates made this pretty straightforward. Normally we look through TYPENAME_TYPEs in a declarator, but if the typename comes from a typedef, it's a dependent scope that we can't look into.

Tested x86_64-pc-linux-gnu, applied to trunk.
commit 303042db4d8a8efe97ce4e13fc89e8260f823ba3
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Nov 13 00:19:44 2009 -0500

    	PR c++/11987
    	* parser.c (cp_parser_direct_declarator): Give helpful error about
    	trying to define member of a dependent typedef.
    	* pt.c (resolve_typename_type): Don't resolve a typedef typename.
    	* tree.c (typedef_variant_p): New.
    	* cp-tree.h: Declare it.

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index f66a009..a71dc73 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5098,6 +5098,7 @@ extern bool type_has_nontrivial_copy_init	(const_tree);
 extern bool class_tmpl_impl_spec_p		(const_tree);
 extern int zero_init_p				(const_tree);
 extern tree strip_typedefs			(tree);
+extern bool typedef_variant_p			(tree);
 extern tree copy_binfo				(tree, tree, tree,
 						 tree *, int);
 extern int member_p				(const_tree);
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index f77ec55..05def24 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -14191,10 +14191,17 @@ cp_parser_direct_declarator (cp_parser* parser,
 					      /*only_current_p=*/false);
 		/* If that failed, the declarator is invalid.  */
 		if (TREE_CODE (type) == TYPENAME_TYPE)
-		  error_at (declarator_id_start_token->location,
-			    "%<%T::%E%> is not a type",
-			    TYPE_CONTEXT (qualifying_scope),
-			    TYPE_IDENTIFIER (qualifying_scope));
+		  {
+		    if (typedef_variant_p (type))
+		      error_at (declarator_id_start_token->location,
+				"cannot define member of dependent typedef "
+				"%qT", type);
+		    else
+		      error_at (declarator_id_start_token->location,
+				"%<%T::%E%> is not a type",
+				TYPE_CONTEXT (qualifying_scope),
+				TYPE_IDENTIFIER (qualifying_scope));
+		  }
 		qualifying_scope = type;
 	      }
 
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 0e688bf..85d9fff 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -17689,6 +17689,9 @@ resolve_typename_type (tree type, bool only_current_p)
      to look inside it.  */
   if (only_current_p && !currently_open_class (scope))
     return type;
+  /* If this is a typedef, we don't want to look inside (c++/11987).  */
+  if (typedef_variant_p (type))
+    return type;
   /* If SCOPE isn't the template itself, it will not have a valid
      TYPE_FIELDS list.  */
   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 9dae184..f9e1cd7 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -1067,6 +1067,14 @@ strip_typedefs (tree t)
   return cp_build_qualified_type (result, cp_type_quals (t));
 }
 
+/* Returns true iff TYPE is a type variant created for a typedef. */
+
+bool
+typedef_variant_p (tree type)
+{
+  return is_typedef_decl (TYPE_NAME (type));
+}
+
 
 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
    graph dominated by T.  If BINFO is NULL, TYPE is a dependent base,
diff --git a/gcc/testsuite/g++.dg/parse/typename11.C b/gcc/testsuite/g++.dg/parse/typename11.C
new file mode 100644
index 0000000..a79e6d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/typename11.C
@@ -0,0 +1,16 @@
+// PR c++/11987
+
+template <int dim> struct X {
+  struct I { I(); };
+};
+
+template <int dim> struct Y : X<dim> {
+  typedef typename X<dim>::I I;
+};
+
+// note: I is nested type in X, not Y!
+template <int dim>
+Y<dim>::I::I () {}		// { dg-error "dependent typedef" }
+// { dg-error "no type|dependent type" "" { target *-*-* } 13 }
+
+template struct Y<1>;
diff --git a/gcc/testsuite/g++.dg/template/crash48.C b/gcc/testsuite/g++.dg/template/crash48.C
index deb9446..6aa3aa3 100644
--- a/gcc/testsuite/g++.dg/template/crash48.C
+++ b/gcc/testsuite/g++.dg/template/crash48.C
@@ -7,4 +7,4 @@ template<typename T> struct A
   typedef typename T::X X;
 };
 
-template<typename T> A<T>::X::X() {} // { dg-error "no type|invalid use|not a type" }
+template<typename T> A<T>::X::X() {} // { dg-error "no type|invalid use|not a type|dependent" }

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