diff mbox series

c++: Don't crash when mangling member with anonymous union type [PR100632]

Message ID 01020191dd1d2e60-8ee35eb1-27fc-4408-82c6-19ab5f5d1f88-000000@eu-west-1.amazonses.com
State New
Headers show
Series c++: Don't crash when mangling member with anonymous union type [PR100632] | expand

Commit Message

Simon Martin Sept. 10, 2024, 6:06 p.m. UTC
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 mbox series

Patch

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}>();