From patchwork Mon May 23 22:47:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 97105 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 D1F38B6F57 for ; Tue, 24 May 2011 08:48:06 +1000 (EST) Received: (qmail 4228 invoked by alias); 23 May 2011 22:48:04 -0000 Received: (qmail 4210 invoked by uid 22791); 23 May 2011 22:48:02 -0000 X-SWARE-Spam-Status: No, hits=-6.3 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, 23 May 2011 22:47:49 +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 p4NMlmuQ004724 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 23 May 2011 18:47:48 -0400 Received: from [127.0.0.1] ([10.3.113.3]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p4NMlmns003387 for ; Mon, 23 May 2011 18:47:48 -0400 Message-ID: <4DDAE413.9000707@redhat.com> Date: Mon, 23 May 2011 18:47:47 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/49105 (rejects-valid with cast to const int&&) 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 The main problem in this PR was that build_const_cast_1 hadn't been updated to support rvalue references, so the first patch does that. The second patch fixes a couple of minor issues that I noticed while investigating this; neither of them should affect observable semantics, but they match the standard better. Tested x86_64-pc-linux-gnu, applying to trunk. commit 93e0f8336fe098431665731a91c23647fcd2a29e Author: Jason Merrill Date: Mon May 23 13:21:52 2011 -0400 PR c++/49105 * typeck.c (build_const_cast_1): Handle rvalue references. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 81ee63d..13b919c 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6241,14 +6241,29 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, /* [expr.const.cast] - An lvalue of type T1 can be explicitly converted to an lvalue of - type T2 using the cast const_cast (where T1 and T2 are object - types) if a pointer to T1 can be explicitly converted to the type - pointer to T2 using a const_cast. */ + For two object types T1 and T2, if a pointer to T1 can be explicitly + converted to the type "pointer to T2" using a const_cast, then the + following conversions can also be made: + + -- an lvalue of type T1 can be explicitly converted to an lvalue of + type T2 using the cast const_cast; + + -- a glvalue of type T1 can be explicitly converted to an xvalue of + type T2 using the cast const_cast; and + + -- if T1 is a class type, a prvalue of type T1 can be explicitly + converted to an xvalue of type T2 using the cast const_cast. */ + if (TREE_CODE (dst_type) == REFERENCE_TYPE) { reference_type = dst_type; - if (! real_lvalue_p (expr)) + if (!TYPE_REF_IS_RVALUE (dst_type) + ? real_lvalue_p (expr) + : (CLASS_TYPE_P (TREE_TYPE (dst_type)) + ? lvalue_p (expr) + : lvalue_or_rvalue_with_address_p (expr))) + /* OK. */; + else { if (complain & tf_error) error ("invalid const_cast of an rvalue of type %qT to type %qT", diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C b/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C new file mode 100644 index 0000000..94ee4ca --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C @@ -0,0 +1,21 @@ +// Test for const_cast to reference (5.2.11/4). +// { dg-options -std=c++0x } + +template T&& xval(); +template T& lval(); +template T prval(); + +struct A { }; + +int main() +{ + const_cast(lval()); + const_cast(xval()); // { dg-error "" } + const_cast(prval()); // { dg-error "" } + const_cast(lval()); + const_cast(xval()); + const_cast(prval()); // { dg-error "" } + const_cast(lval()); + const_cast(xval()); + const_cast(prval()); +} commit 120577c5010612c5f7cf2828c165997b72990ddd Author: Jason Merrill Date: Mon May 23 13:31:50 2011 -0400 PR c++/49105 * typeck.c (cp_build_c_cast): Don't strip cv-quals when converting to reference. (build_static_cast_1): Update for glvalues. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 13b919c..69b25d3 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5768,11 +5768,11 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p, return convert_from_reference (rvalue (cp_fold_convert (type, expr))); } - /* "An lvalue of type cv1 T1 can be cast to type rvalue reference to + /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to 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) + && lvalue_or_rvalue_with_address_p (expr) && reference_related_p (TREE_TYPE (type), intype) && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype))) { @@ -6448,7 +6448,7 @@ cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain) if (!CLASS_TYPE_P (type)) type = TYPE_MAIN_VARIANT (type); result_type = TREE_TYPE (result); - if (!CLASS_TYPE_P (result_type)) + if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE) result_type = TYPE_MAIN_VARIANT (result_type); /* If the type of RESULT does not match TYPE, perform a const_cast to make it match. If the static_cast or diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae25.C b/gcc/testsuite/g++.dg/cpp0x/sfinae25.C new file mode 100644 index 0000000..7bdc8f8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/sfinae25.C @@ -0,0 +1,10 @@ +// PR c++/49105 +// { dg-options -std=c++0x } + +template +char f(int); + +template +auto f(...) -> char(&)[2]; + +static_assert(sizeof(f(0)) == 1, "Error"); // #