From: Marek Polacek Date: Tue, 12 May 2026 21:01:29 +0000 (-0400) Subject: c++/reflection: overzealous complete_type in consteval_only_p [PR125280] X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=de3a3064f65bb52053d771fa44fb982e8ec80727;p=thirdparty%2Fgcc.git c++/reflection: overzealous complete_type in consteval_only_p [PR125280] This bug report shows that we generate a bogus "has incomplete type" error for Element::mData in type12.C with -freflection: consteval_only_p attempts to complete Array when finishing mData, but mData uses Element which is currently being defined, so we can't complete it yet. We could fix this bogus error by checking can_complete_type_without_circularity before calling complete_type in consteval_only_p, but I'm no longer so sure that we must call complete_type at all. This patch removes that call. One consequence of that is that we don't produce the "outside a constant-evaluated context" error for C::mData (only when mData is defined outside the class as with A::mData above). This behavior matches clang++ though so I'm not too worried about it. type13.C shows that even without the complete_type we can still get to consteval_only_p_walker::walk with a member with erroneous type which currently crashes. PR c++/125280 gcc/cp/ChangeLog: * reflect.cc (consteval_only_p): Don't complete_type. (consteval_only_p_walker::walk): Return false for error_mark_node. gcc/testsuite/ChangeLog: * g++.dg/reflect/init19.C: New test. * g++.dg/reflect/type12.C: New test. * g++.dg/reflect/type13.C: New test. Reviewed-by: Patrick Palka --- diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index c30ba1582af..1880a5ac7f5 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -8623,13 +8623,6 @@ consteval_only_p (tree t) if (dependent_type_p (t)) return false; - /* We need the complete type otherwise we'd have no fields for class - templates and thus come up with zilch for things like - template - struct X : T { }; - which could be consteval-only, depending on T. */ - t = complete_type (t); - consteval_only_p_walker walker; return walker.walk (t).is_true (); } @@ -8641,6 +8634,9 @@ consteval_only_p (tree t) tristate consteval_only_p_walker::walk (tree t) { + if (t == error_mark_node) + return false; + t = TYPE_MAIN_VARIANT (t); if (REFLECTION_TYPE_P (t)) diff --git a/gcc/testsuite/g++.dg/reflect/init19.C b/gcc/testsuite/g++.dg/reflect/init19.C new file mode 100644 index 00000000000..bfebe4cf2d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/init19.C @@ -0,0 +1,12 @@ +// PR c++/125280 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +struct C { decltype(^^::) i; }; +struct N { int i; }; + +template +struct X : T { }; + +auto a = X{}; // { dg-error "outside a constant-evaluated context" } +auto b = X{}; diff --git a/gcc/testsuite/g++.dg/reflect/type12.C b/gcc/testsuite/g++.dg/reflect/type12.C new file mode 100644 index 00000000000..c066f27fb69 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/type12.C @@ -0,0 +1,28 @@ +// PR c++/125280 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +template +struct Array { + T mElements[N]; +}; + +struct Element { + static const Array mData; +}; +const Array Element::mData; + +struct CE { decltype(^^::) i; }; + +struct A { + static const Array mData; +}; +const Array A::mData{}; // { dg-error "outside a constant-evaluated context" } + +struct B { + static constexpr Array mData{}; +}; + +struct C { + static const Array mData; +}; diff --git a/gcc/testsuite/g++.dg/reflect/type13.C b/gcc/testsuite/g++.dg/reflect/type13.C new file mode 100644 index 00000000000..22d26852ec9 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/type13.C @@ -0,0 +1,13 @@ +// PR c++/125280 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +template +struct Array { + T mElements[N]; // { dg-error "incomplete type" } +}; + +struct Element { + static const Array mData{}; // { dg-error "initialization of static data member" } +}; +const Array Element::mData;