Patchwork C++ PATCH for c++/48993 (ICE with constexpr ctor)

login
register
mail settings
Submitter Jason Merrill
Date Aug. 6, 2011, 4:32 a.m.
Message ID <4E3CC3EC.9080800@redhat.com>
Download mbox | patch
Permalink /patch/108759/
State New
Headers show

Comments

Jason Merrill - Aug. 6, 2011, 4:32 a.m.
This bug is related to 48089: as there, we can't use 'this' in a 
constructor because it doesn't refer to anything yet.  And as with 
48089, this isn't a complete fix; we currently just sorry rather than 
try to separate the well-formed cases from the ill-formed.

Tested x86_64-pc-linux-gnu, applying to trunk and 4.6.

Patch

commit f7dc8acd232804becb8086eb2915939fb9435823
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Aug 5 18:14:06 2011 -0400

    	PR c++/48993
    	* semantics.c (potential_constant_expression_1) [CALL_EXPR]: Sorry
    	on 'this' in a constructor.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 3d836eb..aa62049 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -7733,7 +7733,17 @@  potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
 		  {
 		    tree x = get_nth_callarg (t, 0);
 		    if (is_this_parameter (x))
-		      /* OK.  */;
+		      {
+			if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
+			  {
+			    if (flags & tf_error)
+			      sorry ("calling a member function of the "
+				     "object being constructed in a constant "
+				     "expression");
+			    return false;
+			  }
+			/* Otherwise OK.  */;
+		      }
 		    else if (!potential_constant_expression_1 (x, rval, flags))
 		      return false;
 		    i = 1;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C
index fc69cfe..5124f7c 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C
@@ -22,3 +22,29 @@  struct R {
 };
 
 constexpr R r;			// { dg-bogus "" "" { xfail *-*-* } }
+
+// Ill-formed (no diagnostic required)
+struct T {
+  int i;
+  constexpr int f() { return i; }
+  constexpr T(): i(0) { }
+  constexpr T(const T& t) : i(f()) { } // { dg-message "" }
+};
+
+constexpr T t1;
+// Ill-formed (diagnostic required)
+constexpr T t2(t1);		// { dg-error "" }
+
+// Well-formed
+struct U {
+  int i, j;
+  constexpr int f(int _i) { return _i; }
+  constexpr int g() { return i; }
+  constexpr U(): i(0), j(0) { }
+  constexpr U(const U& t) : i(f(t.i)),j(0) { } // { dg-bogus "" "" { xfail *-*-* } }
+  constexpr U(int _i) : i(_i),j(g()) { } // { dg-bogus "" "" { xfail *-*-* } }
+};
+
+constexpr U u1;
+constexpr U u2(u1);		// { dg-bogus "" "" { xfail *-*-* } }
+constexpr U u3(1);		// { dg-bogus "" "" { xfail *-*-* } }