From patchwork Wed Jul 21 16:21:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1508349 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=vtbp1R3b; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GVLSl4p6Xz9sWl for ; Thu, 22 Jul 2021 02:21:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F467386FC00 for ; Wed, 21 Jul 2021 16:21:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2F467386FC00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1626884517; bh=1vvnF71SKXiwfT2c9lWostyYsNvMnJ4pPvwY9K0APHg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vtbp1R3bqGq9v+KO8Ebr2fEVynBImU89XRceqyfOM/eau/QMqH0hZaHceFf099/i0 nLxRkIxGrHXW0vnpIeTDbq+30sUlIIF1KW82LNYoGYIw70VAx+g7XH+4hqw03ij4wr c/WPdO9f+W9xNBuWZDj07aeHxJ6uFnIncqAbllAM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 645A2383F409 for ; Wed, 21 Jul 2021 16:21:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 645A2383F409 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-kSV_ek2vM7SS3nr94fmYYA-1; Wed, 21 Jul 2021 12:21:06 -0400 X-MC-Unique: kSV_ek2vM7SS3nr94fmYYA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1F59710086CC; Wed, 21 Jul 2021 16:21:05 +0000 (UTC) Received: from localhost (unknown [10.33.36.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF5BAE2C1; Wed, 21 Jul 2021 16:21:04 +0000 (UTC) Date: Wed, 21 Jul 2021 17:21:03 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make __gnu_cxx::sequence_buffer move-aware [PR101542] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The PR explains that Clang trunk now selects a different constructor when a non-const sequence_buffer is returned in a context where it qualifies as an implicitly-movable entity. Because lookup is first performed using an rvalue, the sequence_buffer(const sequence_buffer&) constructor gets chosen, which makes a copy instead of a "pseudo-move" via the sequence_buffer(sequence_buffer&) constructor. The problem isn't seen with GCC because as noted in the r11-2412 commit log, GCC actually implements a slightly modified rule that avoids breaking exactly this type of code. This patch adds a move constructor to sequence_buffer, so that implicit or explicit moves will have the same effect, calling the sequence_buffer(sequence_buffer&) constructor. A move assignment operator is also added to make move assignment work similarly. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/101542 * include/ext/rope (sequence_buffer): Add move constructor and move assignment operator. * testsuite/ext/rope/101542.cc: New test. Tested powerpc64le-linux. Committed to trunk. commit 8edb61420502c62fa2cccdd98876a9aa039b72a6 Author: Jonathan Wakely Date: Wed Jul 21 15:29:19 2021 libstdc++: Make __gnu_cxx::sequence_buffer move-aware [PR101542] The PR explains that Clang trunk now selects a different constructor when a non-const sequence_buffer is returned in a context where it qualifies as an implicitly-movable entity. Because lookup is first performed using an rvalue, the sequence_buffer(const sequence_buffer&) constructor gets chosen, which makes a copy instead of a "pseudo-move" via the sequence_buffer(sequence_buffer&) constructor. The problem isn't seen with GCC because as noted in the r11-2412 commit log, GCC actually implements a slightly modified rule that avoids breaking exactly this type of code. This patch adds a move constructor to sequence_buffer, so that implicit or explicit moves will have the same effect, calling the sequence_buffer(sequence_buffer&) constructor. A move assignment operator is also added to make move assignment work similarly. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/101542 * include/ext/rope (sequence_buffer): Add move constructor and move assignment operator. * testsuite/ext/rope/101542.cc: New test. diff --git a/libstdc++-v3/include/ext/rope b/libstdc++-v3/include/ext/rope index 81e4f23708f..9681dbc6225 100644 --- a/libstdc++-v3/include/ext/rope +++ b/libstdc++-v3/include/ext/rope @@ -203,6 +203,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer); } + // Non-const "copy" modifies the parameter - yuck sequence_buffer(sequence_buffer& __x) { __x.flush(); @@ -213,6 +214,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION sequence_buffer(_Sequence& __s) : _M_prefix(&__s), _M_buf_count(0) { } + // Non-const "copy" modifies the parameter - yuck sequence_buffer& operator=(sequence_buffer& __x) { @@ -230,7 +232,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::copy(__x._M_buffer, __x._M_buffer + __x._M_buf_count, _M_buffer); return *this; } - + +#if __cplusplus >= 201103L + sequence_buffer(sequence_buffer&& __x) : sequence_buffer(__x) { } + sequence_buffer& operator=(sequence_buffer&& __x) { return *this = __x; } +#endif + void push_back(value_type __x) { diff --git a/libstdc++-v3/testsuite/ext/rope/101542.cc b/libstdc++-v3/testsuite/ext/rope/101542.cc new file mode 100644 index 00000000000..e89f23d3d48 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/rope/101542.cc @@ -0,0 +1,27 @@ +// { dg-do run { target c++11 } } +// PR libstdc++/101542 +#include +#include + +template T f(T x) { return x; } +template T g(T x) { return std::move(x); } + +int main() +{ + std::string s; + { + __gnu_cxx::sequence_buffer a(s); + { + __gnu_cxx::sequence_buffer b = std::move(a); + b.push_back('h'); + b.push_back('e'); + b.push_back('l'); + b.push_back('l'); + b.push_back('o'); + + __gnu_cxx::sequence_buffer c; + c = f(g((std::move(b)))); + } + } + VERIFY( s == "hello" ); +}