Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? This fixes PR115358 whose testcase used a constexpr static array variable, but it seems the general issue is not specific to constexpr as illustrated by the below testcase. Note that Clang currently rejects the testcase for a similar reason to GCC...
-- >8 -- 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 its expression context. This seems analogous to deducing the return type of a function which is similarly done upon first use even in a template context. 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. --- gcc/cp/decl2.cc | 2 ++ gcc/cp/semantics.cc | 7 ------- gcc/cp/typeck.cc | 7 ------- gcc/testsuite/g++.dg/template/array37.C | 16 ++++++++++++++++ 4 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/array37.C diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index 6c3ef60d51f..cdd2b8aada2 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -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); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index b4f626924af..3247521e03e 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -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 diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 5970ac3d398..9297948cfa5 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -2127,13 +2127,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 index 00000000000..ed03b955f05 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array37.C @@ -0,0 +1,16 @@ +// PR c++/115358 +// { dg-do compile { target c++14 } } + +template<class T> +struct A { static int STR[]; }; + +template<class T> +int A<T>::STR[] = {1,2,3}; + +void g(int(&)[3]); + +int main() { + [](auto) { + g(A<int>::STR); // { dg-bogus "int []" } + }; +} -- 2.45.2.606.g9005149a4a