From patchwork Thu Mar 3 02:50:35 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 85216 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id F12C3B70DB for ; Thu, 3 Mar 2011 13:50:49 +1100 (EST) Received: (qmail 6194 invoked by alias); 3 Mar 2011 02:50:48 -0000 Received: (qmail 6186 invoked by uid 22791); 3 Mar 2011 02:50:47 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, TW_CX, TW_RG, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 03 Mar 2011 02:50:37 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p232oa8Q008059 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 2 Mar 2011 21:50:36 -0500 Received: from [127.0.0.1] (ovpn-113-31.phx2.redhat.com [10.3.113.31]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p232oZvN012826 for ; Wed, 2 Mar 2011 21:50:36 -0500 Message-ID: <4D6F01FB.5010209@redhat.com> Date: Wed, 02 Mar 2011 21:50:35 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/47774 (bogus constexpr error with template ctor) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The test in build_vec_init_expr for template context wasn't allowing for instantiations, as it should have. And in any case, that isn't the time to be complaining; being in a constexpr function isn't the only context that requires a constant expression. So this patch defers the diagnostic to the point where we are actually requiring a constant expression. Tested x86_64-pc-linux-gnu, applied to trunk. commit 2ffec02b459771b2586b360ac8b07443cee7dfef Author: Jason Merrill Date: Tue Mar 1 17:14:22 2011 -0500 PR c++/47774 * tree.c (build_vec_init_elt): Split out from... (build_vec_init_expr): ...here. (diagnose_non_constexpr_vec_init): New fn. * semantics.c (potential_constant_expression_1): Use it. * cp-tree.h: Declare it. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index d5a6d5c..4b49046 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5400,6 +5400,7 @@ extern tree build_cplus_array_type (tree, tree); extern tree build_array_of_n_type (tree, int); extern tree build_array_copy (tree); extern tree build_vec_init_expr (tree, tree); +extern void diagnose_non_constexpr_vec_init (tree); extern tree hash_tree_cons (tree, tree, tree); extern tree hash_tree_chain (tree, tree); extern tree build_qualified_name (tree, tree, tree, bool); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 6b3e914..52a962d 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7722,7 +7722,10 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) return true; if (flags & tf_error) - error ("non-constant array initialization"); + { + error ("non-constant array initialization"); + diagnose_non_constexpr_vec_init (t); + } return false; default: diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index ed4f67b..56639ff 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -456,6 +456,47 @@ build_cplus_new (tree type, tree init) return rval; } +/* Subroutine of build_vec_init_expr: Build up a single element + intialization as a proxy for the full array initialization to get things + marked as used and any appropriate diagnostics. + + Since we're deferring building the actual constructor calls until + gimplification time, we need to build one now and throw it away so + that the relevant constructor gets mark_used before cgraph decides + what functions are needed. Here we assume that init is either + NULL_TREE, void_type_node (indicating value-initialization), or + another array to copy. */ + +static tree +build_vec_init_elt (tree type, tree init) +{ + tree inner_type = strip_array_types (type); + VEC(tree,gc) *argvec; + + if (integer_zerop (array_type_nelts_total (type)) + || !CLASS_TYPE_P (inner_type)) + /* No interesting initialization to do. */ + return integer_zero_node; + else if (init == void_type_node) + return build_value_init (inner_type, tf_warning_or_error); + + gcc_assert (init == NULL_TREE + || (same_type_ignoring_top_level_qualifiers_p + (type, TREE_TYPE (init)))); + + argvec = make_tree_vector (); + if (init) + { + tree dummy = build_dummy_object (inner_type); + if (!real_lvalue_p (init)) + dummy = move (dummy); + VEC_quick_push (tree, argvec, dummy); + } + return build_special_member_call (NULL_TREE, complete_ctor_identifier, + &argvec, inner_type, LOOKUP_NORMAL, + tf_warning_or_error); +} + /* Return a TARGET_EXPR which expresses the initialization of an array to be named later, either default-initialization or copy-initialization from another array of the same type. */ @@ -464,62 +505,22 @@ tree build_vec_init_expr (tree type, tree init) { tree slot; - tree inner_type = strip_array_types (type); - tree elt_init = integer_zero_node; bool value_init = false; + tree elt_init = build_vec_init_elt (type, init); - /* Since we're deferring building the actual constructor calls until - gimplification time, we need to build one now and throw it away so - that the relevant constructor gets mark_used before cgraph decides - what functions are needed. Here we assume that init is either - NULL_TREE, void_type_node (indicating value-initialization), or - another array to copy. */ - if (integer_zerop (array_type_nelts_total (type))) + if (init == void_type_node) { - /* No actual initialization to do. */; - init = NULL_TREE; - } - else if (init == void_type_node) - { - elt_init = build_value_init (inner_type, tf_warning_or_error); value_init = true; init = NULL_TREE; } - else - { - gcc_assert (init == NULL_TREE - || (same_type_ignoring_top_level_qualifiers_p - (type, TREE_TYPE (init)))); - - if (CLASS_TYPE_P (inner_type)) - { - VEC(tree,gc) *argvec = make_tree_vector (); - if (init) - { - tree dummy = build_dummy_object (inner_type); - if (!real_lvalue_p (init)) - dummy = move (dummy); - VEC_quick_push (tree, argvec, dummy); - } - elt_init - = build_special_member_call (NULL_TREE, complete_ctor_identifier, - &argvec, inner_type, LOOKUP_NORMAL, - tf_warning_or_error); - } - } slot = build_local_temp (type); init = build2 (VEC_INIT_EXPR, type, slot, init); SET_EXPR_LOCATION (init, input_location); - if (current_function_decl - && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) - { - if (potential_constant_expression (elt_init)) - VEC_INIT_EXPR_IS_CONSTEXPR (init) = true; - else if (!processing_template_decl) - require_potential_constant_expression (elt_init); - } + if (cxx_dialect >= cxx0x + && potential_constant_expression (elt_init)) + VEC_INIT_EXPR_IS_CONSTEXPR (init) = true; VEC_INIT_EXPR_VALUE_INIT (init) = value_init; init = build_target_expr (slot, init); @@ -528,6 +529,23 @@ build_vec_init_expr (tree type, tree init) return init; } +/* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context + that requires a constant expression. */ + +void +diagnose_non_constexpr_vec_init (tree expr) +{ + tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr)); + tree init, elt_init; + if (VEC_INIT_EXPR_VALUE_INIT (expr)) + init = void_zero_node; + else + init = VEC_INIT_EXPR_INIT (expr); + + elt_init = build_vec_init_elt (type, init); + require_potential_constant_expression (elt_init); +} + tree build_array_copy (tree init) { diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor9.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor9.C new file mode 100644 index 0000000..b7693f1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor9.C @@ -0,0 +1,19 @@ +// PR c++/47774 +// { dg-options -std=c++0x } + +struct A +{ + A() {} +}; + +template +struct array +{ + constexpr array() : mem() {} + T mem[7]; +}; + +int main() +{ + array ar; +}