On 11/15/25 9:47 AM, Nathaniel Shead wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk and 15?

-- >8 --

r16-4930-gfd5c057c2d01 ensured that we noted all class-scope variables.
But I also added a clause to 'read_var_def' to skip all class-scope
instantiations, under the mistaken belief that this would be handled in
read_class_def.

But as the testcase shows, read_class_def cannot (and should not)
register instantiations of member variable templates, as when reading
the class it just sees the template declaration.  So this patch removes
the constraint from read_var_def.

This may cause us to note some variables more than once but I think
that's unlikely to be a large problem, and adding more constraints may
hit another case that I'm not thinking of.

OK, but I wonder if checking primary_template_specialization_p would help avoid the extras?
        PR c++/122625

gcc/cp/ChangeLog:

        * module.cc (trees_in::read_var_def): Remove !DECL_CLASS_SCOPE_P
        constraint on note_vague_linkage_variable.

gcc/testsuite/ChangeLog:

        * g++.dg/modules/inst-7_a.C: New test.
        * g++.dg/modules/inst-7_b.C: New test.

Signed-off-by: Nathaniel Shead <[email protected]>
---
  gcc/cp/module.cc                        |  8 +++-----
  gcc/testsuite/g++.dg/modules/inst-7_a.C | 14 ++++++++++++++
  gcc/testsuite/g++.dg/modules/inst-7_b.C |  9 +++++++++
  3 files changed, 26 insertions(+), 5 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/modules/inst-7_a.C
  create mode 100644 gcc/testsuite/g++.dg/modules/inst-7_b.C

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index ccabd640757..34bb5fbb564 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -13062,11 +13062,9 @@ trees_in::read_var_def (tree decl, tree maybe_template)
          if (DECL_EXPLICIT_INSTANTIATION (decl)
              && !DECL_EXTERNAL (decl))
            setup_explicit_instantiation_definition_linkage (decl);
-         /* Class static data members are handled in read_class_def.  */
-         if (!DECL_CLASS_SCOPE_P (decl)
-             && (DECL_IMPLICIT_INSTANTIATION (decl)
-                 || (DECL_EXPLICIT_INSTANTIATION (decl)
-                     && !DECL_EXTERNAL (decl))))
+         if (DECL_IMPLICIT_INSTANTIATION (decl)
+             || (DECL_EXPLICIT_INSTANTIATION (decl)
+                 && !DECL_EXTERNAL (decl)))
            note_vague_linkage_variable (decl);
        }
        if (!dyn_init)
diff --git a/gcc/testsuite/g++.dg/modules/inst-7_a.C 
b/gcc/testsuite/g++.dg/modules/inst-7_a.C
new file mode 100644
index 00000000000..8b151dc51dc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/inst-7_a.C
@@ -0,0 +1,14 @@
+// PR c++/122625
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi M }
+
+export module M;
+struct integral_constant {
+  void f() const {}
+};
+struct span {
+  template <int> static constexpr integral_constant __v{};
+};
+export inline void format() {
+  span::__v<1>.f();
+}
diff --git a/gcc/testsuite/g++.dg/modules/inst-7_b.C 
b/gcc/testsuite/g++.dg/modules/inst-7_b.C
new file mode 100644
index 00000000000..27ff2248c93
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/inst-7_b.C
@@ -0,0 +1,9 @@
+// PR c++/122625
+// { dg-additional-options "-fmodules" }
+
+import M;
+int main() {
+  format();
+}
+
+// { dg-final { scan-assembler {_ZNW1M4span3__vILi1EEE:} } }

Reply via email to