]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constinit diagnostic regression [PR120506]
authorJason Merrill <jason@redhat.com>
Mon, 2 Jun 2025 14:59:02 +0000 (10:59 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 2 Jun 2025 19:00:16 +0000 (15:00 -0400)
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.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/constinit21.C [new file with mode: 0644]

index 3a87e73a4d0f2628b3762d4f93fff7bad290cef5..b9fdc94b961a209b34baf0d5ade2864f2295e12e 100644 (file)
@@ -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 (file)
index 0000000..18bca90
--- /dev/null
@@ -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" }