From patchwork Tue Dec 14 22:32:49 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 75575 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 9284DB6F11 for ; Wed, 15 Dec 2010 09:33:11 +1100 (EST) Received: (qmail 29450 invoked by alias); 14 Dec 2010 22:33:04 -0000 Received: (qmail 29436 invoked by uid 22791); 14 Dec 2010 22:33:01 -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; Tue, 14 Dec 2010 22:32:51 +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.13.8/8.13.8) with ESMTP id oBEMWo10030262 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 14 Dec 2010 17:32:50 -0500 Received: from [127.0.0.1] (ovpn-113-91.phx2.redhat.com [10.3.113.91]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id oBEMWn0M016932 for ; Tue, 14 Dec 2010 17:32:49 -0500 Message-ID: <4D07F091.3040309@redhat.com> Date: Tue, 14 Dec 2010 17:32:49 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20101206 Lightning/1.0b1 Shredder/3.0.11pre MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/46930 (ICE with uninitialized constexpr static data member) 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 A constexpr variable, must be initialized when it is declared; you can't forward declare a constexpr variable. This applies to static data members, too. Tested x86_64-pc-linux-gnu, applied to trunk. commit 67091a3efa479b3611252b8174c878265f029a1a Author: Jason Merrill Date: Tue Dec 14 01:52:54 2010 -0500 PR c++/46930 * decl.c (grokdeclarator): Reject uninitialized constexpr static data member. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 1be0f97..f9331bc 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -9763,6 +9763,13 @@ grokdeclarator (const cp_declarator *declarator, if (thread_p) DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); + + if (constexpr_p && !initialized) + { + error ("constexpr static data member %qD must have an " + "initializer", decl); + constexpr_p = false; + } } else { diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-decl.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-decl.C new file mode 100644 index 0000000..0a3fcb6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-decl.C @@ -0,0 +1,10 @@ +// PR c++/46930 +// { dg-options -std=c++0x } + +struct S { + static constexpr int size; // { dg-error "must have an initializer" } + // { dg-error "previous declaration" "" { target *-*-* } 5 } +}; + +const int limit = 2 * S::size; +constexpr int S::size = 256; // { dg-error "" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C index c7757f4..6b090a0 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ex1.C @@ -16,9 +16,9 @@ struct S { constexpr int twice(); constexpr int t(); // { dg-message "used but never defined" } private: - static constexpr int val; // constexpr variable + static constexpr int val = 7; // constexpr variable }; -constexpr int S::val = 7; + constexpr int S::twice() { return val + val; } constexpr S s = { }; int x1 = s.twice(); // ok diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-static5.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-static5.C index cb553a2..a401cc0 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-static5.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-static5.C @@ -3,10 +3,10 @@ template struct A { - constexpr static T t; + constexpr static T t = T(); // { dg-error "literal" } }; template -constexpr T A::t = T(); // { dg-error "not literal" } +constexpr T A::t; struct B {