C++: add type checking for static local vector variable in template
authorwangpc <pc.wang@linux.alibaba.com>
Sat, 18 Sep 2021 05:53:09 +0000 (13:53 +0800)
committerJason Merrill <jason@redhat.com>
Tue, 21 Sep 2021 00:52:46 +0000 (20:52 -0400)
This patch moves verify_type_context from start_decl_1 to cp_finish_decl to
do more type checking such as static local vector variable in C++ template.

2021-08-06  wangpc  <pc.wang@linux.alibaba.com>

gcc/cp/ChangeLog

* decl.c (start_decl_1): Move verify_type_context to ...
(cp_finish_decl): ... to here.

gcc/testsuite/ChangeLog

* g++.target/aarch64/sve/static-var-in-template.C: New test.

gcc/cp/decl.c
gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C [new file with mode: 0644]

index 58ddc6a..722e540 100644 (file)
@@ -5772,14 +5772,6 @@ start_decl_1 (tree decl, bool initialized)
       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
     }
 
-  if (is_global_var (decl))
-    {
-      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
-                                  ? TCTX_THREAD_STORAGE
-                                  : TCTX_STATIC_STORAGE);
-      verify_type_context (input_location, context, TREE_TYPE (decl));
-    }
-
   if (initialized)
     /* Is it valid for this decl to have an initializer at all?  */
     {
@@ -7977,6 +7969,14 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       && DECL_INITIALIZED_IN_CLASS_P (decl))
     check_static_variable_definition (decl, type);
 
+  if (!processing_template_decl && VAR_P (decl) && is_global_var (decl))
+    {
+      type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
+                                  ? TCTX_THREAD_STORAGE
+                                  : TCTX_STATIC_STORAGE);
+      verify_type_context (input_location, context, TREE_TYPE (decl));
+    }
+
   if (init && TREE_CODE (decl) == FUNCTION_DECL)
     {
       tree clone;
diff --git a/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C b/gcc/testsuite/g++.target/aarch64/sve/static-var-in-template.C
new file mode 100644 (file)
index 0000000..74237ff
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+
+#include <arm_sve.h>
+
+template <int N>
+void f()
+{
+    static svbool_t pg = svwhilelt_b64(0, N);
+}
+
+int main(int argc, char **argv)
+{
+    f<2>();
+    return 0;
+}
+
+/* { dg-error "SVE type 'svbool_t' does not have a fixed size" "" { target *-*-* } 0 } */