Signed-off-by: Valentyn Yukhymenko <[email protected]>
---
 gcc/cp/typeck.cc                     | 10 ++++++++
 gcc/testsuite/g++.dg/reflect/anon4.C | 34 ++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/reflect/anon4.C

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 20ef2a4d6df..e54f10f0fb3 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -3616,6 +3616,16 @@ finish_class_member_access_expr (cp_expr object, tree 
name, bool template_p,
            name = OVL_NAME (name);
        }
 
+      if (TREE_CODE (name) == FIELD_DECL && ANON_UNION_TYPE_P (scope))
+       {
+         tree c = CP_TYPE_CONTEXT (scope);
+         while (ANON_UNION_TYPE_P (c))
+           c = CP_TYPE_CONTEXT (c);
+
+         if (TYPE_P (c))
+           scope = c;
+       }
+
       if (scope)
        {
          if (TREE_CODE (scope) == ENUMERAL_TYPE)
diff --git a/gcc/testsuite/g++.dg/reflect/anon4.C 
b/gcc/testsuite/g++.dg/reflect/anon4.C
new file mode 100644
index 00000000000..e40c727c3bb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/anon4.C
@@ -0,0 +1,34 @@
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123642
+
+struct foo
+{
+  int i;
+  union
+  {
+    int a;
+    long b;
+    union
+    {
+      double c;
+    };
+  };
+};
+
+void test ()
+{
+  constexpr foo bar { .i = 11, .a = 1 };
+
+  static_assert (bar.a == 1);
+  static_assert (bar.[: ^^foo::a :] == 1); 
+
+  static_assert (bar.*(&foo::a) == 1);
+  static_assert (bar.*&[: ^^foo::a :] == 1); 
+
+  constexpr foo bar1 { .i = 42, .c = 3.14 };
+
+  static_assert (bar1.c == 3.14);
+  static_assert (bar1.[: ^^foo::c :] == 3.14);
+}
+
-- 

Reply via email to