From patchwork Wed May 22 09:02:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1937720 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=VPeXrYuE; 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 4Vklhy72Xcz20PS for ; Wed, 22 May 2024 19:03:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC1CF38654B4 for ; Wed, 22 May 2024 09:03:43 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 09A1B3865487 for ; Wed, 22 May 2024 09:03:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 09A1B3865487 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 09A1B3865487 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716368603; cv=none; b=Dw3kfp5j3RkQVhONy9Sj1M5g7kAqgodevDe9FXYeyg/YmWvBmzkQHm25HnW9pEixuLcI4o22UKcXjd80lXwC/BKfH1DrngkEH0LPJ65GlfL1Y+gK9z6UFo+jue8BYRocwT6riGOEODMNNcDJErlFRUx5Nb5X52MI7D59xXGo7pg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716368603; c=relaxed/simple; bh=i1paeXuwqWCA7ebjChywP+KRsiR1XX0NHbsDWyzlqZs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=a6bF10u6cHgx8AA2QKbxiX/QwU8/1GWCB0DRTVxesaesmPfp/iD2x1hpzfxEoDYHghwx2EeUJg+lo0uZH+B31OfVPz+wy6xpwekyNXG3pHVvUMcEGmRuh3KIoRklKkUssn1l2zWNhpTM8lBHu6tF2uB/M5khopEbEeKDdHK2wBQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716368599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=eqwE2Gx5UMS/5UzyR6g+xUr9AzfKGutkbSmnwu0AXKw=; b=VPeXrYuEVqeKIDOVpfWXhVVuWxYZzhPk84ZtL6mWieZX1Ulg32z07g8BySA4fdavTHyOwp pHzXpo3YVBjR52kbIAqBTqdSjUdiTpeo6blFN7Sgtw8ppg+ZSSoK+L6ah7x6lVPB9WGYKn O8y25PqPQO9aEP/eHZ7mcg7qT9IX8lE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-bdMXutJ9OcOXZbuZj4aMEg-1; Wed, 22 May 2024 05:03:17 -0400 X-MC-Unique: bdMXutJ9OcOXZbuZj4aMEg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 80E52101A52C; Wed, 22 May 2024 09:03:17 +0000 (UTC) Received: from localhost (unknown [10.42.28.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 326CAC15BB1; Wed, 22 May 2024 09:03:17 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Ensure std::variant relops convert to bool [PR115145] Date: Wed, 22 May 2024 10:02:58 +0100 Message-ID: <20240522090311.173846-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham 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 x86_64-linux. Pushed to trunk. -- >8 -- Ensure that the result of comparing the variant alternatives is converted to bool immediately rather than copied. libstdc++-v3/ChangeLog: PR libstdc++/115145 * include/std/variant (operator==, operator!=, operator<) (operator<=, operator>, operator>=): Add trailing-return-type to lambda expressions to trigger conversion to bool. * testsuite/20_util/variant/relops/115145.cc: New test. --- libstdc++-v3/include/std/variant | 63 ++++++++++--------- .../20_util/variant/relops/115145.cc | 36 +++++++++++ 2 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/variant/relops/115145.cc diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index cfb4bcdbcc9..371cbb90f54 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1271,10 +1271,11 @@ namespace __detail::__variant operator== [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l == __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l == __r; + }); } template @@ -1286,10 +1287,11 @@ namespace __detail::__variant operator!= [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l != __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l != __r; + }); } template @@ -1301,10 +1303,11 @@ namespace __detail::__variant operator< [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l < __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l < __r; + }); } template @@ -1316,10 +1319,11 @@ namespace __detail::__variant operator<= [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l <= __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l <= __r; + }); } template @@ -1331,10 +1335,11 @@ namespace __detail::__variant operator> [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l > __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l > __r; + }); } template @@ -1346,10 +1351,11 @@ namespace __detail::__variant operator>= [[nodiscard]] (const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) { - return __detail::__variant::__compare(true, __lhs, __rhs, - [](auto&& __l, auto&& __r) { - return __l >= __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) -> bool { + return __l >= __r; + }); } constexpr bool operator==(monostate, monostate) noexcept { return true; } @@ -1363,10 +1369,11 @@ namespace __detail::__variant { common_comparison_category_t...> __ret = strong_ordering::equal; - return __detail::__variant::__compare(__ret, __v, __w, - [](auto&& __l, auto&& __r) { - return __l <=> __r; - }); + namespace __variant = __detail::__variant; + return __variant::__compare(__ret, __v, __w, + [](auto&& __l, auto&& __r) { + return __l <=> __r; + }); } constexpr strong_ordering diff --git a/libstdc++-v3/testsuite/20_util/variant/relops/115145.cc b/libstdc++-v3/testsuite/20_util/variant/relops/115145.cc new file mode 100644 index 00000000000..f3443cbbbcc --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/relops/115145.cc @@ -0,0 +1,36 @@ +// { dg-do compile { target c++17 } } +// PR libstdc++/115145 +// lambda in rewritten std::variant comparisons does not specify return type + +#include + +struct Bool { + operator bool() & { return val; } + const bool val; +}; + +Bool t{true}, f{false}; + +struct A { + Bool& operator==(const A&) const { return t; } + Bool& operator!=(const A&) const { return f; } + Bool& operator<(const A&) const { return f; } + Bool& operator>(const A&) const { return f; } + Bool& operator<=(const A&) const { return t; } + Bool& operator>=(const A&) const { return t; } +}; + +bool check_bool(bool); +template void check_bool(T) = delete; + +void +test_pr115145() +{ + std::variant v; + check_bool( v == v ); + check_bool( !(v != v) ); + check_bool( !(v < v) ); + check_bool( !(v > v) ); + check_bool( v <= v ); + check_bool( v >= v ); +}