From: Nathaniel Shead Date: Sat, 22 Nov 2025 13:06:06 +0000 (+1100) Subject: c++/modules: Fix -Wexpose-global-module-tu-local [PR122636] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fac9e25d6d0b72220c12c424363b76e782f34779;p=thirdparty%2Fgcc.git c++/modules: Fix -Wexpose-global-module-tu-local [PR122636] 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 Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 40f592b7a2f..a78d452ee8a 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -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 index 00000000000..8b7bb45856c --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-19_a.C @@ -0,0 +1,21 @@ +// PR c++/122636 +// { dg-additional-options "-fmodules" } +// { dg-module-cmi M } + +export module M; +export template +struct Foo { + constexpr static inline auto lambda = []{}; + template + static void foo(T = lambda) {} +}; + +export template +struct Type +{ + template + 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 index 00000000000..58d1fd521b0 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-19_b.C @@ -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::foo(); } +} + +int main() { + bar(); + + enum class E { }; + Type t; + t.test(0); +}