From patchwork Mon Feb 21 15:34:25 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 83851 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 ED4D7B7136 for ; Tue, 22 Feb 2011 02:34:44 +1100 (EST) Received: (qmail 28447 invoked by alias); 21 Feb 2011 15:34:42 -0000 Received: (qmail 28164 invoked by uid 22791); 21 Feb 2011 15:34:40 -0000 X-SWARE-Spam-Status: No, hits=-6.2 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; Mon, 21 Feb 2011 15:34:27 +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 p1LFYQHx001313 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 21 Feb 2011 10:34:26 -0500 Received: from [127.0.0.1] (ovpn-113-43.phx2.redhat.com [10.3.113.43]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p1LFYPC2022110 for ; Mon, 21 Feb 2011 10:34:26 -0500 Message-ID: <4D628601.1090207@redhat.com> Date: Mon, 21 Feb 2011 10:34:25 -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++/47207 (ICE on constexpr var in own init) 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 Here decl_constant_var_p was asserting that a constexpr variable is always usable as a constant, which is not the case if it's used in its own initializer. Tested x86_64-pc-linux-gnu, applied to trunk. commit 54c825f1ac61ada5551de1610ac15198f920b885 Author: Jason Merrill Date: Mon Feb 21 09:56:11 2011 -0500 PR c++/47207 * decl2.c (decl_constant_var_p): A constexpr var needs an initializer to be constant. * semantics.c (cxx_eval_constant_expression): Complain about constexpr var used in its own initializer. * call.c (set_up_extended_ref_temp): Set DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P too. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 078542a..8dccbbe 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -8149,6 +8149,7 @@ set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp) Currently this is only useful for initializer_list temporaries, since reference vars can't appear in constant expressions. */ DECL_DECLARED_CONSTEXPR_P (var) = true; + DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true; TREE_CONSTANT (var) = true; } DECL_INITIAL (var) = init; diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index a4b7dfa..93d44a4 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -3550,20 +3550,21 @@ decl_constant_var_p (tree decl) tree type = TREE_TYPE (decl); if (TREE_CODE (decl) != VAR_DECL) return false; - if (DECL_DECLARED_CONSTEXPR_P (decl)) - ret = true; - else if (CP_TYPE_CONST_NON_VOLATILE_P (type) - && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) + if (DECL_DECLARED_CONSTEXPR_P (decl) + || (CP_TYPE_CONST_NON_VOLATILE_P (type) + && INTEGRAL_OR_ENUMERATION_TYPE_P (type))) { /* We don't know if a template static data member is initialized with - a constant expression until we instantiate its initializer. */ + a constant expression until we instantiate its initializer. Even + in the case of a constexpr variable, we can't treat it as a + constant until its initializer is complete in case it's used in + its own initializer. */ mark_used (decl); ret = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl); } else ret = false; - gcc_assert (!ret || DECL_INITIAL (decl)); return ret; } diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index b7ed525..6a9c6a0 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6768,7 +6768,10 @@ cxx_eval_constant_expression (const constexpr_call *call, tree t, tree type = TREE_TYPE (r); error ("the value of %qD is not usable in a constant " "expression", r); - if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) + if (DECL_DECLARED_CONSTEXPR_P (r)) + inform (DECL_SOURCE_LOCATION (r), + "%qD used in its own initializer", r); + else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) { if (!CP_TYPE_CONST_P (type)) inform (DECL_SOURCE_LOCATION (r), diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-diag2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-diag2.C new file mode 100644 index 0000000..c78416e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-diag2.C @@ -0,0 +1,5 @@ +// PR c++/47207 +// { dg-options -std=c++0x } + +constexpr int X (X); // { dg-error "not usable" } +// { dg-message "own initializer" "" { target *-*-* } 4 }