]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: using non-dep array var of unknown bound [PR115358]
authorPatrick Palka <ppalka@redhat.com>
Tue, 25 Jun 2024 14:42:21 +0000 (10:42 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 25 Jun 2024 14:42:21 +0000 (10:42 -0400)
For a non-dependent array variable of unknown bound, it seems we need to
try instantiating its definition upon use in a template context for sake
of proper checking and typing of the overall expression, like we do for
function specializations with deduced return type.

PR c++/115358

gcc/cp/ChangeLog:

* decl2.cc (mark_used): Call maybe_instantiate_decl for an array
variable with unknown bound.
* semantics.cc (finish_decltype_type): Remove now redundant
handling of array variables with unknown bound.
* typeck.cc (cxx_sizeof_expr): Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/template/array37.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/decl2.cc
gcc/cp/semantics.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/template/array37.C [new file with mode: 0644]

index 6c3ef60d51f8c837fd8e378e4cd745854805678d..cdd2b8aada2149fa68836c0194b040a0412839f4 100644 (file)
@@ -6001,6 +6001,8 @@ mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */)
      find out its type.  For OpenMP user defined reductions, we need them
      instantiated for reduction clauses which inline them by hand directly.  */
   if (undeduced_auto_decl (decl)
+      || (VAR_P (decl)
+         && VAR_HAD_UNKNOWN_BOUND (decl))
       || (TREE_CODE (decl) == FUNCTION_DECL
          && DECL_OMP_DECLARE_REDUCTION_P (decl)))
     maybe_instantiate_decl (decl);
index 08f5f245e7d11a76b975bb04c0075ded1b3ca8ba..6c1813d37c61a9f7a3cdd96db65078d6ba987026 100644 (file)
@@ -12002,13 +12002,6 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
       return error_mark_node;
     }
 
-  /* To get the size of a static data member declared as an array of
-     unknown bound, we need to instantiate it.  */
-  if (VAR_P (expr)
-      && VAR_HAD_UNKNOWN_BOUND (expr)
-      && DECL_TEMPLATE_INSTANTIATION (expr))
-    instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
-
   if (id_expression_or_member_access_p)
     {
       /* If e is an id-expression or a class member access (5.2.5
index 717eb63eb9850b12dcb9b05079f638e24e352973..50f48768a95d59cf54a543fc83fb22fea4d37d99 100644 (file)
@@ -2130,13 +2130,6 @@ cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
   location_t e_loc = cp_expr_loc_or_loc (e, loc);
   STRIP_ANY_LOCATION_WRAPPER (e);
 
-  /* To get the size of a static data member declared as an array of
-     unknown bound, we need to instantiate it.  */
-  if (VAR_P (e)
-      && VAR_HAD_UNKNOWN_BOUND (e)
-      && DECL_TEMPLATE_INSTANTIATION (e))
-    instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
-
   if (TREE_CODE (e) == PARM_DECL
       && DECL_ARRAY_PARAMETER_P (e)
       && (complain & tf_warning))
diff --git a/gcc/testsuite/g++.dg/template/array37.C b/gcc/testsuite/g++.dg/template/array37.C
new file mode 100644 (file)
index 0000000..c5c1166
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/115358
+
+template<class T>
+struct A { static int STR[]; };
+
+template<class T>
+int A<T>::STR[] = {1,2,3};
+
+void f(int(&)[3]);
+
+template<class T>
+void g() {
+  f(A<int>::STR); // { dg-bogus "int []" }
+}