https://gcc.gnu.org/g:8da00520ecb80b73b427f0821a444e830a70b475

commit r16-6261-g8da00520ecb80b73b427f0821a444e830a70b475
Author: Nathaniel Shead <[email protected]>
Date:   Fri Dec 5 01:14:36 2025 +1100

    c++/modules: Don't build STAT_HACKs for current TU namespace bindings 
[PR122995]
    
    The issue in the PR is that we're checking if the binding entity for the
    current TU matches the namespace we're pushing.  In this case the slot
    however is a STAT_HACK we created during 'maybe_record_mergeable_decl'
    to indicate that the binding entity contains a global module binding.
    
    Adding '|| (STAT_HACK_P ((tree) slot) && STAT_DECL ((tree) slot) == ns)'
    should fix the assertion, but I think we want to just not build the
    STAT_HACK for namespaces, as they'll always be global module regardless,
    and cannot match with any other declaration, so there's no need for the
    special flag.
    
            PR c++/122995
    
    gcc/cp/ChangeLog:
    
            * name-lookup.cc (maybe_record_mergeable_decl): Don't build a
            STAT_HACK for namespaces.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/namespace-17_a.C: New test.
            * g++.dg/modules/namespace-17_b.C: New test.
    
    Signed-off-by: Nathaniel Shead <[email protected]>

Diff:
---
 gcc/cp/name-lookup.cc                         |  4 +++-
 gcc/testsuite/g++.dg/modules/namespace-17_a.C |  8 ++++++++
 gcc/testsuite/g++.dg/modules/namespace-17_b.C | 10 ++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 4c07fd40f644..8fa5a0c246d7 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -3791,7 +3791,9 @@ maybe_record_mergeable_decl (tree *slot, tree name, tree 
decl)
     (slot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
      true);
 
-  if (!is_attached)
+  /* A namespace is always global module so there's no need to mark
+     the current binding slot as such.  */
+  if (!is_attached && TREE_CODE (decl) != NAMESPACE_DECL)
     {
       binding_slot &orig
        = BINDING_VECTOR_CLUSTER (*slot, 0).slots[BINDING_SLOT_CURRENT];
diff --git a/gcc/testsuite/g++.dg/modules/namespace-17_a.C 
b/gcc/testsuite/g++.dg/modules/namespace-17_a.C
new file mode 100644
index 000000000000..e78a8245cadc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/namespace-17_a.C
@@ -0,0 +1,8 @@
+// PR c++/122995
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi bug2 }
+
+export module bug2;
+namespace N {
+  namespace C { struct r3_cpo; } export extern inline C::r3_cpo const r3;
+}
diff --git a/gcc/testsuite/g++.dg/modules/namespace-17_b.C 
b/gcc/testsuite/g++.dg/modules/namespace-17_b.C
new file mode 100644
index 000000000000..33d56db08195
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/namespace-17_b.C
@@ -0,0 +1,10 @@
+// PR c++/122995
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi bug }
+
+export module bug;
+import bug2;
+namespace N {
+  namespace C { struct r1_cpo; } export extern inline C::r1_cpo const r1;
+  namespace C { struct r2_cpo; } export extern inline C::r2_cpo const r2;
+}

Reply via email to