]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Propagate type of array with deduced size to its template [PR122422]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sun, 26 Oct 2025 09:32:10 +0000 (20:32 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Mon, 27 Oct 2025 23:45:51 +0000 (10:45 +1100)
This is the same issue as solved in r16-1759-g98fd493db6216c for auto,
but this time for arrays with size deduced from their initializers.

PR c++/122422

gcc/cp/ChangeLog:

* decl.cc (maybe_deduce_size_from_array_init): Propagate type to
corresponding TEMPLATE_DECL.

gcc/testsuite/ChangeLog:

* g++.dg/modules/merge-20.h: New test.
* g++.dg/modules/merge-20_a.H: New test.
* g++.dg/modules/merge-20_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/decl.cc
gcc/testsuite/g++.dg/modules/merge-20.h [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/merge-20_a.H [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/merge-20_b.C [new file with mode: 0644]

index 2089e4c21accf1e055c1b7b70555cacc5b97286e..e2c20a34e6ff4330c97bb561ea0b411b766099b5 100644 (file)
@@ -7096,6 +7096,14 @@ maybe_deduce_size_from_array_init (tree decl, tree init)
       cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
 
       relayout_decl (decl);
+
+      /* Update the type of the corresponding TEMPLATE_DECL to match.  */
+      if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
+       {
+         tree tmpl = template_for_substitution (decl);
+         if (DECL_TEMPLATE_RESULT (tmpl) == decl)
+           TREE_TYPE (tmpl) = TREE_TYPE (decl);
+       }
     }
 }
 
diff --git a/gcc/testsuite/g++.dg/modules/merge-20.h b/gcc/testsuite/g++.dg/modules/merge-20.h
new file mode 100644 (file)
index 0000000..c169d11
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/122422
+template <typename> struct Type {
+  constexpr static int arr[] = { 42, 43, 44 };
+};
+inline Type<int> tt;
diff --git a/gcc/testsuite/g++.dg/modules/merge-20_a.H b/gcc/testsuite/g++.dg/modules/merge-20_a.H
new file mode 100644 (file)
index 0000000..2fca74b
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/122422
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+#include "merge-20.h"
diff --git a/gcc/testsuite/g++.dg/modules/merge-20_b.C b/gcc/testsuite/g++.dg/modules/merge-20_b.C
new file mode 100644 (file)
index 0000000..fc69fa0
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/122422
+// { dg-additional-options "-fmodules -fno-module-lazy" }
+
+#include "merge-20.h"
+import "merge-20_a.H";