From d5777f4fef078ba53211fa28382c588381d38eac Mon Sep 17 00:00:00 2001 From: ofAlpaca Date: Mon, 26 Aug 2024 02:15:25 +0800 Subject: [PATCH] Avoid truncation warnings for contiguous iterators with unusual difference types (#4898) Co-authored-by: Stephan T. Lavavej --- stl/inc/xutility | 4 +-- tests/std/test.lst | 1 + .../env.lst | 4 +++ .../test.compile.pass.cpp | 36 +++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/env.lst create mode 100644 tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/test.compile.pass.cpp diff --git a/stl/inc/xutility b/stl/inc/xutility index 9097c5177..e223096de 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -4659,7 +4659,7 @@ _OutCtgIt _Copy_memmove(_CtgIt _First, _CtgIt _Last, _OutCtgIt _Dest) { if constexpr (is_pointer_v<_OutCtgIt>) { return reinterpret_cast<_OutCtgIt>(_Dest_ch + _Count); } else { - return _Dest + (_LastPtr - _FirstPtr); + return _Dest + static_cast<_Iter_diff_t<_OutCtgIt>>(_LastPtr - _FirstPtr); } } @@ -4984,7 +4984,7 @@ _CtgIt2 _Copy_backward_memmove(_CtgIt1 _First, _CtgIt1 _Last, _CtgIt2 _Dest) { if constexpr (is_pointer_v<_CtgIt2>) { return static_cast<_CtgIt2>(_Result); } else { - return _Dest - (_LastPtr - _FirstPtr); + return _Dest - static_cast<_Iter_diff_t<_CtgIt2>>(_LastPtr - _FirstPtr); } } diff --git a/tests/std/test.lst b/tests/std/test.lst index 167b1f22a..41ecb21be 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -230,6 +230,7 @@ tests\GH_003119_error_category_ctor tests\GH_003246_cmath_narrowing tests\GH_003570_allocate_at_least tests\GH_003617_vectorized_meow_element +tests\GH_003663_cast_contiguous_iterator_difference_type tests\GH_003676_format_large_hh_mm_ss_values tests\GH_003735_char_traits_signatures tests\GH_003840_tellg_when_reading_lf_file_in_text_mode diff --git a/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/env.lst b/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/env.lst new file mode 100644 index 000000000..351a8293d --- /dev/null +++ b/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_20_matrix.lst diff --git a/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/test.compile.pass.cpp b/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/test.compile.pass.cpp new file mode 100644 index 000000000..bc6547f0f --- /dev/null +++ b/tests/std/tests/GH_003663_cast_contiguous_iterator_difference_type/test.compile.pass.cpp @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include + +class ContiguousIterator { +public: + using value_type = int; + using difference_type = int; + using element_type = int; + using iterator_category = std::contiguous_iterator_tag; + int* operator->() const; + int& operator*() const; + int& operator[](int) const; + ContiguousIterator& operator++(); + ContiguousIterator operator++(int); + ContiguousIterator& operator--(); + ContiguousIterator operator--(int); + ContiguousIterator& operator+=(int); + ContiguousIterator& operator-=(int); + friend auto operator<=>(ContiguousIterator, ContiguousIterator) = default; + friend int operator-(ContiguousIterator, ContiguousIterator); + friend ContiguousIterator operator+(ContiguousIterator, int); + friend ContiguousIterator operator-(ContiguousIterator, int); + friend ContiguousIterator operator+(int, ContiguousIterator); +}; + +static_assert(std::contiguous_iterator); + +// GH-3663 /: contiguous iterators with non-ptrdiff_t difference types +void test() { + int* p = nullptr; + std::copy(p, p, ContiguousIterator{}); + std::sort(ContiguousIterator{}, ContiguousIterator{}); +}