From patchwork Thu Nov 30 15:50:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 81042 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0372738393B9 for ; Thu, 30 Nov 2023 15:52:36 +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 ESMTPS id BD7DA384DEF1 for ; Thu, 30 Nov 2023 15:52:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD7DA384DEF1 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 BD7DA384DEF1 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=1701359521; cv=none; b=n7T7bUxC8lBuUz/Y9N7rURIAPi9tKEVTrXctKunnP+dHQytAOGWmUBNCbKVJjEJZT6bnEy13jOEsnixwSFUsok5K6324Aw8AH3ZXri0zjxh2HTR6ki7jCnG3jRsa1n6Szc/kYGBrru6TFo2Oh3fUB/QfRKKCf0I1dNOHq7Jn2VM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701359521; c=relaxed/simple; bh=PJCMZ5DJWwK1DqilmV7hkgcP7n+ZDJXVP+ezHObombs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LRr34Q+m4SjAFR3GsZ/9FGqa5f1Ejhd2xWIeRSqgox/3bluYCVIX6KFy9i4B74x2O2Lkl6ZRGXIGVBlNo9VJY2X5EOKSG43uv/wO8t+O40Vf/qoNj4iEkGoKw4aqVTr+n63HGxKikMjH4foqMKU3mKVe+9DyfjeQWoEfJn7CptE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701359520; 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=qi3n71mjbY0CVsTKNqqbzf4uf/A/LuqJ1Sp6oQfZ+lo=; b=SKrzpB5LfNsfW72/DV8BCGRUgru8bA0PgiIRKAnJwpWnuZ+SOEOZDr8mWHeQRPhx6Tsuzb H95rTh+x1fFMZqJdCmHL+ZjczlOG5V+g5QoSpZmDpOyMoF/+8zUbQqgqgjWzXUoIWUCerH R4ALHYSQl6vynA6WPI9k15FPYsHscew= 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-452-EdV4TqR0MGut-gJ6-E0dAA-1; Thu, 30 Nov 2023 10:51:57 -0500 X-MC-Unique: EdV4TqR0MGut-gJ6-E0dAA-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 B7E17185A793; Thu, 30 Nov 2023 15:51:56 +0000 (UTC) Received: from localhost (unknown [10.42.28.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8001DC15885; Thu, 30 Nov 2023 15:51:56 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Add workaround to std::ranges::subrange [PR111948] Date: Thu, 30 Nov 2023 15:50:50 +0000 Message-ID: <20231130155155.74430-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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, 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+patchwork=sourceware.org@gcc.gnu.org I think I'll push this to work around the compiler bug. We can revert it later if the front end gets fixed. Tested x86_64-linux. Needed on trunk and gcc-13. -- >8 -- libstdc++-v3/ChangeLog: PR libstdc++/111948 * include/bits/ranges_util.h (subrange): Add constructor to _Size to aoid setting member in constructor. * testsuite/std/ranges/subrange/111948.cc: New test. --- libstdc++-v3/include/bits/ranges_util.h | 21 ++++++++++++------- .../testsuite/std/ranges/subrange/111948.cc | 8 +++++++ 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/111948.cc diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h index ab6c69c57d0..185e46ec7a9 100644 --- a/libstdc++-v3/include/bits/ranges_util.h +++ b/libstdc++-v3/include/bits/ranges_util.h @@ -267,13 +267,21 @@ namespace ranges using __size_type = __detail::__make_unsigned_like_t>; - template + template struct _Size - { }; + { + [[__gnu__::__always_inline__]] + constexpr _Size(_Tp = {}) { } + }; template struct _Size<_Tp, true> - { _Tp _M_size; }; + { + [[__gnu__::__always_inline__]] + constexpr _Size(_Tp __s = {}) : _M_size(__s) { } + + _Tp _M_size; + }; [[no_unique_address]] _Size<__size_type> _M_size = {}; @@ -294,11 +302,8 @@ namespace ranges noexcept(is_nothrow_constructible_v<_It, decltype(__i)> && is_nothrow_constructible_v<_Sent, _Sent&>) requires (_Kind == subrange_kind::sized) - : _M_begin(std::move(__i)), _M_end(__s) - { - if constexpr (_S_store_size) - _M_size._M_size = __n; - } + : _M_begin(std::move(__i)), _M_end(__s), _M_size(__n) + { } template<__detail::__different_from _Rng> requires borrowed_range<_Rng> diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc new file mode 100644 index 00000000000..dcc64b56def --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc @@ -0,0 +1,8 @@ +// { dg-do compile { target c++20 } } + +#include + +// Bug libstdc++/111948 - subrange modifies a const size object + +constexpr auto r = std::ranges::subrange(std::views::iota(0), 5); +static_assert(std::ranges::distance(r));