]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Find non-exported reachable decls when instantiating friend classes...
authorNathaniel Shead <nathanieloshead@gmail.com>
Mon, 21 Apr 2025 09:58:03 +0000 (19:58 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 22 Apr 2025 07:25:46 +0000 (17:25 +1000)
In r15-9029-geb26b667518c95, we started checking for conflicting
declarations with any reachable decl attached to the same originating
module.  This exposed the issue in the PR, where we would always create
a new type even if a matching type existed in the original module.

This patch reworks lookup_imported_hidden_friend to handle this case
better, by first checking for any reachable decl in the attached module
before looking in the mergeable decl slots.

PR c++/119863

gcc/cp/ChangeLog:

* name-lookup.cc (get_mergeable_namespace_binding): Remove
no-longer-used function.
(lookup_imported_hidden_friend): Also look for hidden imported
decls in an attached decl's module.

gcc/testsuite/ChangeLog:

* g++.dg/modules/tpl-friend-18_a.C: New test.
* g++.dg/modules/tpl-friend-18_b.C: New test.
* g++.dg/modules/tpl-friend-18_c.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/name-lookup.cc
gcc/testsuite/g++.dg/modules/tpl-friend-18_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-18_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-18_c.C [new file with mode: 0644]

index 498126a191abcb14398aa40487a93a016aaff32a..165c26bb578e2bc36928b593535ed52e4a559974 100644 (file)
@@ -4178,22 +4178,6 @@ mergeable_namespace_slots (tree ns, tree name, bool is_attached, tree *vec)
   return vslot;
 }
 
-/* Retrieve the bindings for an existing mergeable entity in namespace
-   NS slot NAME.  Returns NULL if no such bindings exists.  */
-
-static tree
-get_mergeable_namespace_binding (tree ns, tree name, bool is_attached)
-{
-  tree *mslot = find_namespace_slot (ns, name, false);
-  if (!mslot || !*mslot || TREE_CODE (*mslot) != BINDING_VECTOR)
-    return NULL_TREE;
-
-  tree *vslot = get_fixed_binding_slot
-    (mslot, name, is_attached ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL,
-     false);
-  return vslot ? *vslot : NULL_TREE;
-}
-
 /* DECL is a new mergeable namespace-scope decl.  Add it to the
    mergeable entities on GSLOT.  */
 
@@ -4574,9 +4558,9 @@ lookup_imported_hidden_friend (tree friend_tmpl)
 
   lazy_load_pendings (friend_tmpl);
 
-  tree bind = get_mergeable_namespace_binding
-    (current_namespace, DECL_NAME (inner), DECL_MODULE_ATTACH_P (inner));
-  if (!bind)
+  tree name = DECL_NAME (inner);
+  tree *slot = find_namespace_slot (current_namespace, name, false);
+  if (!slot || !*slot || TREE_CODE (*slot) != BINDING_VECTOR)
     return NULL_TREE;
 
   /* We're only interested in declarations attached to the same module
@@ -4584,9 +4568,28 @@ lookup_imported_hidden_friend (tree friend_tmpl)
   int m = get_originating_module (friend_tmpl, /*global=-1*/true);
   gcc_assert (m != 0);
 
+  /* First check whether there's a reachable declaration attached to the module
+     we're looking for.  */
+  if (m > 0)
+    if (binding_slot *mslot = search_imported_binding_slot (slot, m))
+      {
+       if (mslot->is_lazy ())
+         lazy_load_binding (m, current_namespace, name, mslot);
+       for (ovl_iterator iter (*mslot); iter; ++iter)
+         if (DECL_CLASS_TEMPLATE_P (*iter))
+           return *iter;
+      }
+
+  /* Otherwise, look in the mergeable slots for this name, in case an importer
+     has already instantiated this declaration.  */
+  tree *vslot = get_fixed_binding_slot
+    (slot, name, m > 0 ? BINDING_SLOT_PARTITION : BINDING_SLOT_GLOBAL, false);
+  if (!vslot || !*vslot)
+    return NULL_TREE;
+
   /* There should be at most one class template from the module we're
      looking for, return it.  */
-  for (ovl_iterator iter (bind); iter; ++iter)
+  for (ovl_iterator iter (*vslot); iter; ++iter)
     if (DECL_CLASS_TEMPLATE_P (*iter)
        && get_originating_module (*iter, true) == m)
       return *iter;
diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-18_a.C b/gcc/testsuite/g++.dg/modules/tpl-friend-18_a.C
new file mode 100644 (file)
index 0000000..333c976
--- /dev/null
@@ -0,0 +1,25 @@
+// PR c++/119863
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi A }
+
+export module A;
+
+template<typename>
+class T;
+
+template<typename>
+class U
+{
+  template<typename>
+  friend class T;
+};
+
+template<typename V>
+class T
+{
+  U<V> x = {};
+};
+
+export
+template<typename V>
+T<V> f(V) { return {}; }
diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-18_b.C b/gcc/testsuite/g++.dg/modules/tpl-friend-18_b.C
new file mode 100644 (file)
index 0000000..2e537ed
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/119863
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi B }
+
+export module B;
+
+// this should not be considered conflicting
+template <typename>
+class T;
diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-18_c.C b/gcc/testsuite/g++.dg/modules/tpl-friend-18_c.C
new file mode 100644 (file)
index 0000000..6c8d85b
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/119863
+// { dg-additional-options "-fmodules" }
+
+import A;
+import B;
+
+int main()
+{
+  auto const x = f(1);
+}