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 to convert_default_arg for c++/5247


Another frequently reported bug. In this one the compiler got into an infinite recursion trying to evaluate a recursive default argument.

Tested x86_64-pc-linux-gnu, applied to trunk.

2007-10-27  Jason Merrill  <jason@redhat.com>

	PR c++/5247
	* call.c (convert_default_arg): Detect recursion.

Index: cp/call.c
===================================================================
*** cp/call.c	(revision 129660)
--- cp/call.c	(working copy)
*************** cxx_type_promotes_to (tree type)
*** 4672,4680 ****
--- 4672,4685 ----
     the indicated TYPE, which is a parameter to FN.  Do any required
     conversions.  Return the converted value.  */
  
+ static GTY(()) VEC(tree,gc) *default_arg_context;
+ 
  tree
  convert_default_arg (tree type, tree arg, tree fn, int parmnum)
  {
+   int i;
+   tree t;
+ 
    /* If the ARG is an unparsed default argument expression, the
       conversion cannot be performed.  */
    if (TREE_CODE (arg) == DEFAULT_ARG)
*************** convert_default_arg (tree type, tree arg
*** 4685,4690 ****
--- 4690,4704 ----
        return error_mark_node;
      }
  
+   /* Detect recursion.  */
+   for (i = 0; VEC_iterate (tree, default_arg_context, i, t); ++i)
+     if (t == fn)
+       {
+ 	error ("recursive evaluation of default argument for %q#D", fn);
+ 	return error_mark_node;
+       }
+   VEC_safe_push (tree, gc, default_arg_context, fn);
+ 
    if (fn && DECL_TEMPLATE_INFO (fn))
      arg = tsubst_default_argument (fn, type, arg);
  
*************** convert_default_arg (tree type, tree arg
*** 4711,4716 ****
--- 4725,4732 ----
        arg = convert_for_arg_passing (type, arg);
      }
  
+   VEC_pop (tree, default_arg_context);
+ 
    return arg;
  }
  
Index: testsuite/g++.dg/overload/defarg1.C
===================================================================
*** testsuite/g++.dg/overload/defarg1.C	(revision 0)
--- testsuite/g++.dg/overload/defarg1.C	(revision 0)
***************
*** 0 ****
--- 1,9 ----
+ // PR c++/5247
+ 
+ template<typename T>
+ int foo (T t, int = foo(T()));
+ 
+ int main()
+ {
+   foo(0);			// { dg-error "default argument" }
+ }

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