From patchwork Fri Apr 8 02:17:29 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 90244 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 84AF0B6F84 for ; Fri, 8 Apr 2011 12:17:39 +1000 (EST) Received: (qmail 29365 invoked by alias); 8 Apr 2011 02:17:37 -0000 Received: (qmail 29355 invoked by uid 22791); 8 Apr 2011 02:17:36 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, 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; Fri, 08 Apr 2011 02:17:31 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p382HU4c000571 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 7 Apr 2011 22:17:30 -0400 Received: from [127.0.0.1] ([10.3.113.14]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p382HUrH025764 for ; Thu, 7 Apr 2011 22:17:30 -0400 Message-ID: <4D9E7039.2080305@redhat.com> Date: Thu, 07 Apr 2011 22:17:29 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Fedora/3.1.9-0.39.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/48500 (C++0x crash on non-type-dependent but non-constant call in template) 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 constexpr fallout. In fold_non_dependent_expr, we determined that the expression is not type-dependent, is potentially constant, and is not value-dependent, so we try to fold it. But it isn't potentially constant. So this patch fixes that determination. Tested x86_64-pc-linux-gnu, applying to trunk and 4.6 (regression). commit e49c0a722ad7226f7dabfc4a2fea9e47f8fc235f Author: Jason Merrill Date: Thu Apr 7 14:52:08 2011 -0400 PR c++/48500 * semantics.c (potential_constant_expression_1) [CALL_EXPR]: Check arguments even if we don't know the function. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 2184a53..0b4d1ec 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7358,6 +7358,8 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) class member access expression, including the result of the implicit transformation in the body of the non-static member function (9.3.1); */ + /* FIXME this restriction seems pointless since the standard dropped + "potential constant expression". */ if (is_this_parameter (t)) { if (flags & tf_error) @@ -7373,51 +7375,63 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) { tree fun = get_function_named_in_call (t); const int nargs = call_expr_nargs (t); - if (TREE_CODE (fun) != FUNCTION_DECL) + i = 0; + + if (is_overloaded_fn (fun)) + { + if (TREE_CODE (fun) == FUNCTION_DECL) + { + if (builtin_valid_in_constant_expr_p (fun)) + return true; + if (!DECL_DECLARED_CONSTEXPR_P (fun) + && !morally_constexpr_builtin_function_p (fun)) + { + if (flags & tf_error) + error ("%qD is not %", fun); + return false; + } + /* A call to a non-static member function takes the address + of the object as the first argument. But in a constant + expression the address will be folded away, so look + through it now. */ + if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) + && !DECL_CONSTRUCTOR_P (fun)) + { + tree x = get_nth_callarg (t, 0); + if (is_this_parameter (x)) + /* OK. */; + else if (!potential_constant_expression_1 (x, rval, flags)) + { + if (flags & tf_error) + error ("object argument is not a potential " + "constant expression"); + return false; + } + i = 1; + } + } + else + fun = get_first_fn (fun); + /* Skip initial arguments to base constructors. */ + if (DECL_BASE_CONSTRUCTOR_P (fun)) + i = num_artificial_parms_for (fun); + fun = DECL_ORIGIN (fun); + } + else { if (potential_constant_expression_1 (fun, rval, flags)) - /* Might end up being a constant function pointer. */ - return true; - if (flags & tf_error) - error ("%qE is not a function name", fun); - return false; - } - /* Skip initial arguments to base constructors. */ - if (DECL_BASE_CONSTRUCTOR_P (fun)) - i = num_artificial_parms_for (fun); - else - i = 0; - fun = DECL_ORIGIN (fun); - if (builtin_valid_in_constant_expr_p (fun)) - return true; - if (!DECL_DECLARED_CONSTEXPR_P (fun) - && !morally_constexpr_builtin_function_p (fun)) - { - if (flags & tf_error) - error ("%qD is not %", fun); - return false; + /* Might end up being a constant function pointer. */; + else + { + if (flags & tf_error) + error ("%qE is not a function name", fun); + return false; + } } for (; i < nargs; ++i) { tree x = get_nth_callarg (t, i); - /* A call to a non-static member function takes the - address of the object as the first argument. - But in a constant expression the address will be folded - away, so look through it now. */ - if (i == 0 && DECL_NONSTATIC_MEMBER_P (fun) - && !DECL_CONSTRUCTOR_P (fun)) - { - if (is_this_parameter (x)) - /* OK. */; - else if (!potential_constant_expression_1 (x, rval, flags)) - { - if (flags & tf_error) - error ("object argument is not a potential constant " - "expression"); - return false; - } - } - else if (!potential_constant_expression_1 (x, rval, flags)) + if (!potential_constant_expression_1 (x, rval, flags)) { if (flags & tf_error) error ("argument in position %qP is not a " diff --git a/gcc/testsuite/g++.dg/cpp0x/regress/call1.C b/gcc/testsuite/g++.dg/cpp0x/regress/call1.C new file mode 100644 index 0000000..833318b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/regress/call1.C @@ -0,0 +1,13 @@ +// PR c++/48500 +// { dg-options -std=c++0x } + +struct linked_ptr { +}; +template linked_ptr make_linked_ptr(T* ptr); +struct Concrete; +struct NewedClass { + NewedClass(const Concrete& req){} +}; +template void AddObjToChange(const ArgT& req) { + linked_ptr p = make_linked_ptr(new NewedClass(req)); +}