Here when lazily loading the binding for f due to its first use from the
template g, processing_template_decl is set which causes the call to
note_vague_linkage_fn from module_state::read_cluster to have no effect,
and thus we never push f onto deferred_fns and end up never emitting its
definition despite needing it.
The behavior of the lazy loading machinery shouldn't be sensitive to
whether we're inside a template, so to that end this patch makes us
clear processing_template_decl in the entrypoints lazy_load_binding and
lazy_load_pendings.
PR c++/99377
gcc/cp/ChangeLog:
* module.cc (lazy_load_binding): Clear processing_template_decl.
(lazy_load_pendings): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/modules/pr99377-2_a.C: New test.
* g++.dg/modules/pr99377-2_b.C: New test.
timevar_start (TV_MODULE_IMPORT);
+ /* Make sure lazy loading from a template context behaves as if
+ from a non-template context. */
+ processing_template_decl_sentinel ptds;
+
/* Stop GC happening, even in outermost loads (because our caller
could well be building up a lookup set). */
function_depth++;
void
lazy_load_pendings (tree decl)
{
+ /* Make sure lazy loading from a template context behaves as if
+ from a non-template context. */
+ processing_template_decl_sentinel ptds;
+
tree key_decl;
pending_key key;
key.ns = find_pending_key (decl, &key_decl);
--- /dev/null
+// PR c++/99377
+// { dg-additional-options -fmodules-ts }
+// { dg-module-cmi pr99377_2 }
+export module pr99377_2;
+
+export inline void f() { }
--- /dev/null
+// PR c++/99377
+// { dg-additional-options -fmodules-ts }
+// { dg-do link }
+import pr99377_2;
+
+template<class> void g() { f(); }
+
+int main() { f(); }