From patchwork Thu Mar 17 02:32:34 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 87331 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 CB8ECB6FD2 for ; Thu, 17 Mar 2011 13:32:51 +1100 (EST) Received: (qmail 1240 invoked by alias); 17 Mar 2011 02:32:41 -0000 Received: (qmail 1142 invoked by uid 22791); 17 Mar 2011 02:32:41 -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; Thu, 17 Mar 2011 02:32: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 p2H2WZZw008905 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 16 Mar 2011 22:32:35 -0400 Received: from [127.0.0.1] (ovpn-113-145.phx2.redhat.com [10.3.113.145]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p2H2WY64021936 for ; Wed, 16 Mar 2011 22:32:35 -0400 Message-ID: <4D8172C2.70403@redhat.com> Date: Wed, 16 Mar 2011 22:32:34 -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++/48089 (ICE with invalid constexpr 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 In a normal constexpr function, we treat *this as a potential constant expression. But in a constexpr constructor it isn't, since we're still in the process of initializing the object. Tested x86_64-pc-linux-gnu, applying to trunk. commit 0f23f8a6fca94b1a00a51ea34643268e9ff840e9 Author: Jason Merrill Date: Wed Mar 16 20:55:52 2011 -0400 PR c++/48089 * semantics.c (potential_constant_expression_1): Don't allow *this in a constructor. (register_constexpr_fundef): Use potential_rvalue_constant_expression. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index a0c5ae3..7519d26 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -5674,11 +5674,11 @@ register_constexpr_fundef (tree fun, tree body) body = unshare_expr (TREE_OPERAND (body, 0)); } - if (!potential_constant_expression (body)) + if (!potential_rvalue_constant_expression (body)) { DECL_DECLARED_CONSTEXPR_P (fun) = false; if (!DECL_TEMPLATE_INSTANTIATION (fun)) - require_potential_constant_expression (body); + require_potential_rvalue_constant_expression (body); return NULL; } fundef->body = body; @@ -7496,7 +7496,16 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) tree x = TREE_OPERAND (t, 0); STRIP_NOPS (x); if (is_this_parameter (x)) - return true; + { + if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)) && want_rval) + { + if (flags & tf_error) + error ("the value of the object being constructed is " + "not a constant expression"); + return false; + } + return true; + } return potential_constant_expression_1 (x, rval, flags); } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C new file mode 100644 index 0000000..88ef3d6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-48089.C @@ -0,0 +1,9 @@ +// PR c++/48089 +// { dg-options -std=c++0x } + +struct s { + constexpr s() : v(v) { } // { dg-error "object being constructed" } + char v; +}; + +s bang;