Skip to content

Conversation

@frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Sep 28, 2025

Several components in libc++ aren't defending against overloaded operator,(T, Iter) currently. Existing deleted overloads in test_iterators.h are insufficient for such cases.

This PR adds corresponding deleted overloads with reversed order and fixes these libc++ components.

  • piecewise_linear_distribution's iterator pair constructor,
  • piecewise_linear_distribution::param_type's iterator pair constructor,
  • piecewise_constant_distribution's iterator pair constructor,
  • piecewise_constant_distribution::param_type's iterator pair constructor,
  • money_get::do_get,
  • money_put::do_put, and
  • num_put::do_put.

Fixes #160732.

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner September 28, 2025 06:06
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2025

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

Changes

Existing deleted overloads in test_iterators.h are insufficient for the (size_t, Iter) case. This PR only adds one non-member overload for cpp17_input_iterator because it is only adaptor used for tests. Overloads for other iterators may be added in one or more later PRs.

Fixes #160732.


Full diff: https://github.com/llvm/llvm-project/pull/161049.diff

7 Files Affected:

  • (modified) libcxx/include/__random/piecewise_constant_distribution.h (+1-1)
  • (modified) libcxx/include/__random/piecewise_linear_distribution.h (+1-1)
  • (modified) libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp (+3-1)
  • (modified) libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/param_ctor_iterator.pass.cpp (+3-1)
  • (modified) libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp (+3-1)
  • (modified) libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp (+3-1)
  • (modified) libcxx/test/support/test_iterators.h (+3)
diff --git a/libcxx/include/__random/piecewise_constant_distribution.h b/libcxx/include/__random/piecewise_constant_distribution.h
index c5bfa8dc3a4be..bd624fffd7f0e 100644
--- a/libcxx/include/__random/piecewise_constant_distribution.h
+++ b/libcxx/include/__random/piecewise_constant_distribution.h
@@ -190,7 +190,7 @@ piecewise_constant_distribution<_RealType>::param_type::param_type(
     __areas_.assign(1, 0.0);
   } else {
     __densities_.reserve(__b_.size() - 1);
-    for (size_t __i = 0; __i < __b_.size() - 1; ++__i, ++__f_w)
+    for (size_t __i = 0; __i < __b_.size() - 1; ++__i, (void)++__f_w)
       __densities_.push_back(*__f_w);
     __init();
   }
diff --git a/libcxx/include/__random/piecewise_linear_distribution.h b/libcxx/include/__random/piecewise_linear_distribution.h
index a9906430c005c..1ceef77c8716b 100644
--- a/libcxx/include/__random/piecewise_linear_distribution.h
+++ b/libcxx/include/__random/piecewise_linear_distribution.h
@@ -194,7 +194,7 @@ piecewise_linear_distribution<_RealType>::param_type::param_type(
     __areas_.assign(1, 0.0);
   } else {
     __densities_.reserve(__b_.size());
-    for (size_t __i = 0; __i < __b_.size(); ++__i, ++__f_w)
+    for (size_t __i = 0; __i < __b_.size(); ++__i, (void)++__f_w)
       __densities_.push_back(*__f_w);
     __init();
   }
diff --git a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp
index ea6e807ca47b5..46decd7d974ee 100644
--- a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/ctor_iterator.pass.cpp
@@ -21,15 +21,17 @@
 #include <cassert>
 #include <vector>
 
+#include "test_iterators.h"
 #include "test_macros.h"
 
 int main(int, char**)
 {
     {
         typedef std::piecewise_constant_distribution<> D;
+        typedef cpp17_input_iterator<const double*> InIt;
         double b[] = {10};
         double p[] = {12};
-        D d(b, b, p);
+        D d(InIt(b), InIt(b), InIt(p));
         std::vector<double> iv = d.intervals();
         assert(iv.size() == 2);
         assert(iv[0] == 0);
diff --git a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/param_ctor_iterator.pass.cpp b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/param_ctor_iterator.pass.cpp
index baf6108b7e2e8..8cb42391925a7 100644
--- a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/param_ctor_iterator.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.pconst/param_ctor_iterator.pass.cpp
@@ -20,6 +20,7 @@
 #include <cassert>
 #include <vector>
 
+#include "test_iterators.h"
 #include "test_macros.h"
 
 int main(int, char**)
@@ -27,9 +28,10 @@ int main(int, char**)
     {
         typedef std::piecewise_constant_distribution<> D;
         typedef D::param_type P;
+        typedef cpp17_input_iterator<const double*> InIt;
         double b[] = {10};
         double p[] = {12};
-        P pa(b, b, p);
+        P pa(InIt(b), InIt(b), InIt(p));
         std::vector<double> iv = pa.intervals();
         assert(iv.size() == 2);
         assert(iv[0] == 0);
diff --git a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp
index 24f7d4e18c36a..084538dfb5156 100644
--- a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/ctor_iterator.pass.cpp
@@ -21,15 +21,17 @@
 #include <cassert>
 #include <vector>
 
+#include "test_iterators.h"
 #include "test_macros.h"
 
 int main(int, char**)
 {
     {
         typedef std::piecewise_linear_distribution<> D;
+        typedef cpp17_input_iterator<const double*> InIt;
         double b[] = {10};
         double p[] = {12};
-        D d(b, b, p);
+        D d(InIt(b), InIt(b), InIt(p));
         std::vector<double> iv = d.intervals();
         assert(iv.size() == 2);
         assert(iv[0] == 0);
diff --git a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp
index 04ded2a1c9706..42754456e0d6d 100644
--- a/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.plinear/param_ctor_iterator.pass.cpp
@@ -20,6 +20,7 @@
 #include <cassert>
 #include <vector>
 
+#include "test_iterators.h"
 #include "test_macros.h"
 
 int main(int, char**)
@@ -27,9 +28,10 @@ int main(int, char**)
     {
         typedef std::piecewise_linear_distribution<> D;
         typedef D::param_type P;
+        typedef cpp17_input_iterator<const double*> InIt;
         double b[] = {10};
         double p[] = {12};
-        P pa(b, b, p);
+        P pa(InIt(b), InIt(b), InIt(p));
         std::vector<double> iv = pa.intervals();
         assert(iv.size() == 2);
         assert(iv[0] == 0);
diff --git a/libcxx/test/support/test_iterators.h b/libcxx/test/support/test_iterators.h
index 0335a4c561017..006b27c10b3f0 100644
--- a/libcxx/test/support/test_iterators.h
+++ b/libcxx/test/support/test_iterators.h
@@ -109,6 +109,9 @@ class cpp17_input_iterator
 
     template <class T>
     void operator,(T const &) = delete;
+
+    template <class T>
+    friend void operator,(const T&, const cpp17_input_iterator&) = delete;
 };
 #if TEST_STD_VER > 14
 template <class It>

@frederick-vs-ja frederick-vs-ja changed the title [libc++] Avoid overloaded operator, for piecewise_*_distribution [libc++] Avoid overloaded operator, for (T, Iter) cases Sep 28, 2025
Several components in libc++ aren't defending against overloaded
`operator,(T, Iter)` currently. Existing deleted overloads in
`test_iterators.h` are insufficient for such cases.

This PR adds corresponding deleted overloads with reversed order and
fixes these libc++ components.
- `piecewise_linear_distribution`'s iterator pair constructor,
- `piecewise_linear_distribution::param_type`'s iterator pair
constructor,
- `piecewise_constant_distribution`'s iterator pair constructor,
- `piecewise_constant_distribution::param_type`'s iterator pair
constructor,
- `money_get::do_get`,
- `money_put::do_put`, and
- `num_put::do_put`.
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to forward to the algorithms here if we have to touch these lines anyways. I'm fine with replacing them either in this patch or in a predecessor.

@frederick-vs-ja frederick-vs-ja merged commit 1ffe79d into llvm:main Nov 10, 2025
80 checks passed
ckoparkar added a commit to ckoparkar/llvm-project that referenced this pull request Nov 10, 2025
* main: (1028 commits)
  [clang][DebugInfo] Attach `DISubprogram` to additional call variants (llvm#166202)
  [C2y] Claim nonconformance to WG14 N3348 (llvm#166966)
  [X86] 2012-01-10-UndefExceptionEdge.ll - regenerate test checks (llvm#167307)
  Remove unused standard headers: <string>, <optional>, <numeric>, <tuple> (llvm#167232)
  [DebugInfo] Add Verifier check for incorrectly-scoped retainedNodes (llvm#166855)
  [VPlan] Don't apply predication discount to non-originally-predicated blocks (llvm#160449)
  [libc++] Avoid overloaded `operator,` for (`T`, `Iter`) cases (llvm#161049)
  [tools][llc] Make save-stats.ll test target independent (llvm#167238)
  [AArch64] Fallback to PRFUM for PRFM with negative or unaligned offset (llvm#166756)
  [X86] ldexp-avx512.ll - add v8f16/v16f16/v32f16 test coverage for llvm#165694 (llvm#167294)
  [DropAssumes] Drop dereferenceable assumptions after vectorization. (llvm#166947)
  [VPlan] Simplify branch-cond with getVectorTripCount (llvm#155604)
  Remove unused <algorithm> inclusion (llvm#166942)
  [AArch64] Combine subtract with borrow to SBC. (llvm#165271)
  [AArch64][SVE] Avoid redundant extend of unsigned i8/i16 extracts. (llvm#165863)
  [SPIRV] Fix failing assertion in SPIRVAsmPrinter (llvm#166909)
  [libc++] Merge insert/emplace(const_iterator, Args...) implementations (llvm#166470)
  [libc++] Replace __libcpp_is_final with a variable template (llvm#167137)
  [gn build] Port 152bda7
  [libc++] Replace the last uses of __tuple_types with __type_list (llvm#167214)
  ...
@frederick-vs-ja frederick-vs-ja deleted the piecewise-ctor-no-comma branch December 2, 2025 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc++] <random>: piecewise_linear_distribution(InputIteratorB, InputIteratorB, InputIteratorW) invokes comma operator

3 participants