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] Avoid ICE on auto non-static data members (PR c++/51401)


Hi!

By my reading auto is not valid on non-static data members, if they aren't
NSDMI, then we'd error on them (but differently between templates and
non-templates), but with NSDMI in template we just ICE because init is
DEFAULT_ARG.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2011-12-07  Jakub Jelinek  <jakub@redhat.com>

	PR c++/51401
	* decl.c (grokdeclarator): Error for auto on non-static data members.

	* g++.dg/cpp0x/auto7.C: Adjust expected error message.
	* g++.dg/cpp0x/auto29.C: New test.

--- gcc/cp/decl.c.jj	2011-12-07 12:51:17.000000000 +0100
+++ gcc/cp/decl.c	2011-12-07 18:09:09.113310949 +0100
@@ -9971,6 +9971,12 @@ grokdeclarator (const cp_declarator *dec
       }
     else if (decl_context == FIELD)
       {
+	if (!staticp && type_uses_auto (type))
+	  {
+	    error ("non-static data member declared %<auto%>");
+	    type = error_mark_node;
+	  }
+
 	/* The C99 flexible array extension.  */
 	if (!staticp && TREE_CODE (type) == ARRAY_TYPE
 	    && TYPE_DOMAIN (type) == NULL_TREE)
--- gcc/testsuite/g++.dg/cpp0x/auto7.C.jj	2011-05-31 08:02:58.000000000 +0200
+++ gcc/testsuite/g++.dg/cpp0x/auto7.C	2011-12-07 18:14:18.011549947 +0100
@@ -9,5 +9,5 @@ template<int> struct A
 {
   static auto k = 7;	// { dg-error "non-const" }
   static auto l;	// { dg-error "has no initializer" }
-  auto m;		// { dg-error "has no initializer" }
+  auto m;		// { dg-error "non-static data member declared" }
 };
--- gcc/testsuite/g++.dg/cpp0x/auto29.C.jj	2011-12-07 18:12:22.181184910 +0100
+++ gcc/testsuite/g++.dg/cpp0x/auto29.C	2011-12-07 18:11:52.000000000 +0100
@@ -0,0 +1,25 @@
+// PR c++/51401
+// { dg-do compile }
+// { dg-options "-std=c++11" }
+
+template <int>
+struct A
+{
+  auto i;	// { dg-error "non-static data member declared" }
+};
+
+template <int>
+struct B
+{
+  auto i = 0;	// { dg-error "non-static data member declared" }
+};
+
+struct C
+{
+  auto i;	// { dg-error "non-static data member declared" }
+};
+
+struct D
+{
+  auto i = 0;	// { dg-error "non-static data member declared" }
+};

	Jakub


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