]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: inline variables and modules
authorJason Merrill <jason@redhat.com>
Tue, 19 Nov 2024 20:59:40 +0000 (21:59 +0100)
committerJason Merrill <jason@redhat.com>
Thu, 21 Nov 2024 18:37:23 +0000 (19:37 +0100)
We weren't writing out the definition of an inline variable, so the importer
either got an undefined symbol or 0.

gcc/cp/ChangeLog:

* module.cc (has_definition): Also true for inline vars.

gcc/testsuite/ChangeLog:

* g++.dg/modules/inline-1_a.C: New test.
* g++.dg/modules/inline-1_b.C: New test.

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

index 3b25f9569280f784360f3c97fea1d8e7f467af0b..617bf4c68b1876ef93e0cab2765e0a7922a4d90b 100644 (file)
@@ -11919,7 +11919,8 @@ has_definition (tree decl)
               since there's no TU to emit them in otherwise.  */
            return true;
 
-         if (!decl_maybe_constant_var_p (decl))
+         if (!decl_maybe_constant_var_p (decl)
+             && !DECL_INLINE_VAR_P (decl))
            return false;
 
          return true;
diff --git a/gcc/testsuite/g++.dg/modules/inline-1_a.C b/gcc/testsuite/g++.dg/modules/inline-1_a.C
new file mode 100644 (file)
index 0000000..eafd450
--- /dev/null
@@ -0,0 +1,11 @@
+// { dg-additional-options -fmodules }
+// { dg-module-do run }
+export module M;
+
+inline int b = 42;
+struct A
+{
+  static inline int a = 4200;
+};
+
+export inline int f() { return b+A::a; }
diff --git a/gcc/testsuite/g++.dg/modules/inline-1_b.C b/gcc/testsuite/g++.dg/modules/inline-1_b.C
new file mode 100644 (file)
index 0000000..af319b1
--- /dev/null
@@ -0,0 +1,8 @@
+// { dg-additional-options -fmodules }
+import M;
+
+int main()
+{
+  if (f() != 4242)
+    __builtin_abort ();
+}