Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

Here we weren't emitting the guard variable for 'a'  when we emitted 'afn'
in the importer, because we only treated inline variables as needing that.
Fixed by generalizing to vague_linkage_p.

But we need to specifically exempt vtables, because the rest of the module
code handles them specially and expects them to be DECL_EXTERNAL.

gcc/cp/ChangeLog:

        * module.cc (trees_out::core_bools): Check vague_linkage_p.
        (has_definition): Likewise.

gcc/testsuite/ChangeLog:

        * g++.dg/modules/static-3_b.C: New test.
        * g++.dg/modules/static-3_a.H: New test.
---
 gcc/cp/module.cc                          | 23 ++++++++++++-----------
 gcc/testsuite/g++.dg/modules/static-3_b.C |  8 ++++++++
 gcc/testsuite/g++.dg/modules/static-3_a.H | 13 +++++++++++++
 3 files changed, 33 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/static-3_b.C
 create mode 100644 gcc/testsuite/g++.dg/modules/static-3_a.H

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 617bf4c68b1..5e68bb414cd 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -5490,16 +5490,13 @@ trees_out::core_bools (tree t, bits_out& bits)
 
            case VAR_DECL:
              if (TREE_PUBLIC (t)
-                 && !(TREE_STATIC (t)
-                      && DECL_FUNCTION_SCOPE_P (t)
-                      && DECL_DECLARED_INLINE_P (DECL_CONTEXT (t)))
-                 && !DECL_VAR_DECLARED_INLINE_P (t))
+                 && DECL_VTABLE_OR_VTT_P (t))
+               /* We handle vtable linkage specially.  */
                is_external = true;
-             break;
-
+             gcc_fallthrough ();
            case FUNCTION_DECL:
              if (TREE_PUBLIC (t)
-                 && !DECL_DECLARED_INLINE_P (t))
+                 && !vague_linkage_p (t))
                is_external = true;
              break;
            }
@@ -11919,11 +11916,15 @@ has_definition (tree decl)
               since there's no TU to emit them in otherwise.  */
            return true;
 
-         if (!decl_maybe_constant_var_p (decl)
-             && !DECL_INLINE_VAR_P (decl))
-           return false;
+         if (decl_maybe_constant_var_p (decl))
+           /* We might need its constant value.  */
+           return true;
 
-         return true;
+         if (vague_linkage_p (decl))
+           /* These are emitted as needed.  */
+           return true;
+
+         return false;
        }
       break;
 
diff --git a/gcc/testsuite/g++.dg/modules/static-3_b.C 
b/gcc/testsuite/g++.dg/modules/static-3_b.C
new file mode 100644
index 00000000000..70180f8eb32
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/static-3_b.C
@@ -0,0 +1,8 @@
+// { dg-additional-options -fmodules }
+
+import "static-3_a.H";
+
+int main()
+{
+  afn();
+}
diff --git a/gcc/testsuite/g++.dg/modules/static-3_a.H 
b/gcc/testsuite/g++.dg/modules/static-3_a.H
new file mode 100644
index 00000000000..e5a014e9373
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/static-3_a.H
@@ -0,0 +1,13 @@
+// { dg-additional-options -fmodules }
+// { dg-module-do link }
+
+inline int i;
+
+struct A {
+  A() { ++i; }
+};
+
+inline A& afn() {
+  static A a;
+  return a;
+}

base-commit: 819f67a2f633d2000f09119f0e19b784ea0a4bd8
prerequisite-patch-id: 44b613ffa7552972edb4078fd124f755bb857985
prerequisite-patch-id: 4f6f2295c571a63cb862f9e8c3455c66d41cc2e3
-- 
2.47.0

Reply via email to