https://gcc.gnu.org/g:49f421a31f63405d3ca466e144d010c550206e72

commit r16-1061-g49f421a31f63405d3ca466e144d010c550206e72
Author: Jason Merrill <ja...@redhat.com>
Date:   Mon Jun 2 10:59:02 2025 -0400

    c++: constinit diagnostic regression [PR120506]
    
    In r16-57 I thought it was unnecessary to mention incomplete initialization
    after another diagnostic, but actually it's useful elaboration.
    
            PR c++/120506
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_outermost_constant_expr): Always check
            CONSTRUCTOR_NO_CLEARING.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/constinit21.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                      |  3 +--
 gcc/testsuite/g++.dg/cpp2a/constinit21.C | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 3a87e73a4d0f..b9fdc94b961a 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -9278,8 +9278,7 @@ cxx_eval_outermost_constant_expr (tree t, bool 
allow_non_constant,
 
   /* After verify_constant because reduced_constant_expression_p can unset
      CONSTRUCTOR_NO_CLEARING.  */
-  if (!non_constant_p
-      && TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
+  if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
     {
       if (!allow_non_constant)
        error ("%qE is not a constant expression because it refers to "
diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit21.C 
b/gcc/testsuite/g++.dg/cpp2a/constinit21.C
new file mode 100644
index 000000000000..18bca9012024
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constinit21.C
@@ -0,0 +1,28 @@
+// PR c++/120506
+// { dg-do compile { target c++20 } }
+// Test that we give more information about why the init is non-constant
+
+struct A
+{
+  constexpr A(int c) : counter(c) { }
+
+  int counter;
+};
+
+
+struct B : A
+{
+  constexpr B(int c) : A(c) { }
+
+  int i; // OOPS, not initialized
+};
+
+struct C
+{
+  B sem;
+
+  constexpr C(int c) : sem(c) { }
+};
+
+constinit C s(0);              // { dg-error "incompletely initialized" }
+// { dg-prune-output "constant" }

Reply via email to