From patchwork Thu Jul 25 00:04:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1964576 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ALOgbw+i; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WTrjp5Dnnz1ybY for ; Thu, 25 Jul 2024 10:05:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C65F33858283 for ; Thu, 25 Jul 2024 00:05:00 +0000 (GMT) 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 657F63858C35 for ; Thu, 25 Jul 2024 00:04:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 657F63858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 657F63858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721865881; cv=none; b=Z9WZqW358n7FeU5la5MtgHFTBFYPavD0Q4+txKCw0yhapMNZcHfRZFYK7lk5DuvAhQN/8MD12ABzplcMU7Qaoa1zkaiwuSgl80gguMcOjsd9+LfQeIWDeQPrR/SSPEYYGhaaA3dmsL4hWVjrk+HX7Tyu4sK7MSWM/PruTkLx1gc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721865881; c=relaxed/simple; bh=xXYkrMH1iaIO8jztPrZqmWiwq6e+GBQMJmettUb2w2Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=i6CLUM5PLoMLKKLNhsA2GRtrM/ltlzyXLilpKMUehE46s559ThW+gfBaCybakJ0kXIfmlmDu/+HpjhUDElyLGrLgbSaA+qcCCZhO8E7A/GtxDa6FD/+hyjFG1YyBFoaLaIy4qp0Uh9TC7zg73PVOhk0v2lEL4Nx4G9WEuJvv564= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721865879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZOC4kKyOqTPgZeHT9qRs0muEHGhiccLYl016zrbMZtI=; b=ALOgbw+iSArW7ImCYOyhaOJcrMkfWuKbSVT90+0nQMWWePxOz05qTA7E6xHAaFjkw3l6r6 j9cQCETs0V3mzmTPkw9mhDUy6eD9wolGWrYotKhhxG73ArNvd1KDwDQRO9FK4IMWoHiEwO ElJ/qf5txE6uQbBG3cvhYGoCk02NdZw= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-442-m8EyEaLmMtq6MZ2zxQY8MA-1; Wed, 24 Jul 2024 20:04:37 -0400 X-MC-Unique: m8EyEaLmMtq6MZ2zxQY8MA-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7a1633381ebso33858285a.0 for ; Wed, 24 Jul 2024 17:04:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721865876; x=1722470676; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZOC4kKyOqTPgZeHT9qRs0muEHGhiccLYl016zrbMZtI=; b=Uw+9tK3AgoXKWckCqIhxsKO7PxzxsisbLzgydaMUn4Y2pbdchfg2+t/g8Z8ZaKuttD EWYm1ZhJztkamUUkoN9JchVhZ0nS/X3emma96T3MElKh1OJgOTb6tXF8O6bU6TVFR4xq bg1tvDxg8CNS5SJ6xJIxRvCJYub2SHnfRFMjUT5IYd3PjTWKnZjTnQmDV35wk561TLFW wsxMRHHFf3sb3yzN2kc6I5UsG/hF/+qb8z6x1Tj9nJbni6ORfQNwsQ3FBz9X4W2WMGPz 7Hb3Q+5iTIM3QlRPndU0QHdYVJloHBIU+pCw+VikIZMWnCEb1EQrEWLSP37pFFa8ZfIM sZWg== X-Gm-Message-State: AOJu0YyLxoma1x1SCO4Ja2U33NQfS8EJNye+/rOW3uufGtNX5fXKkr5C XV43x75xEiSV2Q1hMDgEHf53YZmBKZlPMhnCF1H5mYOgXQlZFGP77JFFnqC0Jygz6bv5Qz2/eBp eDdkIcnL9q0tdQ9EBvKyTAEgN78BoBXaMO2hG5XOvwLkz+IQnN+8HPad4eFF+/FNGkv6i5QNFee mLl3ViNDeO3l67yP99dG4gcfhSzl8zpjwslEvs X-Received: by 2002:a05:620a:2951:b0:79f:137b:6e75 with SMTP id af79cd13be357-7a1d7f7a40amr24145585a.67.1721865876256; Wed, 24 Jul 2024 17:04:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHURYkE809gAlowTqFY3fQM3zcNIz2TjjeJVR+izdOIFNYiBY+cRopIIrpkR0J+Lrr5Wa4tzw== X-Received: by 2002:a05:620a:2951:b0:79f:137b:6e75 with SMTP id af79cd13be357-7a1d7f7a40amr24141985a.67.1721865875822; Wed, 24 Jul 2024 17:04:35 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1d73e87f5sm16162385a.64.2024.07.24.17.04.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 17:04:35 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: fix uses of explicit object parameter [PR116038] Date: Wed, 24 Jul 2024 20:04:33 -0400 Message-ID: <20240725000433.3732987-1-ppalka@redhat.com> X-Mailer: git-send-email 2.46.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested on x86_64-pc-linux-gnu, does this look OK for trunk/14 (after 14.2 is released)? -- >8 -- The type of an implicit object parameter is always the current class. For an explicit object parameter however, its deduced type can be a class derived from the current class. So when combining multiple implicit-object overloads into a single explicit-object overload we need to account for this possibility. For example, when accessing a member from the current class through an explicit object parameter, it may be a derived class from which the member is not accessible, as in the below testcases. This pitfall is discussed[1] in the deducing this paper. The general solution is to cast the explicit object parameter to (a reference to) the current class, appropriately qualified, rather than e.g. using std::forward which preserves the deduced type. This patch corrects the existing problematic uses of explicit object parameters in the library, all of which forward the parameter via std::forward, to instead cast the parameter to the current class via our __like_t alias template. Note that unlike the paper's like_t, ours always returns a reference so when can just write __like_t(self) instead of (_like_t&&)self as the paper does. [1]: https://wg21.link/P0847#name-lookup-within-member-functions and the section after that PR libstdc++/116038 libstdc++-v3/ChangeLog: * include/std/functional (_Bind_front::operator()): Use __like_t instead of std::forward when forwarding __self. (_Bind_back::operator()): Likewise. * include/std/ranges (_Partial::operator()): Likewise. (_Pipe::operator()): Likewise. * testsuite/20_util/function_objects/bind_back/116038.cc: New test. * testsuite/20_util/function_objects/bind_front/116038.cc: New test. * testsuite/std/ranges/adaptors/116038.cc: New test. --- libstdc++-v3/include/std/functional | 4 +-- libstdc++-v3/include/std/ranges | 11 ++++--- .../function_objects/bind_back/116038.cc | 27 +++++++++++++++++ .../function_objects/bind_front/116038.cc | 27 +++++++++++++++++ .../testsuite/std/ranges/adaptors/116038.cc | 29 +++++++++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/function_objects/bind_back/116038.cc create mode 100644 libstdc++-v3/testsuite/20_util/function_objects/bind_front/116038.cc create mode 100644 libstdc++-v3/testsuite/std/ranges/adaptors/116038.cc diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 99364286a72..7788a963757 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -944,7 +944,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(is_nothrow_invocable_v<__like_t<_Self, _Fd>, __like_t<_Self, _BoundArgs>..., _CallArgs...>) { - return _S_call(std::forward<_Self>(__self), _BoundIndices(), + return _S_call(__like_t<_Self, _Bind_front>(__self), _BoundIndices(), std::forward<_CallArgs>(__call_args)...); } #else @@ -1072,7 +1072,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(is_nothrow_invocable_v<__like_t<_Self, _Fd>, _CallArgs..., __like_t<_Self, _BoundArgs>...>) { - return _S_call(std::forward<_Self>(__self), _BoundIndices(), + return _S_call(__like_t<_Self, _Bind_back>(__self), _BoundIndices(), std::forward<_CallArgs>(__call_args)...); } diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 3f335b95a08..b7c7aa36ddc 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -1033,7 +1033,7 @@ namespace views::__adaptor return _Adaptor{}(std::forward<_Range>(__r), std::forward(__args)...); }; - return std::apply(__forwarder, std::forward<_Self>(__self)._M_args); + return std::apply(__forwarder, __like_t<_Self, _Partial>(__self)._M_args); } #else template @@ -1082,7 +1082,10 @@ namespace views::__adaptor requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Arg>> constexpr auto operator()(this _Self&& __self, _Range&& __r) - { return _Adaptor{}(std::forward<_Range>(__r), std::forward<_Self>(__self)._M_arg); } + { + return _Adaptor{}(std::forward<_Range>(__r), + __like_t<_Self, _Partial>(__self)._M_arg); + } #else template requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> @@ -1185,8 +1188,8 @@ namespace views::__adaptor constexpr auto operator()(this _Self&& __self, _Range&& __r) { - return (std::forward<_Self>(__self)._M_rhs - (std::forward<_Self>(__self)._M_lhs + return (__like_t<_Self, _Pipe>(__self)._M_rhs + (__like_t<_Self, _Pipe>(__self)._M_lhs (std::forward<_Range>(__r)))); } #else diff --git a/libstdc++-v3/testsuite/20_util/function_objects/bind_back/116038.cc b/libstdc++-v3/testsuite/20_util/function_objects/bind_back/116038.cc new file mode 100644 index 00000000000..ed392b1434e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/bind_back/116038.cc @@ -0,0 +1,27 @@ +// PR libstdc++/116038 +// { dg-do compile { target c++23 } } + +#include +#include + +struct A { }; +struct B { }; + +template +struct overloaded : private Ts... { + overloaded(Ts...); + using Ts::operator()...; +}; + +int apply_a(A, int); +int apply_b(B, int); + +int main() { + overloaded o = { std::bind_back(apply_a, 1), + std::bind_back(apply_b, 2) }; + A a; + o(a); + std::as_const(o)(a); + std::move(o)(a); + std::move(std::as_const(o))(a); +} diff --git a/libstdc++-v3/testsuite/20_util/function_objects/bind_front/116038.cc b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/116038.cc new file mode 100644 index 00000000000..3bf1226375b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/116038.cc @@ -0,0 +1,27 @@ +// PR libstdc++/116038 +// { dg-do compile { target c++20 } } + +#include +#include + +struct A { }; +struct B { }; + +template +struct overloaded : private Ts... { + overloaded(Ts...); + using Ts::operator()...; +}; + +int apply_a(int, A); +int apply_b(int, B); + +int main() { + overloaded o = { std::bind_front(apply_a, 1), + std::bind_front(apply_b, 2) }; + A a; + o(a); + std::as_const(o)(a); + std::move(o)(a); + std::move(std::as_const(o))(a); +} diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/116038.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/116038.cc new file mode 100644 index 00000000000..1afdf3d06d1 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/116038.cc @@ -0,0 +1,29 @@ +// PR libstdc++/116038 +// { dg-do compile { target c++20 } } + +#include +#include + +struct A { }; +struct B { }; + +template +struct overloaded : private Ts... { + overloaded(Ts...); + using Ts::operator()...; +}; + +int x[5]; +struct integralish { operator int() const; } i; + +int main() { + overloaded o1 = { std::views::drop(i) }; + o1(x); + std::move(o1)(x); + std::as_const(o1)(x); + + overloaded o2 = { std::views::drop(i) | std::views::take(i) }; + o2(x); + std::move(o2)(x); + std::as_const(o1)(x); +}