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++/53733 (DR 1402, deleting move ctor)


Apparently we need to implement DR 1402 in 4.7 in order to fix the std::pair ABI breakage properly. So here it is: if overload resolution chooses a non-trivial copy constructor, instead of causing the move constructor to be deleted, we just don't implicitly declare it.

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

While I was looking at this, I noticed that my earlier fix to handling of cleanups vs. ctor exception specifications was incomplete, so I've fixed that too.
commit e962a2624b47d4a46786ba0ba4d499928323f30c
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jul 10 01:59:11 2012 +0200

    	DR 1402
    	PR c++/53733
    	* cp-tree.h (FNDECL_SUPPRESS_IMPLICIT_DECL): New.
    	(struct lang_decl_fn): Add suppress_implicit_decl field.
    	* method.c (implicitly_declare_fn): Check it.
    	(process_subob_fn): Add no_implicit_p parm.
    	(walk_field_subobs, synthesized_method_walk): Likewise.
    	(maybe_explain_implicit_delete): Adjust.
    	(explain_implicit_non_constexpr): Adjust.

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 713001e..10efa2a 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -1940,7 +1940,7 @@ struct GTY(()) lang_decl_fn {
   unsigned thunk_p : 1;
   unsigned this_thunk_p : 1;
   unsigned hidden_friend_p : 1;
-  /* 1 spare bit.  */
+  unsigned suppress_implicit_decl : 1;
 
   /* For a non-thunk function decl, this is a tree list of
      friendly classes. For a thunk function decl, it is the
@@ -3107,6 +3107,12 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter)
 #define DECL_HIDDEN_FRIEND_P(NODE) \
   (LANG_DECL_FN_CHECK (DECL_COMMON_CHECK (NODE))->hidden_friend_p)
 
+/* Nonzero if NODE is a FUNCTION_DECL generated by implicitly_declare_fn
+   that we shouldn't actually declare implicitly; it is only used for
+   comparing to an =default declaration.  */
+#define FNDECL_SUPPRESS_IMPLICIT_DECL(NODE) \
+  (LANG_DECL_FN_CHECK (DECL_COMMON_CHECK (NODE))->suppress_implicit_decl)
+
 /* Nonzero if DECL has been declared threadprivate by
    #pragma omp threadprivate.  */
 #define CP_DECL_THREADPRIVATE_P(DECL) \
diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 7ea2de9..79edf81 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -922,7 +922,7 @@ get_copy_assign (tree type)
 
 static void
 process_subob_fn (tree fn, bool move_p, tree *spec_p, bool *trivial_p,
-		  bool *deleted_p, bool *constexpr_p,
+		  bool *deleted_p, bool *constexpr_p, bool *no_implicit_p,
 		  const char *msg, tree arg)
 {
   if (!fn || fn == error_mark_node)
@@ -948,12 +948,10 @@ process_subob_fn (tree fn, bool move_p, tree *spec_p, bool *trivial_p,
 	}
     }
 
+  /* Core 1402: A non-trivial copy op suppresses the implicit
+     declaration of the move ctor/op=.  */
   if (move_p && !move_fn_p (fn) && !trivial_fn_p (fn))
-    {
-      if (msg)
-	error (msg, arg);
-      goto bad;
-    }
+    *no_implicit_p = true;
 
   if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
     {
@@ -980,8 +978,8 @@ static void
 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
 		   int quals, bool copy_arg_p, bool move_p,
 		   bool assign_p, tree *spec_p, bool *trivial_p,
-		   bool *deleted_p, bool *constexpr_p, const char *msg,
-		   int flags, tsubst_flags_t complain)
+		   bool *deleted_p, bool *constexpr_p, bool *no_implicit_p,
+		   const char *msg, int flags, tsubst_flags_t complain)
 {
   tree field;
   for (field = fields; field; field = DECL_CHAIN (field))
@@ -1079,7 +1077,8 @@ walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
 	{
 	  walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
 			     copy_arg_p, move_p, assign_p, spec_p, trivial_p,
-			     deleted_p, constexpr_p, msg, flags, complain);
+			     deleted_p, constexpr_p, no_implicit_p,
+			     msg, flags, complain);
 	  continue;
 	}
 
@@ -1096,7 +1095,7 @@ walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
       rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
 
       process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
-			constexpr_p, msg, field);
+			constexpr_p, no_implicit_p, msg, field);
     }
 }
 
@@ -1110,7 +1109,7 @@ walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
 static void
 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
 			 tree *spec_p, bool *trivial_p, bool *deleted_p,
-			 bool *constexpr_p, bool diag)
+			 bool *constexpr_p, bool *no_implicit_p, bool diag)
 {
   tree binfo, base_binfo, scope, fnname, rval, argtype;
   bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
@@ -1198,6 +1197,9 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
   if (trivial_p)
     *trivial_p = expected_trivial;
 
+  if (no_implicit_p)
+    *no_implicit_p = false;
+
   /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
      class versions and other properties of the type.  But a subobject
      class can be trivially copyable and yet have overload resolution
@@ -1256,7 +1258,7 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
       rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
 
       process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
-			constexpr_p, msg, basetype);
+			constexpr_p, no_implicit_p, msg, basetype);
       if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
 	{
 	  /* In a constructor we also need to check the subobject
@@ -1269,7 +1271,7 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
 	     throw) or exception-specification (a throw from one of the
 	     dtors would be a double-fault).  */
 	  process_subob_fn (rval, false, NULL, NULL,
-			    deleted_p, NULL, NULL,
+			    deleted_p, NULL, NULL, NULL,
 			    basetype);
 	}
 
@@ -1310,13 +1312,13 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
 	  rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
 
 	  process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
-			    constexpr_p, msg, basetype);
+			    constexpr_p, no_implicit_p, msg, basetype);
 	  if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
 	    {
 	      rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
 				      NULL_TREE, flags, complain);
 	      process_subob_fn (rval, false, NULL, NULL,
-				deleted_p, NULL, NULL,
+				deleted_p, NULL, NULL, NULL,
 				basetype);
 	    }
 	}
@@ -1331,13 +1333,14 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
 	   "constructor or trivial copy constructor");
   walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
 		     copy_arg_p, move_p, assign_p, spec_p, trivial_p,
-		     deleted_p, constexpr_p, msg, flags, complain);
+		     deleted_p, constexpr_p, no_implicit_p,
+		     msg, flags, complain);
   if (ctor_p)
     walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
 		       sfk_destructor, TYPE_UNQUALIFIED, false,
 		       false, false, NULL, NULL,
 		       deleted_p, NULL,
-		       NULL, flags, complain);
+		       NULL, NULL, flags, complain);
 
   pop_scope (scope);
 
@@ -1407,7 +1410,7 @@ maybe_explain_implicit_delete (tree decl)
 		 "definition would be ill-formed:", decl);
 	  pop_scope (scope);
 	  synthesized_method_walk (ctype, sfk, const_p,
-				   NULL, NULL, NULL, NULL, true);
+				   NULL, NULL, NULL, NULL, NULL, true);
 	}
 
       input_location = loc;
@@ -1427,7 +1430,7 @@ explain_implicit_non_constexpr (tree decl)
   bool dummy;
   synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
 			   special_function_p (decl), const_p,
-			   NULL, NULL, NULL, &dummy, true);
+			   NULL, NULL, NULL, &dummy, NULL, true);
 }
 
 /* Implicitly declare the special function indicated by KIND, as a
@@ -1451,6 +1454,7 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
   bool deleted_p;
   bool trivial_p;
   bool constexpr_p;
+  bool no_implicit_p;
 
   /* Because we create declarations for implicitly declared functions
      lazily, we may be creating the declaration for a member of TYPE
@@ -1520,7 +1524,7 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
     }
 
   synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
-			   &deleted_p, &constexpr_p, false);
+			   &deleted_p, &constexpr_p, &no_implicit_p, false);
   /* Don't bother marking a deleted constructor as constexpr.  */
   if (deleted_p)
     constexpr_p = false;
@@ -1585,6 +1589,7 @@ implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
       DECL_DELETED_FN (fn) = deleted_p;
       DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
     }
+  FNDECL_SUPPRESS_IMPLICIT_DECL (fn) = no_implicit_p;
   DECL_EXTERNAL (fn) = true;
   DECL_NOT_REALLY_EXTERN (fn) = 1;
   DECL_DECLARED_INLINE_P (fn) = 1;
@@ -1777,6 +1782,10 @@ lazily_declare_fn (special_function_kind sfk, tree type)
 	  || sfk == sfk_move_assignment))
     return NULL_TREE;
 
+  /* We also suppress implicit move if it would call a non-trivial copy.  */
+  if (FNDECL_SUPPRESS_IMPLICIT_DECL (fn))
+    return NULL_TREE;
+
   /* A destructor may be virtual.  */
   if (sfk == sfk_destructor
       || sfk == sfk_move_assignment
diff --git a/gcc/testsuite/g++.dg/cpp0x/defaulted21.C b/gcc/testsuite/g++.dg/cpp0x/defaulted21.C
index 30a4a7c..51505dc 100644
--- a/gcc/testsuite/g++.dg/cpp0x/defaulted21.C
+++ b/gcc/testsuite/g++.dg/cpp0x/defaulted21.C
@@ -3,7 +3,8 @@
 
 struct U {
   U();
-  U(U const&);
+private:
+  U(U const&);			// { dg-error "private" }
 };
 
 struct X {
@@ -13,7 +14,7 @@ struct X {
 };
 
 X::X(X&&)=default;		// { dg-message "implicitly deleted" }
-// { dg-error "does not have a move constructor" "" { target *-*-* } 15 }
+// { dg-prune-output "within this context" }
 
 X f() {
   return X();
diff --git a/gcc/testsuite/g++.dg/cpp0x/defaulted36.C b/gcc/testsuite/g++.dg/cpp0x/defaulted36.C
new file mode 100644
index 0000000..1360f60
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/defaulted36.C
@@ -0,0 +1,24 @@
+// PR c++/53733
+// { dg-do compile { target c++11 } }
+
+template<typename T>
+struct wrap
+{
+  wrap() = default;
+  wrap(wrap&&) = default; // Line 5
+  wrap(const wrap&) = default;
+
+  T t;
+};
+
+struct S {
+  S() = default;
+  S(const S&){}
+  S(S&&) = default;
+};
+
+typedef wrap<const S> W;
+
+W get() { return W(); } // Line 19
+
+int main() {}
commit d0334cc0204e8bff2aada54b8f9c73fdc3ebcd50
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jul 10 01:42:02 2012 +0200

    	* method.c (synthesized_method_walk): Avoid changing
    	EH spec based on cleanups in other places, too.

diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 0237456..7ea2de9 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -1315,7 +1315,7 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
 	    {
 	      rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
 				      NULL_TREE, flags, complain);
-	      process_subob_fn (rval, false, spec_p, NULL,
+	      process_subob_fn (rval, false, NULL, NULL,
 				deleted_p, NULL, NULL,
 				basetype);
 	    }
@@ -1335,7 +1335,7 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
   if (ctor_p)
     walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
 		       sfk_destructor, TYPE_UNQUALIFIED, false,
-		       false, false, spec_p, NULL,
+		       false, false, NULL, NULL,
 		       deleted_p, NULL,
 		       NULL, flags, complain);
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/implicit13.C b/gcc/testsuite/g++.dg/cpp0x/implicit13.C
index 96bc770..3165863 100644
--- a/gcc/testsuite/g++.dg/cpp0x/implicit13.C
+++ b/gcc/testsuite/g++.dg/cpp0x/implicit13.C
@@ -14,7 +14,7 @@ struct B: A { };
 // { dg-final { scan-assembler-not "_ZN1BC1Ev" } }
 B b;
 
-struct C { C() noexcept; ~C(); };
+struct C { C() noexcept; ~C() noexcept(false); };
 struct D: C { };
 extern D d;
 
@@ -22,3 +22,11 @@ void *operator new(__SIZE_TYPE__, void*) noexcept;
 
 #define SA(X) static_assert((X),#X)
 SA(noexcept(new (&d) D));
+
+struct E: virtual C { };
+extern E e;
+SA(noexcept (new (&e) E));
+
+struct F { C c; };
+extern F f;
+SA(noexcept (new (&f) F));

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