We currently crash upon the following valid code (the case from the PR,
invalid, can be made valid by simply adding a definition for f at line
2)

=== cut here ===
struct B { const int *p; };
template<B> void f() {}
struct Nested { union { int k; }; } nested;
template void f<B{&nested.k}>();
=== cut here ===

The problem is that because of the anonymous union, nested.k is
represented as nested.$(decl_of_anon_union).k, and we run into an assert
in write_member_name just before calling write_unqualified_name, because
DECL_NAME ($decl_of_anon_union) is 0.

This patch fixes this by relaxing the assert to also accept members with
an ANON_AGGR_TYPE_P type, that are handled by write_unqualified_name
just fine.

Successfully tested on x86_64-pc-linux-gnu.

        PR c++/100632

gcc/cp/ChangeLog:

        * mangle.cc (write_member_name): Relax assert to accept anonymous
        unions.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/nontype-class67.C: New test.

---
 gcc/cp/mangle.cc                             | 3 ++-
 gcc/testsuite/g++.dg/cpp2a/nontype-class67.C | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class67.C

diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc
index 46dc6923add..11dc66c8d16 100644
--- a/gcc/cp/mangle.cc
+++ b/gcc/cp/mangle.cc
@@ -3255,7 +3255,8 @@ write_member_name (tree member)
     }
   else if (DECL_P (member))
     {
-      gcc_assert (!DECL_OVERLOADED_OPERATOR_P (member));
+      gcc_assert (ANON_AGGR_TYPE_P (TREE_TYPE (member))
+                 || !DECL_OVERLOADED_OPERATOR_P (member));
       write_unqualified_name (member);
     }
   else if (TREE_CODE (member) == TEMPLATE_ID_EXPR)
diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class67.C 
b/gcc/testsuite/g++.dg/cpp2a/nontype-class67.C
new file mode 100644
index 00000000000..accf4284883
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class67.C
@@ -0,0 +1,9 @@
+// PR c++/100632
+// { dg-do compile { target c++20 } }
+
+struct B { const int* p; };
+template<B> void f() {}
+
+struct Nested { union { int k; }; } nested;
+
+template void f<B{&nested.k}>();
-- 
2.44.0


Reply via email to