From patchwork Tue Mar 7 20:13:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1753599 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.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=Rbix7nPk; 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 ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PWRVM6vpbz2469 for ; Wed, 8 Mar 2023 07:14:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82F103855580 for ; Tue, 7 Mar 2023 20:14:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82F103855580 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678220040; bh=3ugdUM1NNJi9/do6IqvZ49MN/fC208KvlT7v0buXdYU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Rbix7nPkQfipLMc1sEv1fZzdSFji3y9wrdM6BtDpKDA05V2o/DsA+rKQ3pD0fO6eA LQjlygqlyrZOJSQ3uO1aclqPpto/Op6q0RE9jTjrWjuCvEpA2kUQ5bn+jINuStePpv A/rISVZEgLX/dqp47zNPDb2o8rUBE7Ezy5ZqSG4g= 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 807BA3858C30 for ; Tue, 7 Mar 2023 20:13:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 807BA3858C30 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-427-i0lCyxvnODmBz-fi-kS8_g-1; Tue, 07 Mar 2023 15:13:40 -0500 X-MC-Unique: i0lCyxvnODmBz-fi-kS8_g-1 Received: by mail-qv1-f70.google.com with SMTP id y6-20020ad457c6000000b00535261af1b1so8148349qvx.13 for ; Tue, 07 Mar 2023 12:13:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678220019; 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=3ugdUM1NNJi9/do6IqvZ49MN/fC208KvlT7v0buXdYU=; b=dlowpUAR/E/qboPB4MDj5Nx5F71lfCtf9lVluhHr9nxkJ+IJMF8yIbKZ2oViJeDAgq 9QrqIVdUt6dfusaX/7KKuN8XCfGfSerKUeNsVKspgstDo4Tfst1YILdOptKtpWoxU5MG 1tc8eiPiWJZLMnk1goT3UoS9BQir7MRiuPRDPkUH7s4ogZB+C4STjHlySNS1ZWNGXxkf cTQYnwJe7CF3aZRT+dv85pchoEVB18+MxTYoBJBodu0oSuD2b2Ti5LVYbuDpEoO6PiVO iQMI7Oo4FTvSsNm8/Jd6XW4Bz5oXZxRHOBc7fQCEiYwsZEhP+8wxWfHWr4rigc8C1aSk yprw== X-Gm-Message-State: AO0yUKU1lFaoqO/ZLi1FG2lq8E+hKMqqWclSYf5menlqA0OzAVqBOAKD LqNeFhmcClrvUW17wrxO/FdmEB6tF8esDWLnU7/t7XjIZwQ6bYDGHuARRLykNGEAxOIXw8d8dzg LExzFI9i/QH8uL0bFXVG3Fdr3D6QQVJgeZiH5/qSfsNOV/euIingo7kO+882cWJjOxGw6RqVxoE w= X-Received: by 2002:a05:622a:15c1:b0:3bf:cf66:f169 with SMTP id d1-20020a05622a15c100b003bfcf66f169mr5661068qty.40.1678220019115; Tue, 07 Mar 2023 12:13:39 -0800 (PST) X-Google-Smtp-Source: AK7set+9ywTEo5+IDvIrBJsWUHxYfedxSZ1SvUkoUuERr1kBwgB4xCYwbFndQJYGRDpzON1ur7uEfA== X-Received: by 2002:a05:622a:15c1:b0:3bf:cf66:f169 with SMTP id d1-20020a05622a15c100b003bfcf66f169mr5661029qty.40.1678220018680; Tue, 07 Mar 2023 12:13:38 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id s4-20020a372c04000000b0074283b87a4esm9999130qkh.90.2023.03.07.12.13.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 12:13:38 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: extraneous begin in cartesian_product_view::end [PR107572] Date: Tue, 7 Mar 2023 15:13:35 -0500 Message-Id: <20230307201335.14969-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.rc0.57.g454dfcbddf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" ranges::begin() isn't guaranteed to be equality-preserving for non-forward ranges, so in cartesian_product_view::end we need to be careful about calling begin() on the first range (which could be non-forward) in the (non-degenerate) case where __empty_tail is false. Since we're already using a variadic lambda to compute __empty_tail, we might as well use that same lambda to build up the tuple of iterators instead of doing it via __tuple_transform. Tested on x86_64-pc-linux-gnu, does this look OK for trunk? PR libstdc++/107572 libstdc++-v3/ChangeLog: * include/std/ranges (cartesian_product_view::end): When building the tuple of iterators, avoid calling ranges::begin on the first range if __empty_tail is false. * testsuite/std/ranges/cartesian_product/1.cc (test07): New test. --- libstdc++-v3/include/std/ranges | 36 +++++++++++++------ .../std/ranges/cartesian_product/1.cc | 22 ++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index e0cac15a64f..0de7bdef504 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -8078,26 +8078,42 @@ namespace views::__adaptor end() requires ((!__detail::__simple_view<_First> || ... || !__detail::__simple_view<_Vs>) && __detail::__cartesian_product_is_common<_First, _Vs...>) { - bool __empty_tail = [this](index_sequence<_Is...>) { - return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...); + auto __it = [this](index_sequence<_Is...>) { + bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...); + auto& __first = std::get<0>(_M_bases); + auto __first_it = __empty_tail + ? ranges::begin(__first) + : __detail::__cartesian_common_arg_end(__first); + // N.B. When implementing P2165R4 this should be changed to always return tuple. + if constexpr (sizeof...(_Is) == 1) + return std::make_pair(std::move(__first_it), + ranges::begin(std::get<1 + _Is>(_M_bases))...); + else + return std::make_tuple(std::move(__first_it), + ranges::begin(std::get<1 + _Is>(_M_bases))...); }(make_index_sequence{}); - auto __it = __detail::__tuple_transform(ranges::begin, _M_bases); - if (!__empty_tail) - std::get<0>(__it) = __detail::__cartesian_common_arg_end(std::get<0>(_M_bases)); return _Iterator{*this, std::move(__it)}; } constexpr _Iterator end() const requires __detail::__cartesian_product_is_common { - bool __empty_tail = [this](index_sequence<_Is...>) { - return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...); + auto __it = [this](index_sequence<_Is...>) { + bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...); + auto& __first = std::get<0>(_M_bases); + auto __first_it = __empty_tail + ? ranges::begin(__first) + : __detail::__cartesian_common_arg_end(__first); + // N.B. When implementing P2165R4 this should be changed to always return tuple. + if constexpr (sizeof...(_Is) == 1) + return std::make_pair(std::move(__first_it), + ranges::begin(std::get<1 + _Is>(_M_bases))...); + else + return std::make_tuple(std::move(__first_it), + ranges::begin(std::get<1 + _Is>(_M_bases))...); }(make_index_sequence{}); - auto __it = __detail::__tuple_transform(ranges::begin, _M_bases); - if (!__empty_tail) - std::get<0>(__it) = __detail::__cartesian_common_arg_end(std::get<0>(_M_bases)); return _Iterator{*this, std::move(__it)}; } diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc index 1ec4422e6f3..34639f514aa 100644 --- a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc +++ b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -178,6 +179,26 @@ test06() return true; } +void +test07() +{ + // PR libstdc++/107572 + static std::istringstream ints("0 1 2 3 4"); + struct istream_range + { + auto begin() { return std::istream_iterator{ints}; } + auto end() { return std::istream_iterator{}; } + }; + istream_range r; + int i = 0; + for (auto [v] : views::cartesian_product(r)) + { + VERIFY( v == i ); + ++i; + }; + VERIFY( i == 5 ); +} + int main() { @@ -187,4 +208,5 @@ main() test04(); test05(); static_assert(test06()); + test07(); }