{"id":2231568,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2231568/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260430210040.2360840-1-jwakely@redhat.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<20260430210040.2360840-1-jwakely@redhat.com>","date":"2026-04-30T21:00:04","name":"libstdc++: Do not assume URBG::result_type exists [PR121919]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e7e8d655a0e0d088ea7cbcfe3810cf8b60871dbc","submitter":{"id":48004,"url":"http://patchwork.ozlabs.org/api/1.1/people/48004/?format=json","name":"Jonathan Wakely","email":"jwakely@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260430210040.2360840-1-jwakely@redhat.com/mbox/","series":[{"id":502385,"url":"http://patchwork.ozlabs.org/api/1.1/series/502385/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502385","date":"2026-04-30T21:00:04","name":"libstdc++: Do not assume URBG::result_type exists [PR121919]","version":1,"mbox":"http://patchwork.ozlabs.org/series/502385/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2231568/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2231568/checks/","tags":{},"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=hfbEb5m+;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=hfbEb5m+","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g666H5XD5z1yHZ\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 01 May 2026 07:01:27 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id EC1084310D63\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 21:01:25 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id CA9E54099F9B\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 21:00:48 +0000 (GMT)","from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-107-0lrR1zeBN1OTEIEM5fBeJw-1; Thu,\n 30 Apr 2026 17:00:44 -0400","from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id CE7DB1956054; Thu, 30 Apr 2026 21:00:42 +0000 (UTC)","from zen.kayari.org (unknown [10.44.50.237])\n by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id C67381800480; Thu, 30 Apr 2026 21:00:41 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org EC1084310D63","OpenDKIM Filter v2.11.0 sourceware.org CA9E54099F9B"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org CA9E54099F9B","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org CA9E54099F9B","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777582848; cv=none;\n b=DI1XEoqeCH5nksbrcSNQeW0AInUMkk7jbPXpLewSe6yCBcRfS71j53hjXQI43yH4rFKldDIFH+3LEmSITi73p3bbQo4TI5cfdxFtZHDIJf6c+Hnp9Trq2Xm05n2XsS5dGVjcSxgonCESSUW7F3KAj9xd0gOLx4aJWti57uzZbQc=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777582848; c=relaxed/simple;\n bh=7BSPsk2DrdYk0pfmzUyefwfctOPwoqIaFHDibEHpjIA=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=na94+y27OM7VSSVoae/fSMTTYxVWwttowVEo4Bm2Htesmyv/2eq2YdbfUdUaL4JweAlZREGljFNEkzfNTlD32LEtwHGdZcYunAxKLupYCcVtMEszGBHE5XBd+FsSGJ1ALI6AdAEwLfuOOtLRBx+9m08RxDo54D2iTZ75i2wqaL8=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777582848;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding;\n bh=60oGoW1CW9WuU0TEXzYmd/DfAa6uyNI01rRaMFquBSk=;\n b=hfbEb5m+LVat2dXUEzAkpqgAgnNuGtymlVNED8E152QNxj5LoYeF/xeO/Y6/aFOOuyN2qC\n tmym3l+QcZKwX09BzI8wniXb/E3VRz4aj0oo5LrwRciuBeSMCQjNhy7N9Gn7jCPBOA7rnj\n iziBSi+akTuJdUD0FS45nSm8x4HZ8wU=","X-MC-Unique":"0lrR1zeBN1OTEIEM5fBeJw-1","X-Mimecast-MFC-AGG-ID":"0lrR1zeBN1OTEIEM5fBeJw_1777582843","From":"Jonathan Wakely <jwakely@redhat.com>","To":"gcc-patches@gcc.gnu.org,\n\tlibstdc++@gcc.gnu.org","Subject":"[PATCH] libstdc++: Do not assume URBG::result_type exists [PR121919]","Date":"Thu, 30 Apr 2026 22:00:04 +0100","Message-ID":"<20260430210040.2360840-1-jwakely@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.93","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"M-4XV29mJBvyBTsalk56JkqKjW2QIGpN5uyxKqGr9NY_1777582843","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","Content-Transfer-Encoding":"8bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"The ranges::sample and ranges::shuffle algorithms are supposed to work\nwith types which model std::uniform_random_bit_generator, which means\nthey should not assume that G::result_type is present. That isn't needed\nto satisfy the concept. Change the algorithms to use decltype(__g())\ninstead of using result_type.\n\nThis isn't sufficient to fix the bug though, because those algorithms\nuse std::uniform_int_distribution and that class template's operator()\noverloads depend on the more restrictive uniform random bit generator\nrequirements, which do include the presence of a nested result_type\nmember.\n\nWe need to change std::uniform_int_distribution to also use decltype\ninstead of the nested result_type, even though the standard says that\nstd::uniform_int_distribution is allowed to assume that result_type\nexists.\n\nThere's yet another problem, which is that a type that returns random\nbool values can model the concept, but doesn't meet the named\nrequirements and can't be used with std::uniform_int_distribution. That\nisn't addressed by this change.\n\nlibstdc++-v3/ChangeLog:\n\n\tPR libstdc++/121919\n\t* include/bits/ranges_algo.h (__sample_fn, __shuffle_fn): Use\n\tdecltype(__g()) instead of remove_reference_t<_G>::result_type.\n\t* include/bits/uniform_int_dist.h\n\t(uniform_int_distribution::operator()): Use decltype(__urng())\n\tinstead of _UniformRandomBitGenerator::result_type\n\t(uniform_int_distribution::__generate_impl): Likewise.\n\t* testsuite/25_algorithms/sample/121919.cc: New test.\n\t* testsuite/25_algorithms/shuffle/121919.cc: New test.\n---\n\nTested x86_64-linux.\n\nI'm opening an LWG issue about this topic, because this is a silly\nsituation we've created.\n\n libstdc++-v3/include/bits/ranges_algo.h       |  7 ++---\n libstdc++-v3/include/bits/uniform_int_dist.h  |  4 +--\n .../testsuite/25_algorithms/sample/121919.cc  | 28 +++++++++++++++++++\n .../testsuite/25_algorithms/shuffle/121919.cc | 25 +++++++++++++++++\n 4 files changed, 57 insertions(+), 7 deletions(-)\n create mode 100644 libstdc++-v3/testsuite/25_algorithms/sample/121919.cc\n create mode 100644 libstdc++-v3/testsuite/25_algorithms/shuffle/121919.cc","diff":"diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h\nindex 0d1928d52a2f..4330d3e70b84 100644\n--- a/libstdc++-v3/include/bits/ranges_algo.h\n+++ b/libstdc++-v3/include/bits/ranges_algo.h\n@@ -1850,8 +1850,7 @@ namespace ranges\n \t    using __distrib_type = uniform_int_distribution<_Size>;\n \t    using __param_type = typename __distrib_type::param_type;\n \t    using _USize = __detail::__make_unsigned_like_t<_Size>;\n-\t    using __uc_type\n-\t      = common_type_t<typename remove_reference_t<_Gen>::result_type, _USize>;\n+\t    using __uc_type = common_type_t<decltype(__g()), _USize>;\n \n \t    if (__first == __last)\n \t      return __out;\n@@ -1964,9 +1963,7 @@ namespace ranges\n \tusing __ud_type = __detail::__make_unsigned_like_t<_DistanceType>;\n \tusing __distr_type = std::uniform_int_distribution<__ud_type>;\n \tusing __p_type = typename __distr_type::param_type;\n-\n-\tusing __uc_type\n-\t  = common_type_t<typename remove_reference_t<_Gen>::result_type, __ud_type>;\n+\tusing __uc_type = common_type_t<decltype(__g()), __ud_type>;\n \n \tif constexpr (sized_sentinel_for<_Sent, _Iter>)\n \t  {\ndiff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h\nindex dcf763ae7649..9c5514c7c138 100644\n--- a/libstdc++-v3/include/bits/uniform_int_dist.h\n+++ b/libstdc++-v3/include/bits/uniform_int_dist.h\n@@ -288,7 +288,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n       operator()(_UniformRandomBitGenerator& __urng,\n \t\t const param_type& __param)\n       {\n-\ttypedef typename _UniformRandomBitGenerator::result_type _Gresult_type;\n+\ttypedef decltype(__urng()) _Gresult_type;\n \ttypedef typename make_unsigned<result_type>::type __utype;\n \ttypedef typename common_type<_Gresult_type, __utype>::type __uctype;\n \n@@ -386,7 +386,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION\n \t\t      const param_type& __param)\n       {\n \t__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)\n-\ttypedef typename _UniformRandomBitGenerator::result_type _Gresult_type;\n+\ttypedef decltype(__urng()) _Gresult_type;\n \ttypedef typename make_unsigned<result_type>::type __utype;\n \ttypedef typename common_type<_Gresult_type, __utype>::type __uctype;\n \ndiff --git a/libstdc++-v3/testsuite/25_algorithms/sample/121919.cc b/libstdc++-v3/testsuite/25_algorithms/sample/121919.cc\nnew file mode 100644\nindex 000000000000..9fdfbf560c71\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/25_algorithms/sample/121919.cc\n@@ -0,0 +1,28 @@\n+// { dg-do compile { target c++20 } }\n+\n+// Bug 121919 ranges::sample assumes a uniform_random_bit_generator\n+// provides result_type\n+\n+#include <algorithm>\n+#include <testsuite_iterators.h>\n+\n+struct G\n+{\n+  constexpr static unsigned min() { return 0;  }\n+  constexpr static unsigned max() { return 10; }\n+  unsigned operator()() const;\n+};\n+\n+static_assert(std::uniform_random_bit_generator<G>);\n+\n+void\n+test_pr121919()\n+{\n+  int i2[2]{ 1, 2 };\n+  __gnu_test::test_random_access_range from(i2);\n+  int i1[1];\n+  __gnu_test::test_random_access_range to(i1);\n+  std::ranges::sample(from, std::ranges::begin(to), 1, G{});\n+  std::ranges::sample(std::ranges::begin(from), std::ranges::end(to),\n+\t\t      std::ranges::begin(to), 1, G{});\n+}\ndiff --git a/libstdc++-v3/testsuite/25_algorithms/shuffle/121919.cc b/libstdc++-v3/testsuite/25_algorithms/shuffle/121919.cc\nnew file mode 100644\nindex 000000000000..9425394c50ee\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/25_algorithms/shuffle/121919.cc\n@@ -0,0 +1,25 @@\n+// { dg-do compile { target c++20 } }\n+\n+// Bug 121919 ranges::shuffle assumes a uniform_random_bit_generator\n+// provides result_type\n+\n+#include <algorithm>\n+#include <testsuite_iterators.h>\n+\n+struct G\n+{\n+  constexpr static unsigned min() { return 0;  }\n+  constexpr static unsigned max() { return 10; }\n+  unsigned operator()() const;\n+};\n+\n+static_assert(std::uniform_random_bit_generator<G>);\n+\n+void\n+test_pr121919()\n+{\n+  int arr[2]{ 1, 2 };\n+  __gnu_test::test_random_access_range r(arr);\n+  std::ranges::shuffle(r, G{});\n+  std::ranges::shuffle(std::ranges::begin(r), std::ranges::end(r), G{});\n+}\n","prefixes":[]}