Skip to content

Commit 2ba0838

Browse files
[libc++] [test] Fix portability issues for MSVC (#93259)
* Guard `std::__make_from_tuple_impl` tests with `#ifdef _LIBCPP_VERSION` and `LIBCPP_STATIC_ASSERT`. * Change `_LIBCPP_CONSTEXPR_SINCE_CXX20` to `TEST_CONSTEXPR_CXX20`. + Other functions in `variant.swap/swap.pass.cpp` were already using the proper test macro. * Mark `what` as `[[maybe_unused]]` when used by `TEST_LIBCPP_REQUIRE`. + This updates one occurrence in `libcxx/test/libcxx` for consistency. * Windows `_putenv_s()` takes 2 arguments, not 3. + See MSVC documentation: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-170 + POSIX `setenv()` takes `int overwrite`, but Windows `_putenv_s()` always overwrites. * Avoid non-Standard zero-length arrays. + Followup to #74183 and #79792. * Add `operator++()` to `unsized_it`. + The Standard requires this due to [N4981][] [move.iter.requirements]/1 "The template parameter `Iterator` shall either meet the *Cpp17InputIterator* requirements ([input.iterators]) or model `input_iterator` ([iterator.concept.input])." + MSVC's STL requires this because it has a strengthened exception specification in `move_iterator` that inspects the underlying iterator's increment operator. * `uniform_int_distribution` forbids `int8_t`/`uint8_t`. + See [N4981][] [rand.req.genl]/1.5. MSVC's STL enforces this. + Note that when changing the distribution's `IntType`, we need to be careful to preserve the original value range of `[0, max_input]`. * fstreams are constructible from `const fs::path::value_type*` on wide systems. + See [ifstream.cons], [ofstream.cons], [fstream.cons]. * In `msvc_stdlib_force_include.h`, map `_HAS_CXX23` to `TEST_STD_VER` 23 instead of 99. + On 2023-05-23, 7140050 started recognizing 23 as a distinct value. * Fix test name typo: `destory_elements.pass.cpp` => `destroy_elements.pass.cpp` [N4981]: https://wg21.link/N4981
1 parent 266fac8 commit 2ba0838

File tree

15 files changed

+45
-38
lines changed

15 files changed

+45
-38
lines changed

libcxx/test/libcxx/time/time.zone/time.zone.db/time.zone.db.tzdb/locate_zone.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ L link link_to_link
7373
TEST_VALIDATE_EXCEPTION(
7474
std::runtime_error,
7575
[&]([[maybe_unused]] const std::runtime_error& e) {
76-
std::string_view what{"tzdb: requested time zone not found"};
76+
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
7777
TEST_LIBCPP_REQUIRE(
7878
e.what() == what,
7979
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));

libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains_subrange.pass.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23
2525

2626
#include <algorithm>
27+
#include <array>
2728
#include <cassert>
2829
#include <concepts>
2930
#include <ranges>
@@ -130,10 +131,10 @@ constexpr void test_iterators() {
130131
}
131132

132133
{ // range has zero length
133-
int a[] = {};
134-
int p[] = {3, 4, 2};
135-
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
136-
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
134+
std::array<int, 0> a = {};
135+
int p[] = {3, 4, 2};
136+
auto whole = std::ranges::subrange(Iter1(a.data()), Sent1(Iter1(a.data())));
137+
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
137138
{
138139
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
139140
assert(!ret);
@@ -145,10 +146,10 @@ constexpr void test_iterators() {
145146
}
146147

147148
{ // subrange has zero length
148-
int a[] = {3, 4, 2};
149-
int p[] = {};
150-
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
151-
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
149+
int a[] = {3, 4, 2};
150+
std::array<int, 0> p = {};
151+
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
152+
auto subrange = std::ranges::subrange(Iter2(p.data()), Sent2(Iter2(p.data())));
152153
{
153154
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
154155
assert(ret);
@@ -160,10 +161,10 @@ constexpr void test_iterators() {
160161
}
161162

162163
{ // range and subrange both have zero length
163-
int a[] = {};
164-
int p[] = {};
165-
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
166-
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
164+
std::array<int, 0> a = {};
165+
std::array<int, 0> p = {};
166+
auto whole = std::ranges::subrange(Iter1(a.data()), Sent1(Iter1(a.data())));
167+
auto subrange = std::ranges::subrange(Iter2(p.data()), Sent2(Iter2(p.data())));
167168
{
168169
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
169170
assert(ret);

libcxx/test/std/input.output/file.streams/fstreams/fstream.cons/path.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ constexpr bool test_non_convert_to_path() {
3737
static_assert(!std::is_constructible_v<std::fstream, const std::basic_string_view<CharT>>);
3838

3939
// Char* pointers
40-
if constexpr (!std::is_same_v<CharT, char>)
40+
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
4141
static_assert(!std::is_constructible_v<std::fstream, const CharT*>);
4242

4343
// Iterators

libcxx/test/std/input.output/file.streams/fstreams/ifstream.cons/path.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ constexpr bool test_non_convert_to_path() {
3838
static_assert(!std::is_constructible_v<std::ifstream, const std::basic_string_view<CharT>>);
3939

4040
// Char* pointers
41-
if constexpr (!std::is_same_v<CharT, char>)
41+
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
4242
static_assert(!std::is_constructible_v<std::ifstream, const CharT*>);
4343

4444
// Iterators

libcxx/test/std/input.output/file.streams/fstreams/ofstream.cons/path.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ constexpr bool test_non_convert_to_path() {
3737
static_assert(!std::is_constructible_v<std::ofstream, const std::basic_string_view<CharT>>);
3838

3939
// Char* pointers
40-
if constexpr (!std::is_same_v<CharT, char>)
40+
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
4141
static_assert(!std::is_constructible_v<std::ofstream, const CharT*>);
4242

4343
// Iterators

libcxx/test/std/iterators/predef.iterators/move.iterators/sized_sentinel.compile.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct unsized_it {
2121
using difference_type = std::ptrdiff_t;
2222

2323
value_type& operator*() const;
24+
unsized_it& operator++();
2425
bool operator==(const unsized_it&) const;
2526
difference_type operator-(const unsized_it&) const { return 0; }
2627
};

libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cassert>
1818
#include <climits>
1919
#include <cstdint>
20+
#include <limits>
2021
#include <random>
2122
#include <type_traits>
2223

@@ -69,12 +70,14 @@ T basic_gcd(T m, T n) {
6970
template <typename Input>
7071
void do_fuzzy_tests() {
7172
std::mt19937 gen(1938);
72-
std::uniform_int_distribution<Input> distrib;
73+
using DistIntType = std::conditional_t<sizeof(Input) == 1, int, Input>; // See N4981 [rand.req.genl]/1.5
74+
constexpr Input max_input = std::numeric_limits<Input>::max();
75+
std::uniform_int_distribution<DistIntType> distrib(0, max_input);
7376

7477
constexpr int nb_rounds = 10000;
7578
for (int i = 0; i < nb_rounds; ++i) {
76-
Input n = distrib(gen);
77-
Input m = distrib(gen);
79+
Input n = static_cast<Input>(distrib(gen));
80+
Input m = static_cast<Input>(distrib(gen));
7881
assert(std::gcd(n, m) == basic_gcd(n, m));
7982
}
8083
}

libcxx/test/std/time/time.zone/time.zone.db/time.zone.db.access/current_zone.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void set_tz(std::string zone) {
3232
// Unlike POSIX it does not mention the string of putenv becomes part
3333
// of the environment.
3434

35-
int status = _putenv_s("TZ", zone.c_str(), 1);
35+
int status = _putenv_s("TZ", zone.c_str());
3636
assert(status == 0);
3737
}
3838

libcxx/test/std/time/time.zone/time.zone.db/time.zone.db.access/locate_zone.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void test_exception([[maybe_unused]] std::string_view zone) {
4040
TEST_VALIDATE_EXCEPTION(
4141
std::runtime_error,
4242
[&]([[maybe_unused]] const std::runtime_error& e) {
43-
std::string_view what{"tzdb: requested time zone not found"};
43+
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
4444
TEST_LIBCPP_REQUIRE(
4545
e.what() == what,
4646
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));

libcxx/test/std/time/time.zone/time.zone.db/time.zone.db.tzdb/current_zone.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void set_tz(std::string zone) {
3434
// Unlike POSIX it does not mention the string of putenv becomes part
3535
// of the environment.
3636

37-
int status = _putenv_s("TZ", zone.c_str(), 1);
37+
int status = _putenv_s("TZ", zone.c_str());
3838
assert(status == 0);
3939
}
4040

libcxx/test/std/time/time.zone/time.zone.db/time.zone.db.tzdb/locate_zone.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void test_exception([[maybe_unused]] std::string_view zone) {
4242
TEST_VALIDATE_EXCEPTION(
4343
std::runtime_error,
4444
[&]([[maybe_unused]] const std::runtime_error& e) {
45-
std::string_view what{"tzdb: requested time zone not found"};
45+
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
4646
TEST_LIBCPP_REQUIRE(
4747
e.what() == what,
4848
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));

libcxx/test/std/utilities/tuple/tuple.tuple/tuple.apply/make_from_tuple.pass.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ template <class T, class Tuple>
209209
static constexpr bool can_make_from_tuple =
210210
std::is_same_v<decltype(test_make_from_tuple<T, Tuple>(T{}, Tuple{})), uint8_t>;
211211

212+
#ifdef _LIBCPP_VERSION
212213
template <class T, class Tuple>
213214
auto test_make_from_tuple_impl(T&&, Tuple&& t)
214215
-> decltype(std::__make_from_tuple_impl<T>(
@@ -224,6 +225,7 @@ uint32_t test_make_from_tuple_impl(...) {
224225
template <class T, class Tuple>
225226
static constexpr bool can_make_from_tuple_impl =
226227
std::is_same_v<decltype(test_make_from_tuple_impl<T, Tuple>(T{}, Tuple{})), uint8_t>;
228+
#endif // _LIBCPP_VERSION
227229

228230
struct A {
229231
int a;
@@ -263,23 +265,23 @@ static_assert(can_make_from_tuple<float, std::tuple<double>>);
263265
// Test std::__make_from_tuple_impl constraints.
264266

265267
// reinterpret_cast
266-
static_assert(!can_make_from_tuple_impl<int*, std::tuple<A*>>);
267-
static_assert(can_make_from_tuple_impl<A*, std::tuple<A*>>);
268+
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int*, std::tuple<A*>>);
269+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<A*, std::tuple<A*>>);
268270

269271
// const_cast
270-
static_assert(!can_make_from_tuple_impl<char*, std::tuple<const char*>>);
271-
static_assert(!can_make_from_tuple_impl<volatile char*, std::tuple<const volatile char*>>);
272-
static_assert(can_make_from_tuple_impl<volatile char*, std::tuple<volatile char*>>);
273-
static_assert(can_make_from_tuple_impl<char*, std::tuple<char*>>);
274-
static_assert(can_make_from_tuple_impl<const char*, std::tuple<char*>>);
275-
static_assert(can_make_from_tuple_impl<const volatile char*, std::tuple<volatile char*>>);
272+
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<char*, std::tuple<const char*>>);
273+
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<volatile char*, std::tuple<const volatile char*>>);
274+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<volatile char*, std::tuple<volatile char*>>);
275+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<char*, std::tuple<char*>>);
276+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const char*, std::tuple<char*>>);
277+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const volatile char*, std::tuple<volatile char*>>);
276278

277279
// static_cast
278-
static_assert(!can_make_from_tuple_impl<int, std::tuple<D>>);
279-
static_assert(!can_make_from_tuple_impl<D, std::tuple<int>>);
280-
static_assert(can_make_from_tuple_impl<long, std::tuple<int>>);
281-
static_assert(can_make_from_tuple_impl<double, std::tuple<float>>);
282-
static_assert(can_make_from_tuple_impl<float, std::tuple<double>>);
280+
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int, std::tuple<D>>);
281+
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<D, std::tuple<int>>);
282+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<long, std::tuple<int>>);
283+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<double, std::tuple<float>>);
284+
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<float, std::tuple<double>>);
283285

284286
} // namespace LWG3528
285287

libcxx/test/std/utilities/variant/variant.variant/variant.swap/swap.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ constexpr void test_swap_sfinae() {
516516
}
517517
}
518518

519-
_LIBCPP_CONSTEXPR_SINCE_CXX20 void test_swap_noexcept() {
519+
TEST_CONSTEXPR_CXX20 void test_swap_noexcept() {
520520
{
521521
using V = std::variant<int, NothrowMoveable>;
522522
static_assert(std::is_swappable_v<V> && has_swap_member<V>(), "");

libcxx/test/support/msvc_stdlib_force_include.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const AssertionDialogAvoider assertion_dialog_avoider{};
9090
#include <version>
9191

9292
#if _HAS_CXX23
93-
# define TEST_STD_VER 99
93+
# define TEST_STD_VER 23
9494
#elif _HAS_CXX20
9595
# define TEST_STD_VER 20
9696
#elif _HAS_CXX17

0 commit comments

Comments
 (0)