]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: non-empty constexpr constructor bodies in C++11 [PR123845]
authorPatrick Palka <ppalka@redhat.com>
Fri, 30 Jan 2026 20:25:43 +0000 (15:25 -0500)
committerPatrick Palka <ppalka@redhat.com>
Fri, 30 Jan 2026 20:25:43 +0000 (15:25 -0500)
This patch makes us support C++14 non-empty constexpr constructor bodies
in C++11, as an extension.  This will make it trivial to safely fix the
C++11 library regression PR114865 that requires us to do
__builtin_clear_padding after initializing _M_i in std::atomic's
single-parameter constructor, and that's not really possible with the
C++11 constexpr restrictions.

Since we lower member initializers to constructor body statements
internally, and so constructor bodies are already effectively non-empty
internally even in C++11, supporting non-empty bodies in user code is
mostly a matter of relaxing the parse-time error.

But constexpr-ex3.C revealed that by accepting the non-empty body of A's
constructor, build_data_member_initialization goes on to mistake the
'i = _i' assignment as a member initializer, and we incorrectly accept
the constructor in C++11 mode (even though omitting mem-inits is only
valid since C++20).  Turns out this is caused by that function
recognizing MODIFY_EXPR only in C++11 mode, logic that was last changed
by r5-5013 (presumably to limit impact of the patch at the time) but I
reckon could just be removed outright.  This should be safe because the
result of build_data_member_initialization is only used by
cx_check_missing_mem_inits for validation; evaluation is in terms of
the entire lowered constructor body.

PR c++/123845
PR libstdc++/114865

gcc/cp/ChangeLog:

* constexpr.cc (build_data_member_initialization): Remove
C++11-specific recognition of MODIFY_EXPR.
(check_constexpr_ctor_body): Relax error diagnostic to a
pedwarn and don't clear DECL_DECLARED_CONSTEXPR_P upon
error.  Return true if complaining.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-ex3.C: Adjust C++11 non-empty
constexpr constructor dg-error to a dg-warning.  Expect
a follow-up missing member initializer diagnostic in C++11 mode.
* g++.dg/cpp2a/constexpr-try1.C: Expect a follow-up
compound-statement in constexpr function diagnostic in C++11
mode.
* g++.dg/cpp2a/constexpr-try2.C: Likewise.  Adjust C++11
non-empty constexpr constructor dg-error to a dg-warning.
* g++.dg/cpp2a/constexpr-try3.C:  Adjust C++11 non-empty
constexpr constructor dg-error to a dg-warning.
* g++.dg/cpp0x/constexpr-ctor23.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp0x/constexpr-ctor23.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-ex3.C
gcc/testsuite/g++.dg/cpp2a/constexpr-try1.C
gcc/testsuite/g++.dg/cpp2a/constexpr-try2.C
gcc/testsuite/g++.dg/cpp2a/constexpr-try3.C

index 1527e9dcbac83c9e88c8dd79d586a41427a73717..a31fe6b113a9ea23432e837db920e5a934fd9752 100644 (file)
@@ -412,11 +412,7 @@ build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
     }
   if (TREE_CODE (t) == CONVERT_EXPR)
     t = TREE_OPERAND (t, 0);
-  if (TREE_CODE (t) == INIT_EXPR
-      /* vptr initialization shows up as a MODIFY_EXPR.  In C++14 we only
-        use what this function builds for cx_check_missing_mem_inits, and
-        assignment in the ctor body doesn't count.  */
-      || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
+  if (TREE_CODE (t) == INIT_EXPR)
     {
       member = TREE_OPERAND (t, 0);
       init = break_out_target_exprs (TREE_OPERAND (t, 1));
@@ -578,11 +574,11 @@ check_constexpr_ctor_body (tree last, tree list, bool complain)
   else if (list != last
           && !check_constexpr_ctor_body_1 (last, list))
     ok = false;
-  if (!ok)
+  if (!ok && complain)
     {
-      if (complain)
-       error ("%<constexpr%> constructor does not have empty body");
-      DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
+      pedwarn (input_location, OPT_Wc__14_extensions,
+              "%<constexpr%> constructor does not have empty body");
+      ok = true;
     }
   return ok;
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor23.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor23.C
new file mode 100644 (file)
index 0000000..4019804
--- /dev/null
@@ -0,0 +1,26 @@
+// Verify we diagnose and accept, as an extension, a non-empty constexpr
+// constructor body in C++11 mode.
+// PR c++/123845
+// { dg-do compile { target c++11_only } }
+// { dg-options "" }
+
+constexpr int negate(int n) { return -n; }
+
+struct A {
+  int m;
+  constexpr A() : m(42) {
+    ++m;
+    m = negate(m);
+  } // { dg-warning "does not have empty body \[-Wc++14-extensions\]" }
+};
+static_assert(A().m == -43, "");
+
+template<class T>
+struct B {
+  int m;
+  constexpr B() : m(42) {
+    ++m;
+    m = negate(m);
+  } // { dg-warning "does not have empty body \[-Wc++14-extensions\]" }
+};
+static_assert(B<int>().m == -43, "");
index 9d6d5ff587ca9a34745cbc09be8e3d3c893026b1..169976afbaf78917203e63e89587c73f152a6593 100644 (file)
@@ -6,7 +6,8 @@
 struct A
 {
   int i;
-  constexpr A(int _i) { i = _i; } // { dg-error "empty body|A::i" "" { target c++17_down } }
+  constexpr A(int _i) { i = _i; } // { dg-warning "empty body" "" { target c++11_only } }
+                                 // { dg-error "'A::i' must be init" "" { target c++17_down } .-1 }
 };
 
 template <class T>
index 977eb86dd192bcbb69992786e6f69d810c10266e..e5e70a62b50fecf9cfe5e81b7174b6e5156f3839 100644 (file)
@@ -32,6 +32,7 @@ struct S {
     } catch (int) {    // { dg-error "compound-statement in 'constexpr' function" "" { target c++11_only } }
     }                  // { dg-error "compound-statement in 'constexpr' function" "" { target c++11_only } .-2 }
   } catch (...) {      // { dg-error "'constexpr' constructor does not have empty body" "" { target c++11_only } }
+                       // { dg-error "compound-statement in 'constexpr' function" "" { target c++11_only } .-1 }
   }
   int m;
 };
index 7ca7261a9e0029c9a85e9114bdde35ca92a14916..9504fdaa8696369037913e3fed61e3288006de7d 100644 (file)
@@ -32,7 +32,8 @@ struct S {
     try {              // { dg-warning "'try' in 'constexpr' function only available with" "" { target c++17_down } }
     } catch (int) {    // { dg-warning "compound-statement in 'constexpr' function" "" { target c++11_only } }
     }                  // { dg-warning "compound-statement in 'constexpr' function" "" { target c++11_only } .-2 }
-  } catch (...) {      // { dg-error "'constexpr' constructor does not have empty body" "" { target c++11_only } }
+  } catch (...) {      // { dg-warning "'constexpr' constructor does not have empty body" "" { target c++11_only } }
+                       // { dg-warning "compound-statement in 'constexpr' function" "" { target c++11_only } .-1 }
   }
   int m;
 };
index ab7e8f6d4649a67033739e4422b6c60a52c128ee..070040c5deef45d491a2e52113adbacf8b6c51b7 100644 (file)
@@ -31,7 +31,7 @@ struct S {
     try {              // { dg-warning "'try' in 'constexpr' function only available with" "" { target c++17_down } }
     } catch (int) {
     }
-  } catch (...) {      // { dg-error "'constexpr' constructor does not have empty body" "" { target c++11_only } }
+  } catch (...) {      // { dg-warning "'constexpr' constructor does not have empty body" "" { target c++11_only } }
   }
   int m;
 };