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++/43281 (ICE with invalid c++0x auto)


It is invalid for a C++0x 'auto' variable to appear in its own initializer for obvious reasons, but we should give a helpful diagnostic rather than crash from infinite recursion...

Tested x86_64-pc-linux-gnu, applied to trunk.
commit 675d6aa9dc87524306aacd25bb00611b72de16d6
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Mar 7 23:32:24 2010 -0500

    	PR c++/43281
    	* pt.c (contains_auto_r): New fn.
    	(do_auto_deduction): Use it.
    	(tsubst): Don't look at TREE_TYPE of a TEMPLATE_TYPE_PARM.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 66e7d73..f5d68f8 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -9921,6 +9921,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 
   if (type
       && TREE_CODE (t) != TYPENAME_TYPE
+      && TREE_CODE (t) != TEMPLATE_TYPE_PARM
       && TREE_CODE (t) != IDENTIFIER_NODE
       && TREE_CODE (t) != FUNCTION_TYPE
       && TREE_CODE (t) != METHOD_TYPE)
@@ -18240,6 +18241,20 @@ listify_autos (tree type, tree auto_node)
   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
 }
 
+/* walk_tree helper for do_auto_deduction.  */
+
+static tree
+contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
+		 void *type)
+{
+  /* Is this a variable with the type we're looking for?  */
+  if (DECL_P (*tp)
+      && TREE_TYPE (*tp) == type)
+    return *tp;
+  else
+    return NULL_TREE;
+}
+
 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.  */
 
@@ -18248,8 +18263,19 @@ do_auto_deduction (tree type, tree init, tree auto_node)
 {
   tree parms, tparms, targs;
   tree args[1];
+  tree decl;
   int val;
 
+  /* The name of the object being declared shall not appear in the
+     initializer expression.  */
+  decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
+  if (decl)
+    {
+      error ("variable %q#D with %<auto%> type used in its own "
+	     "initializer", decl);
+      return error_mark_node;
+    }
+
   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
      with either a new invented type template parameter U or, if the
      initializer is a braced-init-list (8.5.4), with
diff --git a/gcc/testsuite/g++.dg/cpp0x/auto18.C b/gcc/testsuite/g++.dg/cpp0x/auto18.C
new file mode 100644
index 0000000..17f7f99
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/auto18.C
@@ -0,0 +1,6 @@
+// { dg-options "-std=c++0x" }
+
+void f()
+{
+  auto val = val;  // { dg-error "auto. type used in its own initializer" }
+}

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