From patchwork Wed Feb 8 09:23:09 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 140076 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 D33E9B6EF3 for ; Wed, 8 Feb 2012 20:23:37 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1329297818; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=W5qf1zI 0YqsVCFtB1NVWWUoueoM=; b=lrU8WdT0U30h24gntsxEpx3iEXRj6kVbftJA2l1 O1IC9hreQ/Tlu67uFkVfEdqbTuxlCgt57PAdsez8vdDPL5zJhRe2Ns+3TcygnTmG twi+LX2Dgf9vfzgM/HzqPxUgUA8qXffFu2F3r90RB/KhRpyt3VBTxfSxRUvNjWLC gAOU= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=tmy9bhO530cdLUh3FOlLQg5n9vtba4oJzjQuGFCc3UBGYQyxqPICUiPIsJUswl Un6g0ZYBhhJxuy6ZnJSicKAyx9pkUvjrdsUnTCtk+Z3HrI2OMaoCJ45IdJN8Lh5+ sLhoV50V7spHkUFlxOdLubjenwTXGLyuSWgmk503tQ/zo=; Received: (qmail 19629 invoked by alias); 8 Feb 2012 09:23:32 -0000 Received: (qmail 19616 invoked by uid 22791); 8 Feb 2012 09:23:31 -0000 X-SWARE-Spam-Status: No, hits=-6.6 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; Wed, 08 Feb 2012 09:23:11 +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 q189NBUx026201 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 8 Feb 2012 04:23:11 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q189NBkG012873 for ; Wed, 8 Feb 2012 04:23:11 -0500 Received: from [0.0.0.0] (ovpn-113-40.phx2.redhat.com [10.3.113.40]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id q189N95x015245 for ; Wed, 8 Feb 2012 04:23:10 -0500 Message-ID: <4F323EFD.5040500@redhat.com> Date: Tue, 07 Feb 2012 23:23:09 -1000 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0) Gecko/20120131 Thunderbird/10.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/51675 (more constexpr unions) 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 More traffic on PR 51675 demonstrates that my earlier patch didn't fix the whole problem. This patch improves handling of user-defined constructors. Tested x86_64-pc-linux-gnu, applying to trunk. commit 2ebb9d76963c08ebb009a1b6eb8f576eb9e671c0 Author: Jason Merrill Date: Mon Feb 6 14:47:50 2012 -1000 PR c++/51675 * semantics.c (cx_check_missing_mem_inits): Handle unions. Fix constexpr default constructor logic. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 9019962..5646fa7 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6025,13 +6025,28 @@ cx_check_missing_mem_inits (tree fun, tree body, bool complain) bool bad; tree field; unsigned i, nelts; + tree ctype; if (TREE_CODE (body) != CONSTRUCTOR) return false; - bad = false; nelts = CONSTRUCTOR_NELTS (body); - field = TYPE_FIELDS (DECL_CONTEXT (fun)); + ctype = DECL_CONTEXT (fun); + field = TYPE_FIELDS (ctype); + + if (TREE_CODE (ctype) == UNION_TYPE) + { + if (nelts == 0 && next_initializable_field (field)) + { + if (complain) + error ("% constructor for union %qT must " + "initialize exactly one non-static data member", ctype); + return true; + } + return false; + } + + bad = false; for (i = 0; i <= nelts; ++i) { tree index; @@ -6050,8 +6065,6 @@ cx_check_missing_mem_inits (tree fun, tree body, bool complain) if (TREE_CODE (field) != FIELD_DECL || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))) continue; - if (!complain) - return true; ftype = strip_array_types (TREE_TYPE (field)); if (type_has_constexpr_default_constructor (ftype)) { @@ -6062,6 +6075,8 @@ cx_check_missing_mem_inits (tree fun, tree body, bool complain) || errorcount != 0); continue; } + if (!complain) + return true; error ("uninitialized member %qD in % constructor", field); bad = true; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union3.C new file mode 100644 index 0000000..bac9cab --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union3.C @@ -0,0 +1,12 @@ +// PR c++/51675 +// { dg-options -std=c++11 } + +union foo +{ + int x; + short y; + + constexpr foo(): x(0) { } +}; + +constexpr foo f;