]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement C++29 P3424R2 - Deallocation Functions with Throwing Exception Specifi...
authorJakub Jelinek <jakub@redhat.com>
Fri, 17 Jul 2026 07:58:00 +0000 (09:58 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 17 Jul 2026 08:01:19 +0000 (10:01 +0200)
The following patch attempts to implement the C++29 P3424R2
Deallocation Functions with Throwing Exception Specification Are Ill-formed
paper by diagnosing those cases in grokfndecl, maybe_instantiate_noexcept and
cp_parser_class_specifier.

2026-07-17  Jakub Jelinek  <jakub@redhat.com>

PR c++/125833
* cp-tree.h: Implement C++29 P3424R2 - Deallocation Functions with
Throwing Exception Specification Are Ill-formed.
(maybe_diagnose_deallocation_noexcept_false): Declare.
* decl.cc (maybe_diagnose_deallocation_noexcept_false): New function.
(grokfndecl): Call it.
* pt.cc (maybe_instantiate_noexcept): Likewise.
* parser.cc (cp_parser_class_specifier): Likewise.

* g++.dg/cpp0x/dealloc2.C: Expect extra diagnostics in C++29.
* g++.dg/cpp29/dealloc1.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/parser.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp0x/dealloc2.C
gcc/testsuite/g++.dg/cpp29/dealloc1.C [new file with mode: 0644]

index 9271209ba2ebc66ff8590f16dd1ebb5c8fdb7b2b..c73b42ed44ffc9215597a5b0feb170d155ce6c57 100644 (file)
@@ -7651,6 +7651,7 @@ extern tree start_decl                            (const cp_declarator *, cp_decl_specifier_seq *, int,
 extern void start_decl_1                       (tree, bool);
 extern bool check_array_initializer            (tree, tree, tree);
 extern void omp_declare_variant_finalize       (tree, tree);
+extern void maybe_diagnose_deallocation_noexcept_false (tree);
 struct cp_decomp { tree decl; unsigned int count; };
 extern void cp_finish_decl                     (tree, tree, bool, tree, int, cp_decomp * = nullptr);
 extern tree lookup_decomp_type                 (tree);
index d10290752e7bea737c1120bda61a971ca42d5b6e..639eb85c0760d3eef29f70467d5aef8497ed3f2e 100644 (file)
@@ -9430,6 +9430,25 @@ omp_declare_variant_finalize (tree decl, tree attr)
     }
 }
 
+/* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
+   have a potentially throwing exception specification.  */
+
+void
+maybe_diagnose_deallocation_noexcept_false (tree decl)
+{
+  if (cxx_dialect >= cxx29
+      && DECL_NAME (decl)
+      && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
+      && !IDENTIFIER_NEW_OP_P (DECL_NAME (decl)))
+    {
+      tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
+      if (spec && spec == noexcept_false_spec)
+       error_at (DECL_SOURCE_LOCATION (decl),
+                 "deallocation function %qD declared possibly throwing",
+                 decl);
+    }
+}
+
 static void cp_maybe_mangle_decomp (tree, cp_decomp *);
 
 /* Finish processing of a declaration;
@@ -12621,6 +12640,10 @@ grokfndecl (tree ctype,
        }
     }
 
+  /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
+     have a potentially throwing exception specification.  */
+  maybe_diagnose_deallocation_noexcept_false (decl);
+
   /* Caller will do the rest of this.  */
   if (check < 0)
     {
index f455092b989ef5741a7f8fe1402ee8375fcb0ba3..3f042bec1c66cad21b6e794a7455b1919b06200f 100644 (file)
@@ -30135,6 +30135,10 @@ cp_parser_class_specifier (cp_parser* parser)
 
          /* Remove any template parameters from the symbol table.  */
          maybe_end_member_template_processing ();
+
+         /* [basic.stc.dynamic.deallocation]/3 - A deallocation function
+            shall not have a potentially throwing exception specification.  */
+         maybe_diagnose_deallocation_noexcept_false (decl);
        }
       vec_safe_truncate (unparsed_noexcepts, 0);
 
index 1fe94a863102c4672772c2d82f0d87079501522c..1290db45e1825587d4f2ee14c7ea5fe20caa1d0e 100644 (file)
@@ -28802,6 +28802,10 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   if (orig_fn)
     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
 
+  /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
+     have a potentially throwing exception specification.  */
+  maybe_diagnose_deallocation_noexcept_false (fn);
+
   return true;
 }
 
index 5a0a89257b7a3255e99be09f752497152b3de534..75b833382e988aba692cca5784b17746f7b51f94 100644 (file)
@@ -3,6 +3,7 @@
 struct A {};
 void operator delete (void *, A);
 void operator delete (void *, A) noexcept (false);     // { dg-error "declaration of 'void operator delete\\\(void\\\*, A\\\) noexcept \\\(false\\\)' has a different exception specifier" }
+// { dg-error "deallocation function 'void operator delete\\\(void\\\*, A\\\)' declared possibly throwing" "" { target c++29 } .-1 }
 struct B {};
-void operator delete (void *, B) noexcept (false);
+void operator delete (void *, B) noexcept (false);     // { dg-error "deallocation function 'void operator delete\\\(void\\\*, B\\\)' declared possibly throwing" "" { target c++29 } }
 void operator delete (void *, B);                      // { dg-error "declaration of 'void operator delete\\\(void\\\*, B\\\) noexcept' has a different exception specifier" }
diff --git a/gcc/testsuite/g++.dg/cpp29/dealloc1.C b/gcc/testsuite/g++.dg/cpp29/dealloc1.C
new file mode 100644 (file)
index 0000000..cf39a68
--- /dev/null
@@ -0,0 +1,45 @@
+// P3424R2 - Deallocation Functions with Throwing Exception Specification Are Ill-formed
+// { dg-do compile { target c++11 } }
+
+struct A {};
+struct B {};
+void operator delete (void *, A) noexcept (false);     // { dg-error "deallocation function 'void operator delete\\\(void\\\*, A\\\)' declared possibly throwing" "" { target c++29 } }
+void operator delete (void *, B) noexcept (false) {}   // { dg-error "deallocation function 'void operator delete\\\(void\\\*, B\\\)' declared possibly throwing" "" { target c++29 } }
+void operator delete[] (void *, A) noexcept (false);   // { dg-error "deallocation function 'void operator delete \\\[\\\]\\\(void\\\*, A\\\)' declared possibly throwing" "" { target c++29 } }
+void operator delete[] (void *, B) noexcept (false) {} // { dg-error "deallocation function 'void operator delete \\\[\\\]\\\(void\\\*, B\\\)' declared possibly throwing" "" { target c++29 } }
+template <bool T, bool U>
+struct C {
+  static void operator delete (void *) noexcept (T);   // { dg-error "deallocation function 'static void C<T, U>::operator delete\\\(void\\\*\\\) \\\[with bool T = false; bool U = true\\\]' declared possibly throwing" "" { target c++29 } }
+  static void operator delete[] (void *) noexcept (T); // { dg-error "deallocation function 'static void C<T, U>::operator delete \\\[\\\]\\\(void\\\*\\\) \\\[with bool T = false; bool U = true\\\]' declared possibly throwing" "" { target c++29 } }
+};
+template <bool T, bool U>
+struct D {
+  static void operator delete (void *) noexcept (T) {} // { dg-error "deallocation function 'static void D<T, U>::operator delete\\\(void\\\*\\\) \\\[with bool T = false; bool U = true\\\]' declared possibly throwing" "" { target c++29 } }
+  static void operator delete[] (void *) noexcept (T) {}// { dg-error "deallocation function 'static void D<T, U>::operator delete \\\[\\\]\\\(void\\\*\\\) \\\[with bool T = false; bool U = true\\\]' declared possibly throwing" "" { target c++29 } }
+};
+C <false, false> a;
+C <true, false> b;
+D <false, false> c;
+D <true, false> d;
+auto e = &C <false, true>::operator delete;
+auto f = &C <true, true>::operator delete;
+auto g = &D <false, true>::operator delete;
+auto h = &D <true, true>::operator delete;
+auto i = &C <false, true>::operator delete[];
+auto j = &C <true, true>::operator delete[];
+auto k = &D <false, true>::operator delete[];
+auto l = &D <true, true>::operator delete[];
+struct E {
+  static void operator delete (void *) noexcept (C) {} // { dg-error "deallocation function 'static void E::operator delete\\\(void\\\*\\\)' declared possibly throwing" "" { target c++29 } }
+  static constexpr bool C = false;
+};
+template <int N>
+struct F {
+  static void operator delete (void *) noexcept (false);       // { dg-error "deallocation function 'static void F<N>::operator delete\\\(void\\\*\\\)' declared possibly throwing" "" { target c++29 } }
+  static void operator delete[] (void *) noexcept (false);     // { dg-error "deallocation function 'static void F<N>::operator delete \\\[\\\]\\\(void\\\*\\\)' declared possibly throwing" "" { target c++29 } }
+};
+template <int N>
+struct G {
+  static void operator delete (void *) noexcept (false) {}     // { dg-error "deallocation function 'static void G<N>::operator delete\\\(void\\\*\\\)' declared possibly throwing" "" { target c++29 } }
+  static void operator delete[] (void *) noexcept (false) {}   // { dg-error "deallocation function 'static void G<N>::operator delete \\\[\\\]\\\(void\\\*\\\)' declared possibly throwing" "" { target c++29 } }
+};