[gcc r17-2831] c++: anonymous namespace in module partition [PR126209]

Jason Merrill via Gcc-cvs <[email protected]> Thu, 30 Jul 2026 16:13:39 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:da9cb50722ba0341bab9d0bf29d1cfa12be18f42

commit r17-2831-gda9cb50722ba0341bab9d0bf29d1cfa12be18f42
Author: Jason Merrill <[email protected]>
Date:   Wed Jul 29 14:04:24 2026 -0400

    c++: anonymous namespace in module partition [PR126209]
    
    Here since r16-4484 we include all namespaces in the current purview in a
    module, even if the namespace comes from another partition.  That breaks for
    an anonymous namespace, which is local to the TU (and added to the
    definition of TU-local entity by P2996 Reflection); here _c ended up
    representing the anonymous namespace from _a separately from the same one
    passed along from _b, leading to an ICE trying to import them into the same
    slot in _d.  I tried just adding namespaces to is_tu_local_entity, but that
    broke other things, so for 16.2 let's handle them here.
    
            PR c++/126209
    
    gcc/cp/ChangeLog:
    
            * module.cc (depset::hash::add_namespace_entities): Don't
            force out anonymous namespaces.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/anon-5_a.C: New test.
            * g++.dg/modules/anon-5_b.C: New test.
            * g++.dg/modules/anon-5_c.C: New test.
            * g++.dg/modules/anon-5_d.C: New test.

Diff:
---
 gcc/cp/module.cc                        | 6 ++++++
 gcc/testsuite/g++.dg/modules/anon-5_a.C | 6 ++++++
 gcc/testsuite/g++.dg/modules/anon-5_b.C | 3 +++
 gcc/testsuite/g++.dg/modules/anon-5_c.C | 4 ++++
 gcc/testsuite/g++.dg/modules/anon-5_d.C | 3 +++
 5 files changed, 22 insertions(+)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index f7569e688a26..49a47494467c 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -15202,6 +15202,12 @@ depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
       {
+	/* Unless it's a (TU-local) anonymous namespace.
+
+	   FIXME instead of checking here, they should be
+	   is_tu_local_entity.  */
+	if (!TREE_PUBLIC (USING_DECL_DECLS (udir)))
+	  continue;
 	make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
 	if (DECL_MODULE_EXPORT_P (udir))
 	  count++;
diff --git a/gcc/testsuite/g++.dg/modules/anon-5_a.C b/gcc/testsuite/g++.dg/modules/anon-5_a.C
new file mode 100644
index 000000000000..6cc266b5c04c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/anon-5_a.C
@@ -0,0 +1,6 @@
+// PR c++/126209
+// { dg-additional-options -fmodules }
+// { dg-module-cmi }
+export module kernel:event;
+namespace { int anchor = 0; }
+inline int i = anchor;
diff --git a/gcc/testsuite/g++.dg/modules/anon-5_b.C b/gcc/testsuite/g++.dg/modules/anon-5_b.C
new file mode 100644
index 000000000000..9f75fd3e900e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/anon-5_b.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -fmodules }
+export module kernel:memories;
+import :event;
diff --git a/gcc/testsuite/g++.dg/modules/anon-5_c.C b/gcc/testsuite/g++.dg/modules/anon-5_c.C
new file mode 100644
index 000000000000..bf66276d7d62
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/anon-5_c.C
@@ -0,0 +1,4 @@
+// { dg-additional-options -fmodules }
+export module kernel;
+export import :event;
+export import :memories;
diff --git a/gcc/testsuite/g++.dg/modules/anon-5_d.C b/gcc/testsuite/g++.dg/modules/anon-5_d.C
new file mode 100644
index 000000000000..002c54da54d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/anon-5_d.C
@@ -0,0 +1,3 @@
+// { dg-additional-options -fmodules }
+import kernel;
+int main() { return 0; }