]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: variable template array of unknown bound [PR113638]
authorJason Merrill <jason@redhat.com>
Thu, 1 Feb 2024 21:54:39 +0000 (16:54 -0500)
committerJason Merrill <jason@redhat.com>
Fri, 2 Feb 2024 10:16:48 +0000 (05:16 -0500)
When we added variable templates, we didn't extend the VAR_HAD_UNKNOWN_BOUND
handling for class template static data members to handle them as well.

PR c++/113638

gcc/cp/ChangeLog:

* cp-tree.h: Adjust comment.
* pt.cc (instantiate_template): Set VAR_HAD_UNKNOWN_BOUND for
variable template.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/var-templ-array1.C: New test.

(cherry picked from commit 0b786ff38ab398087820d91241e030a28c451df9)

gcc/cp/cp-tree.h
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1y/var-templ-array1.C [new file with mode: 0644]

index c539cc353e4a0f49d3eec28b16f62bb13e2be8d0..9e03488f0df461397be2acfd79d4a3af4907d3dd 100644 (file)
@@ -3220,7 +3220,7 @@ struct GTY(()) lang_decl {
 #define DECL_CONV_FN_TYPE(FN) \
   TREE_TYPE ((gcc_checking_assert (DECL_CONV_FN_P (FN)), DECL_NAME (FN)))
 
-/* Nonzero if NODE, a static data member, was declared in its class as an
+/* Nonzero if NODE, a templated variable, was declared as an
    array of unknown bound.  */
 #define VAR_HAD_UNKNOWN_BOUND(NODE)                    \
   (DECL_LANG_SPECIFIC (VAR_DECL_CHECK (NODE))          \
index 3214ce8df4941fbf3eedb1135cc0e1c262431a79..bcee75b8710a80a4069edb6f3ec40903b874764d 100644 (file)
@@ -22155,6 +22155,13 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
      template, not the most general template.  */
   DECL_TI_TEMPLATE (fndecl) = tmpl;
   DECL_TI_ARGS (fndecl) = targ_ptr;
+  if (VAR_P (pattern))
+    {
+      /* Remember if the variable was declared with [].  */
+      if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
+         && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
+       SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
+    }
 
   set_instantiating_module (fndecl);
 
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ-array1.C b/gcc/testsuite/g++.dg/cpp1y/var-templ-array1.C
new file mode 100644 (file)
index 0000000..b0ff7e7
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/113638
+// { dg-do compile { target c++14 } }
+
+template<int ...Is>
+constexpr int my_array[]{Is...};
+constexpr auto t1 = my_array<2>;
+static_assert(sizeof(my_array<1>) == sizeof(int) * 1, "");