[libcxx-commits] [PATCH] D138268: [libcxx] Fix std::equal not accepting volatile types

Alvin Wong via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 18 03:53:18 PST 2022


alvinhochun created this revision.
Herald added a project: All.
alvinhochun updated this revision to Diff 476378.
alvinhochun added a comment.
alvinhochun updated this revision to Diff 476379.
alvinhochun edited the summary of this revision.
alvinhochun updated this revision to Diff 476382.
alvinhochun updated this revision to Diff 476392.
alvinhochun published this revision for review.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

update test


alvinhochun added a comment.

add the actual fix


alvinhochun added a comment.

update test


alvinhochun added a comment.

fix test


Fixes https://github.com/llvm/llvm-project/issues/59021


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138268

Files:
  libcxx/include/__algorithm/equal.h
  libcxx/test/libcxx/algorithms/equal.pass.cpp


Index: libcxx/test/libcxx/algorithms/equal.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/algorithms/equal.pass.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// check that std::equal can accept iterators with cv-qualified types.
+
+#include <algorithm>
+
+#include "test_macros.h"
+
+// GCC 12 gives "warning: 'a' may be used uninitialized [-Wmaybe-uninitialized]"
+#if defined(__GNUC__) && !defined(__clang__)
+#  pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#endif
+
+template <class T1, class T2>
+void f() {
+  int a[3] = {1, 2, 3};
+  T1* x    = a;
+  T2* y    = a;
+  (void)std::equal(x, x + 3, y);
+  (void)std::equal(y, y + 3, x);
+#if TEST_STD_VER >= 14
+  (void)std::equal(x, x + 3, y, y + 3);
+  (void)std::equal(y, y + 3, x, x + 3);
+#endif
+}
+
+int main(int, char**) {
+  f<int, int>();
+  f<int, const int>();
+  f<int, volatile int>();
+  f<int, const volatile int>();
+  f<const int, const int>();
+  f<const int, volatile int>();
+  f<const int, const volatile int>();
+  f<volatile int, volatile int>();
+  f<volatile int, const volatile int>();
+  f<const volatile int, const volatile int>();
+  return 0;
+}
Index: libcxx/include/__algorithm/equal.h
===================================================================
--- libcxx/include/__algorithm/equal.h
+++ libcxx/include/__algorithm/equal.h
@@ -33,8 +33,8 @@
 template <class _InputIterator1, class _InputIterator2>
 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
-  typedef typename iterator_traits<_InputIterator1>::value_type __v1;
-  typedef typename iterator_traits<_InputIterator2>::value_type __v2;
+  typedef typename iterator_traits<_InputIterator1>::reference __v1;
+  typedef typename iterator_traits<_InputIterator2>::reference __v2;
   return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
 }
 
@@ -72,8 +72,8 @@
 template <class _InputIterator1, class _InputIterator2>
 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
-  typedef typename iterator_traits<_InputIterator1>::value_type __v1;
-  typedef typename iterator_traits<_InputIterator2>::value_type __v2;
+  typedef typename iterator_traits<_InputIterator1>::reference __v1;
+  typedef typename iterator_traits<_InputIterator2>::reference __v2;
   return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
                         typename iterator_traits<_InputIterator1>::iterator_category(),
                         typename iterator_traits<_InputIterator2>::iterator_category());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138268.476392.patch
Type: text/x-patch
Size: 3211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221118/f5c1eba0/attachment.bin>


More information about the libcxx-commits mailing list