]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Add self reference to define_aggregate created aggregates [PR123984]
authorJakub Jelinek <jakub@redhat.com>
Mon, 9 Feb 2026 16:54:10 +0000 (17:54 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 9 Feb 2026 16:54:10 +0000 (17:54 +0100)
We weren't adding the DECL_SELF_REFERENCE_P TYPE_DECL to TYPE_FIELDS of
eval_define_aggregate created aggregates, which resulted in ICE in
accessible_base_p which relies on DECL_SELF_REFERENCE_P TYPE_DECL to be
present in base classes of other classes.

2026-02-09  Jakub Jelinek  <jakub@redhat.com>

PR c++/123984
* reflect.cc (eval_define_aggregate): Set TYPE_BEING_DEFINED on type
after pushclass, call build_self_reference, remove assertion that
TYPE_FIELDS (type) is NULL and instead set fields to
TYPE_FIELDS (type).

* g++.dg/reflect/define_aggregate6.C: New test.

gcc/cp/reflect.cc
gcc/testsuite/g++.dg/reflect/define_aggregate6.C [new file with mode: 0644]

index b6b7556e4247c3a57688bdd411e5b6af266a094f..d04930d732cb2a593bbc00c429e8ce2638d092be 100644 (file)
@@ -6080,8 +6080,9 @@ eval_define_aggregate (location_t loc, const constexpr_ctx *ctx,
   if (!TYPE_BINFO (type))
     xref_basetypes (type, NULL_TREE);
   pushclass (type);
-  gcc_assert (!TYPE_FIELDS (type));
-  tree fields = NULL_TREE;
+  TYPE_BEING_DEFINED (type) = 1;
+  build_self_reference ();
+  tree fields = TYPE_FIELDS (type);
   for (int i = 0; i < TREE_VEC_LENGTH (rvec); ++i)
     {
       tree ra = TREE_VEC_ELT (rvec, i);
diff --git a/gcc/testsuite/g++.dg/reflect/define_aggregate6.C b/gcc/testsuite/g++.dg/reflect/define_aggregate6.C
new file mode 100644 (file)
index 0000000..96c37a3
--- /dev/null
@@ -0,0 +1,22 @@
+// PR c++/123984
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+
+struct A;
+consteval
+{
+  define_aggregate (^^A, { data_member_spec (^^int, {.name{"_"}}) });
+}
+
+struct B : A {};
+
+constexpr int
+get (B &&d)
+{
+  constexpr auto ctx = std::meta::access_context::unchecked ();
+  return static_cast <A*> (&d)->[: nonstatic_data_members_of (^^A, ctx)[0] :];
+}
+
+static_assert (get (B { 123 }) == 123);