Skip to content

[libc++][test] Fix MSVC warnings with static_casts #74962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ constexpr bool test() {
assert(std::ranges::equal(out, expected));
assert(base(result.in) == in.end());
assert(base(result.out) == out.end());
assert(numberOfComp == in.size() - 1);
assert(numberOfComp == static_cast<int>(in.size() - 1));
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
}
// range overload
Expand All @@ -434,7 +434,7 @@ constexpr bool test() {
assert(std::ranges::equal(out, expected));
assert(base(result.in) == in.end());
assert(base(result.out) == out.end());
assert(numberOfComp == in.size() - 1);
assert(numberOfComp == static_cast<int>(in.size() - 1));
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ template <> constexpr TestCase<int> FullContainer_End_LongRange<bool> {
template <class Container, class Iter, class Sent, class Validate>
constexpr void test_sequence_insert_range(Validate validate) {
using T = typename Container::value_type;
auto get_pos = [](auto& c, auto& test_case) { return std::ranges::next(c.begin(), test_case.index); };
using D = typename Container::difference_type;
auto get_pos = [](auto& c, auto& test_case) { return std::ranges::next(c.begin(), static_cast<D>(test_case.index)); };

auto test = [&](auto& test_case) {
Container c(test_case.initial.begin(), test_case.initial.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
}
template <class T>
std::size_t hash_scale(T val) {
return val << 1;
return static_cast<std::size_t>(val << 1);
}
template <class T>
std::size_t hash_even(T val) {
Expand All @@ -57,7 +57,7 @@ std::size_t hash_neg(T* val) {
}
template <class T>
std::size_t hash_scale(T* val) {
return *val << 1;
return static_cast<std::size_t>(*val << 1);
}
template <class T>
std::size_t hash_even(T* val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::size_t hash_neg(T val) {
}
template <class T>
std::size_t hash_scale(T val) {
return val << 1;
return static_cast<std::size_t>(val << 1);
}
template <class T>
std::size_t hash_even(T val) {
Expand All @@ -58,7 +58,7 @@ std::size_t hash_neg(T* val) {
}
template <class T>
std::size_t hash_scale(T* val) {
return *val << 1;
return static_cast<std::size_t>(*val << 1);
}
template <class T>
std::size_t hash_even(T* val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
}
template <class T>
std::size_t hash_scale(T val) {
return val << 1;
return static_cast<std::size_t>(val << 1);
}
template <class T>
std::size_t hash_even(T val) {
Expand All @@ -57,7 +57,7 @@ std::size_t hash_neg(T* val) {
}
template <class T>
std::size_t hash_scale(T* val) {
return *val << 1;
return static_cast<std::size_t>(*val << 1);
}
template <class T>
std::size_t hash_even(T* val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::size_t hash_neg(T val) {
}
template <class T>
std::size_t hash_scale(T val) {
return val << 1;
return static_cast<std::size_t>(val << 1);
}
template <class T>
std::size_t hash_even(T val) {
Expand All @@ -56,11 +56,11 @@ std::size_t hash_neg(T* val) {
return std::numeric_limits<T>::max() - *val;
}
template <class T>
size_t hash_scale(T* val) {
return *val << 1;
std::size_t hash_scale(T* val) {
return static_cast<std::size_t>(*val << 1);
}
template <class T>
size_t hash_even(T* val) {
std::size_t hash_even(T* val) {
return *val & 1 ? 1 : 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ constexpr void iterate_stride(M m, const std::array<int, M::extents_type::rank()
constexpr int r = static_cast<int>(M::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
if constexpr (-1 == r) {
ASSERT_NOEXCEPT(m(args...));
size_t expected_val = [&]<size_t... Pos>(std::index_sequence<Pos...>) {
std::size_t expected_val = static_cast<std::size_t>([&]<std::size_t... Pos>(std::index_sequence<Pos...>) {
return ((args * strides[Pos]) + ... + 0);
}(std::make_index_sequence<M::extents_type::rank()>());
assert(expected_val == static_cast<size_t>(m(args...)));
}(std::make_index_sequence<M::extents_type::rank()>()));
assert(expected_val == static_cast<std::size_t>(m(args...)));
} else {
for (typename M::index_type i = 0; i < m.extents().extent(r); i++) {
iterate_stride(m, strides, i, args...);
Expand All @@ -73,7 +73,7 @@ constexpr void test_iteration(std::array<int, E::rank()> strides, Args... args)
}

constexpr bool test() {
constexpr size_t D = std::dynamic_extent;
constexpr std::size_t D = std::dynamic_extent;
test_iteration<std::extents<int>>(std::array<int, 0>{});
test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{2}, 1);
test_iteration<std::extents<unsigned, D>>(std::array<int, 1>{3}, 7);
Expand Down Expand Up @@ -102,7 +102,7 @@ constexpr bool test() {
}

constexpr bool test_large() {
constexpr size_t D = std::dynamic_extent;
constexpr std::size_t D = std::dynamic_extent;
test_iteration<std::extents<int64_t, D, 8, D, D>>(std::array<int, 4>{2000, 2, 20, 200}, 7, 9, 10);
test_iteration<std::extents<int64_t, D, 8, 1, D>>(std::array<int, 4>{2000, 20, 20, 200}, 7, 10);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Container {

constexpr explicit Container(std::ranges::input_range auto&& in)
requires(Rank >= CtrChoice::DirectCtr)
: ctr_choice(CtrChoice::DirectCtr), size_(std::ranges::size(in)) {
: ctr_choice(CtrChoice::DirectCtr), size_(static_cast<int>(std::ranges::size(in))) {
std::ranges::copy(in, begin());
}

Expand All @@ -54,7 +54,7 @@ struct Container {

constexpr Container(std::from_range_t, std::ranges::input_range auto&& in)
requires(Rank >= CtrChoice::FromRangeT)
: ctr_choice(CtrChoice::FromRangeT), size_(std::ranges::size(in)) {
: ctr_choice(CtrChoice::FromRangeT), size_(static_cast<int>(std::ranges::size(in))) {
std::ranges::copy(in, begin());
}

Expand All @@ -70,7 +70,7 @@ struct Container {
template <class Iter>
constexpr Container(Iter b, Iter e)
requires(Rank >= CtrChoice::BeginEndPair)
: ctr_choice(CtrChoice::BeginEndPair), size_(e - b) {
: ctr_choice(CtrChoice::BeginEndPair), size_(static_cast<int>(e - b)) {
std::ranges::copy(b, e, begin());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void testAllocatorOperationsCalled() {

template <class T>
struct AllocatorWithPattern {
constexpr static char pattern = 0xDE;
constexpr static char pattern = static_cast<char>(0xDE);

using value_type = T;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static_assert(!HasMakeSharedForOverwrite<Foo[]>);
static_assert(!HasMakeSharedForOverwrite<int[], std::size_t, int>);
static_assert(!HasMakeSharedForOverwrite<Foo[], std::size_t, int>);

constexpr char pattern = 0xDE;
constexpr char pattern = static_cast<char>(0xDE);
Copy link
Member

Choose a reason for hiding this comment

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

Interesting that MSVC issues a diagnostic here too.


void* operator new(std::size_t count) {
void* ptr = std::malloc(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <cstdlib>
#include <memory>

constexpr char pattern = 0xDE;
constexpr char pattern = static_cast<char>(0xDE);

void* operator new(std::size_t count) {
void* ptr = std::malloc(count);
Expand Down
20 changes: 10 additions & 10 deletions libcxx/test/support/concat_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ template <class OutIt>
requires std::output_iterator<OutIt, const char&>
void test_encode(OutIt& out_it, char16_t value) {
if (value < 0x80)
*out_it++ = value;
*out_it++ = static_cast<char>(value);
else if (value < 0x800) {
*out_it++ = 0b11000000 | (value >> 6);
*out_it++ = 0b10000000 | (value & 0b00111111);
*out_it++ = static_cast<char>(0b11000000 | (value >> 6));
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
} else {
*out_it++ = 0b11100000 | (value >> 12);
*out_it++ = 0b10000000 | ((value) >> 6 & 0b00111111);
*out_it++ = 0b10000000 | (value & 0b00111111);
*out_it++ = static_cast<char>(0b11100000 | (value >> 12));
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 6 & 0b00111111));
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
}
}

Expand All @@ -69,10 +69,10 @@ void test_encode(OutIt& out_it, char32_t value) {
if ((value & 0xffff0000) == 0)
test_encode(out_it, static_cast<char16_t>(value));
else {
*out_it++ = 0b11100000 | (value >> 18);
*out_it++ = 0b10000000 | ((value) >> 12 & 0b00111111);
*out_it++ = 0b10000000 | ((value) >> 6 & 0b00111111);
*out_it++ = 0b10000000 | (value & 0b00111111);
*out_it++ = static_cast<char>(0b11100000 | (value >> 18));
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 12 & 0b00111111));
*out_it++ = static_cast<char>(0b10000000 | ((value) >> 6 & 0b00111111));
*out_it++ = static_cast<char>(0b10000000 | (value & 0b00111111));
}
}

Expand Down