From patchwork Mon Dec 9 17:31:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 36667 Received: (qmail 79211 invoked by alias); 9 Dec 2019 17:31:21 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 79175 invoked by uid 89); 9 Dec 2019 17:31:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=HX-Languages-Length:4701 X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Dec 2019 17:31:15 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id 2286F20308; Mon, 9 Dec 2019 12:31:11 -0500 (EST) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id 2A3B620391; Mon, 9 Dec 2019 12:31:07 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id 0929728175; Mon, 9 Dec 2019 12:31:07 -0500 (EST) X-Gerrit-PatchSet: 2 Date: Mon, 9 Dec 2019 12:31:07 -0500 From: "Sourceware to Gerrit sync (Code Review)" To: Tankut Baris Aktemur , gdb-patches@sourceware.org Cc: Simon Marchi , Tom Tromey Auto-Submitted: auto-generated X-Gerrit-MessageType: merged Subject: [pushed] gdb: rank an lvalue argument incompatible for an rvalue parameter X-Gerrit-Change-Id: I4a6dfc7dac63efa1e3b9f8f391e4b736fbdccdc1 X-Gerrit-Change-Number: 752 X-Gerrit-ChangeURL: X-Gerrit-Commit: 330f1d3825daa0d9e8d6c54f4fcf6fa5800e5664 In-Reply-To: References: Reply-To: noreply@gnutoolchain-gerrit.osci.io, tankut.baris.aktemur@intel.com, simon.marchi@polymtl.ca, tromey@sourceware.org, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-79-g83ff7f88f1 Message-Id: <20191209173107.0929728175@gnutoolchain-gerrit.osci.io> Sourceware to Gerrit sync has submitted this change. Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/752 ...................................................................... gdb: rank an lvalue argument incompatible for an rvalue parameter Passing an lvalue argument to a function that takes an rvalue parameter is not allowed per C++ rules. Consider this function: int g (int &&x) { return x; } Calling g as in int i = 5; int j = g (i); is illegal. For instance, GCC 9.2.1 yields ~~~ test.cpp: In function ‘int main()’: test.cpp:6:14: error: cannot bind rvalue reference of type ‘int&&’ to lvalue of type ‘int’ 6 | int j = g (i); | ^ ~~~ GDB currently allows this function call: ~~~ (gdb) print g(i) $1 = 5 ~~~ Fix this by ranking an lvalue argument incompatible with an rvalue parameter. The behavior after this patch is: ~~~ (gdb) print g(i) Cannot resolve function g to any overloaded instance ~~~ Tested with GCC 9.2.1. gdb/ChangeLog: 2019-12-09 Tankut Baris Aktemur * gdbtypes.c (rank_one_type): Return INCOMPATIBLE_TYPE_BADNESS when ranking an lvalue argument for an rvalue parameter. gdb/testsuite/ChangeLog: 2019-12-09 Tankut Baris Aktemur * gdb.cp/rvalue-ref-overload.cc (g): New function that takes an rvalue parameter. * gdb.cp/rvalue-ref-overload.exp: Test calling it with an lvalue parameter. Change-Id: I4a6dfc7dac63efa1e3b9f8f391e4b736fbdccdc1 --- M gdb/ChangeLog M gdb/gdbtypes.c M gdb/testsuite/ChangeLog M gdb/testsuite/gdb.cp/rvalue-ref-overload.cc M gdb/testsuite/gdb.cp/rvalue-ref-overload.exp 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 9709d0d..329080e 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2019-12-09 Tankut Baris Aktemur + + * gdbtypes.c (rank_one_type): Return INCOMPATIBLE_TYPE_BADNESS + when ranking an lvalue argument for an rvalue parameter. + 2019-12-08 Wataru Ashihara * darwin-nat.c (darwin_nat_target::create_inferior): Fix diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 508628a..0896f71 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -4303,12 +4303,9 @@ } else { - /* Lvalues should prefer lvalue overloads. */ + /* It's illegal to pass an lvalue as an rvalue. */ if (TYPE_CODE (parm) == TYPE_CODE_RVALUE_REF) - { - rank.subrank = REFERENCE_CONVERSION_RVALUE; - return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS); - } + return INCOMPATIBLE_TYPE_BADNESS; } } diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index bfd686d..123344f 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2019-12-09 Tankut Baris Aktemur + + * gdb.cp/rvalue-ref-overload.cc (g): New function that takes + an rvalue parameter. + * gdb.cp/rvalue-ref-overload.exp: Test calling it with an lvalue + parameter. + 2019-12-09 Andrew Burgess * gdb.mi/mi-fortran-modules.exp: Add patterns to skip system diff --git a/gdb/testsuite/gdb.cp/rvalue-ref-overload.cc b/gdb/testsuite/gdb.cp/rvalue-ref-overload.cc index e3111d5..30634a9 100644 --- a/gdb/testsuite/gdb.cp/rvalue-ref-overload.cc +++ b/gdb/testsuite/gdb.cp/rvalue-ref-overload.cc @@ -62,6 +62,12 @@ return 3; } +static int +g (int &&x) +{ + return x; +} + int main () { @@ -78,6 +84,12 @@ int test_const // = 3 = foo_rr_instance1.overloadConst (arg); + /* The statement below is illegal: cannot bind rvalue reference of + type 'int&&' to lvalue of type 'int'. + + result = g (i); */ + result = g (5); // this is OK + marker1 (); // marker1-returns-here return result; } diff --git a/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp b/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp index e92e901..cac3d4b 100644 --- a/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp +++ b/gdb/testsuite/gdb.cp/rvalue-ref-overload.exp @@ -66,3 +66,7 @@ setup_kfail "c++/15372" "*-*-*" gdb_test "print f (3)" "3" "rvalue reference overload" + +gdb_test "print g (i)" \ + "Cannot resolve function g to any overloaded instance" \ + "passing lvalue arg to rvalue parameter"