From patchwork Mon Jan 16 18:39:04 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 136347 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 B07A5B6EF1 for ; Tue, 17 Jan 2012 05:45:59 +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=1327344361; 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=W6/L6xd 4/gFprZoN8zOh5gAnEyU=; b=T6Rq2O3uW7k21d50ulYV3SLald1wg4oM08lJxsn ysR4uD+fHVxxr7BZQOh2ygl0MP/gKnqctl6q/K6Q611aGAsE6rc0ab0KA4LBxqoW BffAgObkckjKhm5qVdT+2BMbP0432bWGxEaztXMP7HEo7q7qvV8UgUW0QlfRygaQ qmU0= 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=w13TdfHqykxDoFdbNCu7e9XdyR4+oZPE1k+mWSNMy1CoRgM3unFFE/rKFM+eSU GRkdh3qcUeg+2LwuB1GriRkLy/w1eiOS1kqDSsULCQzAcncNvVcJAwWik+Jg1pOZ np8kwfq8U3Xb2BNHwICZDjzNM/Jvjsg7zORY5YoEJcR0A=; Received: (qmail 31537 invoked by alias); 16 Jan 2012 18:45:55 -0000 Received: (qmail 31526 invoked by uid 22791); 16 Jan 2012 18:45:54 -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; Mon, 16 Jan 2012 18:45:40 +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 q0GIjSkl004009 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 16 Jan 2012 13:45:39 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q0GId77D024479 for ; Mon, 16 Jan 2012 13:39:07 -0500 Received: from [0.0.0.0] (ovpn-113-35.phx2.redhat.com [10.3.113.35]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id q0GId5MB001505 for ; Mon, 16 Jan 2012 13:39:06 -0500 Message-ID: <4F146EC8.6000106@redhat.com> Date: Mon, 16 Jan 2012 13:39:04 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:9.0) Gecko/20111222 Thunderbird/9.0 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/51868 (static_cast, rvalue refs and bit-fields) 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 There were two problems with build_static_cast_1's handling of this testcase: 1) We weren't using unlowered_expr_type to get the type of the expression, so we were comparing int to the internal bit-field type, which doesn't match. 2) We were trying to bind an lvalue reference to a bit-field in the process of converting to rvalue reference, which doesn't work; for bit-fields, we should convert to an rvalue temporary and then bind the rvalue reference to that. Tested x86_64-pc-linux-gnu, applying to trunk. commit 1e5a058d0b7b0226a5956ee991b9a47757d2aedd Author: Jason Merrill Date: Mon Jan 16 13:12:26 2012 -0500 PR c++/51868 * typeck.c (build_static_cast_1): Handle bit-fields properly. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 11edeee..91e7a0a 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5812,11 +5812,12 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p, { tree intype; tree result; + cp_lvalue_kind clk; /* Assume the cast is valid. */ *valid_p = true; - intype = TREE_TYPE (expr); + intype = unlowered_expr_type (expr); /* Save casted types in the function's used types hash table. */ used_types_insert (type); @@ -5882,22 +5883,29 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p, cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */ if (TREE_CODE (type) == REFERENCE_TYPE && TYPE_REF_IS_RVALUE (type) - && real_lvalue_p (expr) + && (clk = real_lvalue_p (expr)) && reference_related_p (TREE_TYPE (type), intype) && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype))) { - /* Handle the lvalue case here by casting to lvalue reference and - then changing it to an rvalue reference. Casting an xvalue to - rvalue reference will be handled by the main code path. */ - tree lref = cp_build_reference_type (TREE_TYPE (type), false); - result = (perform_direct_initialization_if_possible - (lref, expr, c_cast_p, complain)); - result = cp_fold_convert (type, result); - /* Make sure we don't fold back down to a named rvalue reference, - because that would be an lvalue. */ - if (DECL_P (result)) - result = build1 (NON_LVALUE_EXPR, type, result); - return convert_from_reference (result); + if (clk == clk_ordinary) + { + /* Handle the (non-bit-field) lvalue case here by casting to + lvalue reference and then changing it to an rvalue reference. + Casting an xvalue to rvalue reference will be handled by the + main code path. */ + tree lref = cp_build_reference_type (TREE_TYPE (type), false); + result = (perform_direct_initialization_if_possible + (lref, expr, c_cast_p, complain)); + result = cp_fold_convert (type, result); + /* Make sure we don't fold back down to a named rvalue reference, + because that would be an lvalue. */ + if (DECL_P (result)) + result = build1 (NON_LVALUE_EXPR, type, result); + return convert_from_reference (result); + } + else + /* For a bit-field or packed field, bind to a temporary. */ + expr = rvalue (expr); } /* Resolve overloaded address here rather than once in diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-bitfield.C b/gcc/testsuite/g++.dg/cpp0x/rv-bitfield.C new file mode 100644 index 0000000..a269a76 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/rv-bitfield.C @@ -0,0 +1,13 @@ +// { dg-options -std=c++0x } + +struct A +{ + int i : 1; +}; + +int main() +{ + A a; + static_cast(a.i); +} + diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-bitfield2.C b/gcc/testsuite/g++.dg/cpp0x/rv-bitfield2.C new file mode 100644 index 0000000..e054151 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/rv-bitfield2.C @@ -0,0 +1,17 @@ +// PR c++/51868 +// { dg-options -std=c++0x } + +struct A { + A() {} + A(const A&) {} + A(A&&) {} +}; + +struct B { + A a; + int f : 1; +}; + +B func() { + return B(); +}