[libcxx-commits] [pstl] [pstl] Avoid reusing moved-from iterators in PSTL tests (PR #80128)

Jonathan Wakely via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 31 02:51:17 PST 2024


https://github.com/jwakely created https://github.com/llvm/llvm-project/pull/80128

The `invoke_on_all_iterator_types` and `reverse_invoker` utilities for PSTL tests use forwarding references for all parameters, but some of those parameters get forwarded to move constructors which then leave the objects in a moved-from state. When the parameters are forwarded a second time that results in making new copies of moved-from iterators. For libstdc++ debug mode iterators, the moved-from state is singular, which means copying them will abort at runtime.

The fix is to make copies of iterator arguments instead of forwarding them.

Fixes #80126

>From 48e3106de92a8eeb4b8e58e7ae46f481a9a7d2d5 Mon Sep 17 00:00:00 2001
From: Jonathan Wakely <jwakely at redhat.com>
Date: Wed, 31 Jan 2024 10:47:53 +0000
Subject: [PATCH] [pstl] Avoid reusing moved-from iterators in PSTL tests

The invoke_on_all_iterator_types and reverse_invoker utilities for PSTL
tests use forwarding references for all parameters, but some of those
parameters get forwarded to move constructors which then leave the
objects in a moved-from state. When the parameters are forwarded a
second time that results in making new copies of moved-from iterators.
For libstdc++ debug mode iterators, the moved-from state is singular,
which means copying them will abort at runtime.

The fix is to make copies of iterator arguments instead of forwarding
them.

Fixes #80126
---
 pstl/test/support/utils.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/pstl/test/support/utils.h b/pstl/test/support/utils.h
index ed6d48b9471ac..33ce2081c6c20 100644
--- a/pstl/test/support/utils.h
+++ b/pstl/test/support/utils.h
@@ -1083,29 +1083,29 @@ struct iterator_invoker<std::forward_iterator_tag, /*isReverse=*/std::true_type>
 template <typename IsReverse>
 struct reverse_invoker
 {
-    template <typename... Rest>
+    template <typename Policy, typename Op, typename... Rest>
     void
-    operator()(Rest&&... rest)
+    operator()(Policy&& exec, Op op, Rest&&... rest)
     {
         // Random-access iterator
-        iterator_invoker<std::random_access_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::random_access_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
 
         // Forward iterator
-        iterator_invoker<std::forward_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::forward_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
 
         // Bidirectional iterator
-        iterator_invoker<std::bidirectional_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::bidirectional_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
     }
 };
 
 struct invoke_on_all_iterator_types
 {
-    template <typename... Rest>
+    template <typename Policy, typename Op, typename... Rest>
     void
-    operator()(Rest&&... rest)
+    operator()(Policy&& exec, Op op, Rest&&... rest)
     {
-        reverse_invoker</* IsReverse = */ std::false_type>()(std::forward<Rest>(rest)...);
-        reverse_invoker</* IsReverse = */ std::true_type>()(std::forward<Rest>(rest)...);
+        reverse_invoker</* IsReverse = */ std::false_type>()(std::forward<Policy>(exec), op, rest...);
+        reverse_invoker</* IsReverse = */ std::true_type>()(std::forward<Policy>(exec), op, rest...);
     }
 };
 //============================================================================



More information about the libcxx-commits mailing list