Skip to content

Commit 774295c

Browse files
[libc++][test] Fix MSVC warnings with static_casts (#74962)
Found while running libc++'s tests with MSVC's STL. * `libcxx/test/std/algorithms/alg.modifying.operations/alg.unique/ranges_unique_copy.pass.cpp` + Fix MSVC "warning C4389: '`==`': signed/unsigned mismatch". + This was x86-specific for me. The LHS is `int` and the RHS is `size_t`. We know the `array`'s size, so `static_cast<int>` is certainly safe, and this matches the following `numberOfProj` comparisons. * `libcxx/test/std/containers/sequences/insert_range_sequence_containers.h` + Fix MSVC "warning C4267: 'argument': conversion from '`size_t`' to '`const int`', possible loss of data". + `test_case.index` is `size_t`: https://github.com/llvm/llvm-project/blob/b85f1f9b182234ba366d78ae2174a149e44d08c1/libcxx/test/std/containers/insert_range_helpers.h#L65-L68 + But the container's `difference_type` is `int`: https://github.com/llvm/llvm-project/blob/b85f1f9b182234ba366d78ae2174a149e44d08c1/libcxx/test/support/test_allocator.h#L65-L76 + I introduced an alias `D` to make the long line more readable. * `libcxx/test/std/containers/unord/unord.map/eq.different_hash.pass.cpp` * `libcxx/test/std/containers/unord/unord.multimap/eq.different_hash.pass.cpp` * `libcxx/test/std/containers/unord/unord.multiset/eq.different_hash.pass.cpp` * `libcxx/test/std/containers/unord/unord.set/eq.different_hash.pass.cpp` + Fix MSVC "warning C6297: Arithmetic overflow. Results might not be an expected value." + This warning is almost annoying enough to outright disable, but we use similar `static_cast`s to deal with sign/truncation warnings elsewhere, because there's some value in ensuring that product code is clean with respect to these warnings. If there were many more occurrences, then disabling the warning would be appropriate. + Cleanup: Change 2 inconsistently unqualified occurrences of `size_t` to `std::size_t`. * `libcxx/test/std/containers/views/mdspan/layout_stride/index_operator.pass.cpp` + Fix MSVC "warning C4244: 'initializing': conversion from '`__int64`' to '`size_t`', possible loss of data". + This was x86-specific for me. The `args` are indeed `int64_t`, and we're storing the result in `size_t`, so we should cast. * `libcxx/test/std/ranges/range.utility/range.utility.conv/container.h` + Fix MSVC "warning C4244: 'initializing': conversion from '`ptrdiff_t`' to '`int`', possible loss of data". + Fix MSVC "warning C4267: 'initializing': conversion from '`size_t`' to '`int`', possible loss of data". + We're initializing `int size_`, so we should explicitly cast from pointer subtraction and `std::ranges::size`. * `libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_for_overwrite.pass.cpp` * `libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared_for_overwrite.pass.cpp` * `libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp` + Fix MSVC "warning C4309: 'initializing': truncation of constant value". + MSVC emits this warning because `0xDE` is outside the range of `char` (signed by default in our implementation). * `libcxx/test/support/concat_macros.h` + Fix MSVC "warning C4244: 'argument': conversion from '`char16_t`' to '`const char`', possible loss of data". + Fix MSVC "warning C4244: 'argument': conversion from '`unsigned int`' to '`const char`', possible loss of data". + This code was very recently introduced by @mordante in #73395.
1 parent 8410ee4 commit 774295c

File tree

12 files changed

+35
-34
lines changed

12 files changed

+35
-34
lines changed

libcxx/test/std/algorithms/alg.modifying.operations/alg.unique/ranges_unique_copy.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ constexpr bool test() {
418418
assert(std::ranges::equal(out, expected));
419419
assert(base(result.in) == in.end());
420420
assert(base(result.out) == out.end());
421-
assert(numberOfComp == in.size() - 1);
421+
assert(numberOfComp == static_cast<int>(in.size() - 1));
422422
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
423423
}
424424
// range overload
@@ -434,7 +434,7 @@ constexpr bool test() {
434434
assert(std::ranges::equal(out, expected));
435435
assert(base(result.in) == in.end());
436436
assert(base(result.out) == out.end());
437-
assert(numberOfComp == in.size() - 1);
437+
assert(numberOfComp == static_cast<int>(in.size() - 1));
438438
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
439439
}
440440
}

libcxx/test/std/containers/sequences/insert_range_sequence_containers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ template <> constexpr TestCase<int> FullContainer_End_LongRange<bool> {
427427
template <class Container, class Iter, class Sent, class Validate>
428428
constexpr void test_sequence_insert_range(Validate validate) {
429429
using T = typename Container::value_type;
430-
auto get_pos = [](auto& c, auto& test_case) { return std::ranges::next(c.begin(), test_case.index); };
430+
using D = typename Container::difference_type;
431+
auto get_pos = [](auto& c, auto& test_case) { return std::ranges::next(c.begin(), static_cast<D>(test_case.index)); };
431432

432433
auto test = [&](auto& test_case) {
433434
Container c(test_case.initial.begin(), test_case.initial.end());

libcxx/test/std/containers/unord/unord.map/eq.different_hash.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
3636
}
3737
template <class T>
3838
std::size_t hash_scale(T val) {
39-
return val << 1;
39+
return static_cast<std::size_t>(val << 1);
4040
}
4141
template <class T>
4242
std::size_t hash_even(T val) {
@@ -57,7 +57,7 @@ std::size_t hash_neg(T* val) {
5757
}
5858
template <class T>
5959
std::size_t hash_scale(T* val) {
60-
return *val << 1;
60+
return static_cast<std::size_t>(*val << 1);
6161
}
6262
template <class T>
6363
std::size_t hash_even(T* val) {

libcxx/test/std/containers/unord/unord.multimap/eq.different_hash.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ std::size_t hash_neg(T val) {
3737
}
3838
template <class T>
3939
std::size_t hash_scale(T val) {
40-
return val << 1;
40+
return static_cast<std::size_t>(val << 1);
4141
}
4242
template <class T>
4343
std::size_t hash_even(T val) {
@@ -58,7 +58,7 @@ std::size_t hash_neg(T* val) {
5858
}
5959
template <class T>
6060
std::size_t hash_scale(T* val) {
61-
return *val << 1;
61+
return static_cast<std::size_t>(*val << 1);
6262
}
6363
template <class T>
6464
std::size_t hash_even(T* val) {

libcxx/test/std/containers/unord/unord.multiset/eq.different_hash.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
3636
}
3737
template <class T>
3838
std::size_t hash_scale(T val) {
39-
return val << 1;
39+
return static_cast<std::size_t>(val << 1);
4040
}
4141
template <class T>
4242
std::size_t hash_even(T val) {
@@ -57,7 +57,7 @@ std::size_t hash_neg(T* val) {
5757
}
5858
template <class T>
5959
std::size_t hash_scale(T* val) {
60-
return *val << 1;
60+
return static_cast<std::size_t>(*val << 1);
6161
}
6262
template <class T>
6363
std::size_t hash_even(T* val) {

libcxx/test/std/containers/unord/unord.set/eq.different_hash.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
3636
}
3737
template <class T>
3838
std::size_t hash_scale(T val) {
39-
return val << 1;
39+
return static_cast<std::size_t>(val << 1);
4040
}
4141
template <class T>
4242
std::size_t hash_even(T val) {
@@ -56,11 +56,11 @@ std::size_t hash_neg(T* val) {
5656
return std::numeric_limits<T>::max() - *val;
5757
}
5858
template <class T>
59-
size_t hash_scale(T* val) {
60-
return *val << 1;
59+
std::size_t hash_scale(T* val) {
60+
return static_cast<std::size_t>(*val << 1);
6161
}
6262
template <class T>
63-
size_t hash_even(T* val) {
63+
std::size_t hash_even(T* val) {
6464
return *val & 1 ? 1 : 0;
6565
}
6666

libcxx/test/std/containers/views/mdspan/layout_stride/index_operator.pass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()
5353
constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
5454
if constexpr (-1 == r) {
5555
ASSERT_NOEXCEPT(m(args...));
56-
size_t expected_val = [&]<size_t... Pos>(std::index_sequence<Pos...>) {
56+
std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) {
5757
return ((args * strides[Pos]) + ... + 0);
58-
}(std::make_index_sequence<M::extents_type::rank()>());
59-
assert(expected_val == static_cast<size_t>(m(args...)));
58+
}(std::make_index_sequence<M::extents_type::rank()>()));
59+
assert(expected_val == static_cast<std::size_t>(m(args...)));
6060
} else {
6161
for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
6262
iterate_stride(m, strides, i, args...);
@@ -73,7 +73,7 @@ constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args)
7373
}
7474

7575
constexpr bool test() {
76-
constexpr size_t D = std::dynamic_extent;
76+
constexpr std::size_t D = std::dynamic_extent;
7777
test_iteration<std::extents<int>>(std::array<int, 0>{});
7878
test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1);
7979
test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7);
@@ -102,7 +102,7 @@ constexpr bool test() {
102102
}
103103

104104
constexpr bool test_large() {
105-
constexpr size_t D = std::dynamic_extent;
105+
constexpr std::size_t D = std::dynamic_extent;
106106
test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10);
107107
test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10);
108108
return true;

libcxx/test/std/ranges/range.utility/range.utility.conv/container.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct Container {
3838

3939
constexpr explicit Container(std::ranges::input_range auto&& in)
4040
requires(Rank >= CtrChoice::DirectCtr)
41-
: ctr_choice(CtrChoice::DirectCtr), size_(std::ranges::size(in)) {
41+
: ctr_choice(CtrChoice::DirectCtr), size_(static_cast<int>(std::ranges::size(in))) {
4242
std::ranges::copy(in, begin());
4343
}
4444

@@ -54,7 +54,7 @@ struct Container {
5454

5555
constexpr Container(std::from_range_t, std::ranges::input_range auto&& in)
5656
requires(Rank >= CtrChoice::FromRangeT)
57-
: ctr_choice(CtrChoice::FromRangeT), size_(std::ranges::size(in)) {
57+
: ctr_choice(CtrChoice::FromRangeT), size_(static_cast<int>(std::ranges::size(in))) {
5858
std::ranges::copy(in, begin());
5959
}
6060

@@ -70,7 +70,7 @@ struct Container {
7070
template <class Iter>
7171
constexpr Container(Iter b, Iter e)
7272
requires(Rank >= CtrChoice::BeginEndPair)
73-
: ctr_choice(CtrChoice::BeginEndPair), size_(e - b) {
73+
: ctr_choice(CtrChoice::BeginEndPair), size_(static_cast<int>(e - b)) {
7474
std::ranges::copy(b, e, begin());
7575
}
7676

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/allocate_shared_for_overwrite.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void testAllocatorOperationsCalled() {
156156

157157
template <class T>
158158
struct AllocatorWithPattern {
159-
constexpr static char pattern = 0xDE;
159+
constexpr static char pattern = static_cast<char>(0xDE);
160160

161161
using value_type = T;
162162

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared_for_overwrite.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static_assert(!HasMakeSharedForOverwrite<Foo[]>);
5656
static_assert(!HasMakeSharedForOverwrite<int[], std::size_t, int>);
5757
static_assert(!HasMakeSharedForOverwrite<Foo[], std::size_t, int>);
5858

59-
constexpr char pattern = 0xDE;
59+
constexpr char pattern = static_cast<char>(0xDE);
6060

6161
void* operator new(std::size_t count) {
6262
void* ptr = std::malloc(count);

libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <cstdlib>
2424
#include <memory>
2525

26-
constexpr char pattern = 0xDE;
26+
constexpr char pattern = static_cast<char>(0xDE);
2727

2828
void* operator new(std::size_t count) {
2929
void* ptr = std::malloc(count);

libcxx/test/support/concat_macros.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ template <class OutIt>
5252
requires std::output_iterator<OutIt, const char&>
5353
void test_encode(OutIt& out_it, char16_t value) {
5454
if (value < 0x80)
55-
*out_it++ = value;
55+
*out_it++ = static_cast<char>(value);
5656
else if (value < 0x800) {
57-
*out_it++ = 0b11000000 | (value >> 6);
58-
*out_it++ = 0b10000000 | (value & 0b00111111);
57+
*out_it++ = static_cast<char>(0b11000000 | (value >> 6));
58+
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
5959
} else {
60-
*out_it++ = 0b11100000 | (value >> 12);
61-
*out_it++ = 0b10000000 | ((value) >> 6 & 0b00111111);
62-
*out_it++ = 0b10000000 | (value & 0b00111111);
60+
*out_it++ = static_cast<char>(0b11100000 | (value >> 12));
61+
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 6 & 0b00111111));
62+
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
6363
}
6464
}
6565

@@ -69,10 +69,10 @@ void test_encode(OutIt& out_it, char32_t value) {
6969
if ((value & 0xffff0000) == 0)
7070
test_encode(out_it, static_cast<char16_t>(value));
7171
else {
72-
*out_it++ = 0b11100000 | (value >> 18);
73-
*out_it++ = 0b10000000 | ((value) >> 12 & 0b00111111);
74-
*out_it++ = 0b10000000 | ((value) >> 6 & 0b00111111);
75-
*out_it++ = 0b10000000 | (value & 0b00111111);
72+
*out_it++ = static_cast<char>(0b11100000 | (value >> 18));
73+
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 12 & 0b00111111));
74+
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 6 & 0b00111111));
75+
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
7676
}
7777
}
7878

0 commit comments

Comments
 (0)