Hi again,

On 03/30/2012 12:26 AM, Paolo Carlini wrote:
On 03/29/2012 09:27 PM, Jason Merrill wrote:
On 03/29/2012 03:06 PM, Paolo Carlini wrote:
The exception specification on old_decl doesn't matter; we can drop
that test.
I seem to remember something going wrong with templates otherwise,
because implicitly_declare_fn has gcc_assert (!dependent_type_p (type));


We shouldn't be doing this for templates anyway, as in general we can't know what the implicitly declared function will look like.
Oh my, as simple as the below appears to work!

I simply added a !processing_template_decl check. Then I removed the deduce_noexcept_on_destructor calls in register_specialization and when I found a proper place in grokfndecl (must be before check_explicit_specialization) I noticed that apparently I can remove the other deduce_noexcept_on_destructor call which I had later on in grokfndecl. Thus the below passes the (updated) testsuite on x86_64-linux.
Sorry for essentially self-replying, but today, while I was traveling, I reviewed in my mind your comments over the last days, and I think I had a buglet in the patch which I sent in the last message: it doesn't make sure, in grokfndecl, to *not* call deduce_noexcept_on_destructor on a destructor of a class still being defined. Thus I'm adding a !TYPE_BEING_DEFINED (DECL_CONTEXT (decl)) check and the complete patch (which I'm attaching below) still passes testing. I also double checked that, for a simple case like:

struct A
{
  ~A();
};

A::~A() { }

we process the declaration from check_bases_and_members and then the definition from grokfndecl.

Thanks,
Paolo.

////////////////////////
Index: testsuite/g++.old-deja/g++.eh/cleanup1.C
===================================================================
--- testsuite/g++.old-deja/g++.eh/cleanup1.C    (revision 185982)
+++ testsuite/g++.old-deja/g++.eh/cleanup1.C    (working copy)
@@ -2,6 +2,12 @@
 // Bug: obj gets destroyed twice because the fixups for the return are
 // inside its cleanup region.
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#define NOEXCEPT_FALSE noexcept (false)
+#else
+#define NOEXCEPT_FALSE
+#endif
+
 extern "C" int printf (const char *, ...);
 
 int d;
@@ -9,7 +15,7 @@ int d;
 struct myExc { };
 
 struct myExcRaiser {
-  ~myExcRaiser() { throw myExc(); }
+  ~myExcRaiser() NOEXCEPT_FALSE { throw myExc(); }
 };
 
 struct stackObj {
Index: testsuite/g++.dg/tree-ssa/ehcleanup-1.C
===================================================================
--- testsuite/g++.dg/tree-ssa/ehcleanup-1.C     (revision 185982)
+++ testsuite/g++.dg/tree-ssa/ehcleanup-1.C     (working copy)
@@ -1,9 +1,16 @@
 // { dg-options "-O2 -fdump-tree-ehcleanup1-details" }
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#define NOEXCEPT_FALSE noexcept (false)
+#else
+#define NOEXCEPT_FALSE
+#endif
+
 extern void can_throw ();
 class a
 {
 public:
-  ~a ()
+  ~a () NOEXCEPT_FALSE
   {
     if (0)
       can_throw ();
Index: testsuite/g++.dg/cpp0x/noexcept17.C
===================================================================
--- testsuite/g++.dg/cpp0x/noexcept17.C (revision 0)
+++ testsuite/g++.dg/cpp0x/noexcept17.C (revision 0)
@@ -0,0 +1,54 @@
+// PR c++/50043
+// { dg-options -std=c++11 }
+
+struct True1 {};
+struct True2 { ~True2(); };
+struct True3 { ~True3(){ throw 0; } };
+struct False { ~False() noexcept(false); };
+
+template <typename Base>
+struct A : Base
+{
+};
+
+template <typename Member>
+struct B
+{
+    Member mem;
+};
+
+template <typename Base, typename Member>
+struct C : Base
+{
+    Member mem;
+};
+
+#define SA(X) static_assert(X, #X)
+
+SA( noexcept(True1()));
+SA( noexcept(True2()));
+SA( noexcept(True3()));
+SA(!noexcept(False()));
+
+SA( noexcept(A<True1>()));
+SA( noexcept(A<True2>()));
+SA( noexcept(A<True3>()));
+SA(!noexcept(A<False>()));
+
+SA( noexcept(B<True1>()));
+SA( noexcept(B<True2>()));
+SA( noexcept(B<True3>()));
+SA(!noexcept(B<False>()));
+
+SA( noexcept(C<True1, True2>()));
+SA( noexcept(C<True1, True3>()));
+SA( noexcept(C<True2, True3>()));
+SA( noexcept(C<True2, True1>()));
+SA( noexcept(C<True3, True1>()));
+SA( noexcept(C<True3, True2>()));
+SA(!noexcept(C<False, True1>()));
+SA(!noexcept(C<False, True2>()));
+SA(!noexcept(C<False, True3>()));
+SA(!noexcept(C<True1, False>()));
+SA(!noexcept(C<True2, False>()));
+SA(!noexcept(C<True3, False>()));
Index: testsuite/g++.dg/cpp0x/noexcept01.C
===================================================================
--- testsuite/g++.dg/cpp0x/noexcept01.C (revision 185982)
+++ testsuite/g++.dg/cpp0x/noexcept01.C (working copy)
@@ -50,7 +50,7 @@ struct E
   ~E();
 };
 
-SA (!noexcept (E()));
+SA (noexcept (E()));
 
 struct F
 {
@@ -74,7 +74,7 @@ void tf()
 }
 
 template void tf<int,true>();
-template void tf<E, false>();
+template void tf<E, true>();
 
 // Make sure that noexcept uses the declared exception-specification, not
 // any knowledge we might have about whether or not the function really
Index: testsuite/g++.dg/eh/init-temp1.C
===================================================================
--- testsuite/g++.dg/eh/init-temp1.C    (revision 185982)
+++ testsuite/g++.dg/eh/init-temp1.C    (working copy)
@@ -1,6 +1,12 @@
 // PR c++/15764
 // { dg-do run }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#define NOEXCEPT_FALSE noexcept (false)
+#else
+#define NOEXCEPT_FALSE
+#endif
+
 extern "C" void abort (); 
  
 int thrown; 
@@ -8,7 +14,7 @@ int thrown;
 int as;
 struct a {
   a () { ++as; }
-  ~a () { --as; if (thrown++ == 0) throw 42; }
+  ~a () NOEXCEPT_FALSE { --as; if (thrown++ == 0) throw 42; }
 }; 
  
 int f (a const&) { return 1; } 
Index: testsuite/g++.dg/eh/ctor1.C
===================================================================
--- testsuite/g++.dg/eh/ctor1.C (revision 185982)
+++ testsuite/g++.dg/eh/ctor1.C (working copy)
@@ -5,6 +5,12 @@
 
 // PR 411
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#define NOEXCEPT_FALSE noexcept (false)
+#else
+#define NOEXCEPT_FALSE
+#endif
+
 bool was_f_in_Bar_destroyed=false;
 
 struct Foo
@@ -17,7 +23,7 @@ struct Foo
 
 struct Bar
 {
-  ~Bar()
+  ~Bar() NOEXCEPT_FALSE
   {
     throw 1;
   }
Index: cp/class.c
===================================================================
--- cp/class.c  (revision 185982)
+++ cp/class.c  (working copy)
@@ -4321,6 +4321,41 @@ clone_constructors_and_destructors (tree t)
     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
 }
 
+/* Deduce noexcept for a destructor DTOR.  */
+
+void
+deduce_noexcept_on_destructor (tree dtor)
+{
+  if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
+    {
+      tree ctx = DECL_CONTEXT (dtor);
+      tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
+                                               /*const_p=*/false);
+      tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
+      TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
+    }
+}
+
+/* For each destructor in T, deduce noexcept:
+
+   12.4/3: A declaration of a destructor that does not have an
+   exception-specification is implicitly considered to have the
+   same exception-specification as an implicit declaration (15.4).  */
+
+static void
+deduce_noexcept_on_destructors (tree t)
+{
+  tree fns;
+
+  /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
+     out now.  */
+  if (!CLASSTYPE_METHOD_VEC (t))
+    return;
+
+  for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    deduce_noexcept_on_destructor (OVL_CURRENT (fns));
+}
+
 /* Subroutine of set_one_vmethod_tm_attributes.  Search base classes
    of TYPE for virtual functions which FNDECL overrides.  Return a
    mask of the tm attributes found therein.  */
@@ -4994,6 +5029,10 @@ check_bases_and_members (tree t)
   cant_have_const_ctor = 0;
   no_const_asn_ref = 0;
 
+  /* Deduce noexcept on destructors.  */
+  if (cxx_dialect >= cxx0x)
+    deduce_noexcept_on_destructors (t);
+
   /* Check all the base-classes.  */
   check_bases (t, &cant_have_const_ctor,
               &no_const_asn_ref);
Index: cp/decl.c
===================================================================
--- cp/decl.c   (revision 185983)
+++ cp/decl.c   (working copy)
@@ -7448,6 +7448,13 @@ grokfndecl (tree ctype,
   if (ctype != NULL_TREE)
     grokclassfn (ctype, decl, flags);
 
+  /* 12.4/3  */
+  if (cxx_dialect >= cxx0x
+      && DECL_DESTRUCTOR_P (decl)
+      && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
+      && !processing_template_decl)
+    deduce_noexcept_on_destructor (decl);
+
   decl = check_explicit_specialization (orig_declarator, decl,
                                        template_count,
                                        2 * funcdef_flag +
Index: cp/method.c
===================================================================
--- cp/method.c (revision 185982)
+++ cp/method.c (working copy)
@@ -1444,7 +1444,7 @@ explain_implicit_non_constexpr (tree decl)
    reference argument or a non-const reference.  Returns the
    FUNCTION_DECL for the implicitly declared function.  */
 
-static tree
+tree
 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
 {
   tree fn;
Index: cp/cp-tree.h
===================================================================
--- cp/cp-tree.h        (revision 185982)
+++ cp/cp-tree.h        (working copy)
@@ -4978,6 +4978,7 @@ extern void fixup_attribute_variants              (tree);
 extern tree* decl_cloned_function_p            (const_tree, bool);
 extern void clone_function_decl                        (tree, int);
 extern void adjust_clone_args                  (tree);
+extern void deduce_noexcept_on_destructor       (tree);
 
 /* in cvt.c */
 extern tree convert_to_reference               (tree, tree, int, int, tree);
@@ -5264,6 +5265,8 @@ extern tree get_copy_assign                       (tree);
 extern tree get_default_ctor                   (tree);
 extern tree get_dtor                           (tree, tsubst_flags_t);
 extern tree locate_ctor                                (tree);
+extern tree implicitly_declare_fn               (special_function_kind, tree,
+                                                bool);
 
 /* In optimize.c */
 extern bool maybe_clone_body                   (tree);

Reply via email to