]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Fix -Wexpose-global-module-tu-local [PR122636]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Nov 2025 13:06:06 +0000 (00:06 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Nov 2025 23:45:14 +0000 (10:45 +1100)
I had mistakenly been checking the importedness of the originating
module decl, but this is wrong: really we want to check if the specific
decl we're currently instantating came from another module, so just
check DECL_MODULE_IMPORT_P on this directly.

Also updated slightly since there are cases where we do emit TU-local
function or variable templates, albeit unlikely to come up frequently.

PR c++/122636

gcc/cp/ChangeLog:

* module.cc (instantiating_tu_local_entity): Don't check
importingness of originating module decl; also check templates.

gcc/testsuite/ChangeLog:

* g++.dg/modules/internal-19_a.C: New test.
* g++.dg/modules/internal-19_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/internal-19_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/internal-19_b.C [new file with mode: 0644]

index 40f592b7a2ff8066707e622dd48d4387388af06d..a78d452ee8a1d1e93c749ea72085588c7d9c2125 100644 (file)
@@ -14234,9 +14234,10 @@ instantiating_tu_local_entity (tree decl)
       return true;
     }
 
-  /* Currently, only TU-local variables and functions will be emitted
-     from named modules.  */
-  if (!VAR_OR_FUNCTION_DECL_P (decl))
+  /* Currently, only TU-local variables and functions, or possibly
+     templates thereof, will be emitted from named modules.  */
+  tree inner = STRIP_TEMPLATE (decl);
+  if (!VAR_OR_FUNCTION_DECL_P (inner))
     return false;
 
   /* From this point we will only be emitting warnings; if we're not
@@ -14249,16 +14250,15 @@ instantiating_tu_local_entity (tree decl)
   if (!is_tu_local_entity (decl))
     return false;
 
-  tree origin = get_originating_module_decl (decl);
-  if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (origin))
-      || !DECL_MODULE_IMPORT_P (STRIP_TEMPLATE (origin)))
+  if (!DECL_LANG_SPECIFIC (inner)
+      || !DECL_MODULE_IMPORT_P (inner))
     return false;
 
   /* Referencing TU-local entities from a header is generally OK.
      We don't have an easy way to detect if this declaration came
      from a header via a separate named module, but we can just
      ignore that case for warning purposes.  */
-  unsigned index = import_entity_index (origin);
+  unsigned index = import_entity_index (decl);
   module_state *mod = import_entity_module (index);
   if (mod->is_header ())
     return false;
diff --git a/gcc/testsuite/g++.dg/modules/internal-19_a.C b/gcc/testsuite/g++.dg/modules/internal-19_a.C
new file mode 100644 (file)
index 0000000..8b7bb45
--- /dev/null
@@ -0,0 +1,21 @@
+// PR c++/122636
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi M }
+
+export module M;
+export template <typename>
+struct Foo {
+  constexpr static inline auto lambda = []{};
+  template <typename T = decltype(lambda)>
+  static void foo(T = lambda) {}
+};
+
+export template <typename... Types>
+struct Type
+{
+  template <typename T>
+  auto test(T to)
+  {
+    return [to](auto && ...){ return to; }();
+  }
+};
diff --git a/gcc/testsuite/g++.dg/modules/internal-19_b.C b/gcc/testsuite/g++.dg/modules/internal-19_b.C
new file mode 100644 (file)
index 0000000..58d1fd5
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/122636
+// { dg-additional-options "-fmodules -Werror=expose-global-module-tu-local" }
+
+import M;
+namespace {
+  struct Bar {};
+  void bar() { Foo<Bar>::foo(); }
+}
+
+int main() {
+  bar();
+
+  enum class E { };
+  Type<E> t;
+  t.test(0);
+}