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++/70344 (ICE with recursive constexpr)


cp_fold tries to fold calls to constexpr functions. When we are inside a constexpr function at the time and see a call to the same function, this caused trouble because we hadn't adjusted the parameter types yet. Fixed by handling this special case of calling a constexpr function that isn't defined yet.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 4d4c5c65269b6dcde93fa13a90f2af925c8082c7
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Mar 23 13:51:42 2016 -0400

    	PR c++/70344
    	* constexpr.c (cxx_eval_call_expression): Catch invalid recursion.

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 7b13633..d71e488 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -1239,6 +1239,21 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
       return t;
     }
 
+  if (fun == current_function_decl)
+    {
+      /* A call to the current function, i.e.
+	 constexpr int f (int i) {
+	   constexpr int j = f(i-1);
+	   return j;
+	 }
+	 This would be OK without the constexpr on the declaration of j.  */
+      if (!ctx->quiet)
+	error_at (loc, "%qD called in a constant expression before its "
+		  "definition is complete", fun);
+      *non_constant_p = true;
+      return t;
+    }
+
   constexpr_ctx new_ctx = *ctx;
   if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
       && TREE_CODE (t) == AGGR_INIT_EXPR)
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion2.C
new file mode 100644
index 0000000..978b998
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-recursion2.C
@@ -0,0 +1,17 @@
+// PR c++/70344
+// { dg-do compile { target c++11 } }
+
+struct Z
+{
+  Z () = default;
+  Z (Z const &) = default;
+  constexpr Z (Z &&) {}
+};
+
+constexpr int
+fn (Z v)
+{
+  return fn (v);
+}
+
+auto t = fn (Z ());

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