]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Track all static class variables [PR122421]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sun, 26 Oct 2025 11:27:33 +0000 (22:27 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 2 Nov 2025 13:59:10 +0000 (00:59 +1100)
The linker error in the PR is caused because when a static is defined
out of the class body, it doesn't yet have a definition and so
read_var_def (which would otherwise have noted it) never gets called.

This instead moves the responsibility for noting class-scope variables
to read_class_def.

PR c++/122421

gcc/cp/ChangeLog:

* module.cc (trees_in::read_var_def): Don't handle class-scope
variables anymore.
(trees_in::read_class_def): Handle them here instead.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
(cherry picked from commit fd5c057c2d01346d69119f88ca94debf27842e4e)

gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/inst-6_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/inst-6_b.C [new file with mode: 0644]

index 632dc47552a69c6536bc99aad0bc60417e39be04..eed84706686b1b25a032edf9e6e322646204de3c 100644 (file)
@@ -12848,12 +12848,11 @@ trees_in::read_var_def (tree decl, tree maybe_template)
          if (DECL_EXPLICIT_INSTANTIATION (decl)
              && !DECL_EXTERNAL (decl))
            setup_explicit_instantiation_definition_linkage (decl);
-         if (DECL_IMPLICIT_INSTANTIATION (decl)
-             || (DECL_EXPLICIT_INSTANTIATION (decl)
-                 && !DECL_EXTERNAL (decl))
-             || (DECL_CLASS_SCOPE_P (decl)
-                 && !DECL_VTABLE_OR_VTT_P (decl)
-                 && !DECL_TEMPLATE_INFO (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))))
            note_vague_linkage_variable (decl);
        }
       if (!dyn_init)
@@ -13267,6 +13266,10 @@ trees_in::read_class_def (tree defn, tree maybe_template)
                        DECL_ACCESS (d) = tree_cons (type, access, list);
                    }
                }
+
+             if (TREE_CODE (decl) == VAR_DECL
+                 && TREE_CODE (maybe_template) != TEMPLATE_DECL)
+               note_vague_linkage_variable (decl);
            }
        }
 
diff --git a/gcc/testsuite/g++.dg/modules/inst-6_a.C b/gcc/testsuite/g++.dg/modules/inst-6_a.C
new file mode 100644 (file)
index 0000000..7f35cc1
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/122421
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi M }
+
+export module M;
+
+export template <typename T> struct Type {
+  static const int arr[3];
+};
+
+extern template const int Type<double>::arr[3];
+template <typename T> const int Type<T>::arr[] = { 42, 43, 44 };
+
+export Type<int> ti;
diff --git a/gcc/testsuite/g++.dg/modules/inst-6_b.C b/gcc/testsuite/g++.dg/modules/inst-6_b.C
new file mode 100644 (file)
index 0000000..5a8092c
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/122421
+// { dg-additional-options "-fmodules" }
+
+import M;
+
+int main() {
+  const int& a = Type<int>::arr[0];
+  const int& b = Type<double>::arr[0];
+}
+
+// { dg-final { scan-assembler {_ZNW1M4TypeIiE3arrE:} } }
+// { dg-final { scan-assembler-not {_ZNW1M4TypeIdE3arrE:} } }