]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Keep DECL_SAVED_TREE of cdtor instantiations in modules [PR104040]
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 29 Mar 2024 02:53:54 +0000 (13:53 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Wed, 10 Apr 2024 01:40:32 +0000 (11:40 +1000)
A template instantiation still needs to have its DECL_SAVED_TREE so that
its definition is emitted into the CMI. This way it can be emitted in
the object file of any importers that use it, in case it doesn't end up
getting emitted in this TU.

This is true even for maybe-in-charge functions, because we don't
currently stream the clones directly but instead regenerate them from
this function.

PR c++/104040

gcc/cp/ChangeLog:

* semantics.cc (expand_or_defer_fn_1): Keep DECL_SAVED_TREE for
all vague linkage cdtors with modules.

gcc/testsuite/ChangeLog:

* g++.dg/modules/pr104040_a.C: New test.
* g++.dg/modules/pr104040_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/modules/pr104040_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr104040_b.C [new file with mode: 0644]

index a43ff6e2ab256b6f843e0845f914ae1996c58b25..329c524a509309ad4dd905ce561c29749734539a 100644 (file)
@@ -5029,9 +5029,13 @@ expand_or_defer_fn_1 (tree fn)
       /* We don't want to process FN again, so pretend we've written
         it out, even though we haven't.  */
       TREE_ASM_WRITTEN (fn) = 1;
-      /* If this is a constexpr function, keep DECL_SAVED_TREE.  */
+      /* If this is a constexpr function we still need the body to be
+        able to evaluate it.  Similarly, with modules we only stream
+        the maybe-in-charge cdtor and regenerate the clones from it on
+        demand, so we also need to keep the body.  Otherwise we don't
+        need it anymore.  */
       if (!DECL_DECLARED_CONSTEXPR_P (fn)
-         && !(modules_p () && DECL_DECLARED_INLINE_P (fn)))
+         && !(modules_p () && vague_linkage_p (fn)))
        DECL_SAVED_TREE (fn) = NULL_TREE;
       return false;
     }
diff --git a/gcc/testsuite/g++.dg/modules/pr104040_a.C b/gcc/testsuite/g++.dg/modules/pr104040_a.C
new file mode 100644 (file)
index 0000000..ea36ce0
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/104040
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi test }
+
+export module test;
+
+export template <typename T>
+struct test {
+  ~test() {}
+};
+
+test<bool> use() {
+  return {};
+}
diff --git a/gcc/testsuite/g++.dg/modules/pr104040_b.C b/gcc/testsuite/g++.dg/modules/pr104040_b.C
new file mode 100644 (file)
index 0000000..efe0146
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/104040
+// { dg-additional-options "-fmodules-ts" }
+
+import test;
+
+int main() {
+  test<bool> t{};
+}