From patchwork Mon Jan 2 17:52:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 133889 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 9E9D7B6F9D for ; Tue, 3 Jan 2012 04:52:35 +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=1326131556; 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=3cCHC69 8wotiMiYgKaTVSQuY8m8=; b=RNJN6mz5a7GfJkSmMcMHFPi2pWi1REVmZggP1WG 7MzItyfnKdTE28V4UuLBXd2A01TtgZxzvcLKn3ylJlJRqMKH0G7USiUlG+KpQ5A7 /WuZVWR7vJFZ1arnXVTbJNpvwiZR9+Zb9NyiuMlzkF/xOpVSWv9pod3BUx/mNH2b Ls0M= 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=tcU6sjBsP9LnnYLaz/V+v2AvvGo2HtrMsjS6m0gZZSbQZ75aeLm29W1BXttEz4 9a3mnbYrjxClXVJ9h2vaBlQUki9Bn3sKP4CtX9E0a9wuMRIS7H9SyVp6RmoBwoaj Y+0wiJ1oR63iKIn9ZwX8WFZ9eZmOA2i1iF0Gf8ncQUJz8=; Received: (qmail 22177 invoked by alias); 2 Jan 2012 17:52:31 -0000 Received: (qmail 22161 invoked by uid 22791); 2 Jan 2012 17:52:29 -0000 X-SWARE-Spam-Status: No, hits=-7.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_CX, TW_SF 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, 02 Jan 2012 17:52:11 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q02HqAAH018140 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 2 Jan 2012 12:52:10 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q02HqASE011304 for ; Mon, 2 Jan 2012 12:52:10 -0500 Received: from [0.0.0.0] (ovpn-113-37.phx2.redhat.com [10.3.113.37]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id q02Hq9hR004478 for ; Mon, 2 Jan 2012 12:52:09 -0500 Message-ID: <4F01EEC8.6030807@redhat.com> Date: Mon, 02 Jan 2012 12:52:08 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111112 Thunderbird/8.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/51675 (DR 1359, constexpr union) 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 DR 1359 points out that the rules for constexpr constructors require all data members to be initialized, which is wrong for unions. This patch implements the obvious resolution. Tested x86_64-pc-linux-gnu, applying to trunk. commit 82927de9eb3327c2e65be4f56aec991e36c44fa9 Author: Jason Merrill Date: Mon Jan 2 00:40:02 2012 -0500 PR c++/51675 * method.c (walk_field_subobs): Don't check for uninitialized fields in a union. (synthesized_method_walk): Check here. diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 8101f8a..cf2a713 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1063,7 +1063,8 @@ walk_field_subobs (tree fields, tree fnname, special_function_kind sfk, /* For an implicitly-defined default constructor to be constexpr, every member must have a user-provided default constructor or an explicit initializer. */ - if (constexpr_p && !CLASS_TYPE_P (mem_type)) + if (constexpr_p && !CLASS_TYPE_P (mem_type) + && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) { *constexpr_p = false; if (msg) @@ -1208,12 +1209,19 @@ synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p, resolution, so a constructor can be trivial even if it would otherwise call a non-trivial constructor. */ if (expected_trivial - && !diag && (!copy_arg_p || cxx_dialect < cxx0x)) { if (constexpr_p && sfk == sfk_constructor) - *constexpr_p = trivial_default_constructor_is_constexpr (ctype); - return; + { + bool cx = trivial_default_constructor_is_constexpr (ctype); + *constexpr_p = cx; + if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE) + /* A trivial constructor doesn't have any NSDMI. */ + inform (input_location, "defaulted default constructor does " + "not initialize any non-static data member"); + } + if (!diag) + return; } ++cp_unevaluated_operand; diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-union2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-union2.C new file mode 100644 index 0000000..0bf2aa7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-union2.C @@ -0,0 +1,18 @@ +// PR c++/51675 +// { dg-options -std=c++0x } + +union foo +{ + int x = 0; + short y; + + constexpr foo() = default; +}; + +union bar +{ + int x; + short y; + + constexpr bar() = default; // { dg-error "constexpr" } +};