From: Nathaniel Shead Date: Sun, 26 Oct 2025 09:32:10 +0000 (+1100) Subject: c++/modules: Propagate type of array with deduced size to its template [PR122422] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a2e0ea5acfb88250ab78785456043f941f27ec2;p=thirdparty%2Fgcc.git c++/modules: Propagate type of array with deduced size to its template [PR122422] 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 Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 2089e4c21ac..e2c20a34e6f 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -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 index 00000000000..c169d110fab --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/merge-20.h @@ -0,0 +1,5 @@ +// PR c++/122422 +template struct Type { + constexpr static int arr[] = { 42, 43, 44 }; +}; +inline Type 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 index 00000000000..2fca74b244d --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/merge-20_a.H @@ -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 index 00000000000..fc69fa0478d --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/merge-20_b.C @@ -0,0 +1,5 @@ +// PR c++/122422 +// { dg-additional-options "-fmodules -fno-module-lazy" } + +#include "merge-20.h" +import "merge-20_a.H";