Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< --
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 | 3 +-- gcc/testsuite/g++.dg/cpp2a/constinit21.C | 28 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit21.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 3a87e73a4d0..b9fdc94b961 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 00000000000..18bca901202 --- /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" } base-commit: 7037c72a8bb41ed07da64b23e14857a066a91baa -- 2.49.0