diff --git a/libcxx/CREDITS.TXT b/libcxx/CREDITS.TXT --- a/libcxx/CREDITS.TXT +++ b/libcxx/CREDITS.TXT @@ -24,10 +24,6 @@ E: holgerar@gmail.com D: Minor fix. -N: Ruben Van Boxem -E: vanboxem dot ruben at gmail dot com -D: Initial Windows patches. - N: David Chisnall E: theraven at theravensnest dot org D: FreeBSD and Solaris ports, libcxxrt support, some atomics work. @@ -41,6 +37,10 @@ E: jbcoe@me.com D: Implementation of propagate_const. +N: Matthew Dempsky +E: matthew@dempsky.org +D: Minor patches and bug fixes. + N: Christopher Di Bella E: cjdb@google.com E: cjdb.ns@gmail.com @@ -58,10 +58,6 @@ E: william.w.fisher@gmail.com D: Regex bug fixes. -N: Matthew Dempsky -E: matthew@dempsky.org -D: Minor patches and bug fixes. - N: Google Inc. D: Copyright owner and contributor of the CityHash algorithm @@ -113,6 +109,10 @@ E: jroelofS@jroelofs.com D: Remote testing, Newlib port, baremetal/single-threaded support. +N: Kent Ross +E: k@mad.cash +D: C++20 language feature support + N: Jonathan Sauer D: Minor patches, mostly related to constexpr @@ -131,6 +131,10 @@ E: st@quanttec.com D: Minor fix +N: Ruben Van Boxem +E: vanboxem dot ruben at gmail dot com +D: Initial Windows patches. + N: Michael van der Westhuizen E: r1mikey at gmail dot com @@ -149,11 +153,11 @@ E: xingxue@ca.ibm.com D: AIX port -N: Zhihao Yuan -E: lichray@gmail.com -D: Standard compatibility fixes. - N: Jeffrey Yasskin E: jyasskin@gmail.com E: jyasskin@google.com D: Linux fixes. + +N: Zhihao Yuan +E: lichray@gmail.com +D: Standard compatibility fixes. diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -99,6 +99,7 @@ __bsd_locale_fallbacks.h __compare/common_comparison_category.h __compare/ordering.h + __compare/synth_three_way.h __concepts/arithmetic.h __concepts/assignable.h __concepts/boolean_testable.h @@ -120,6 +121,7 @@ __concepts/same_as.h __concepts/semiregular.h __concepts/swappable.h + __concepts/three_way_comparable.h __concepts/totally_ordered.h __config __debug diff --git a/libcxx/include/__compare/synth_three_way.h b/libcxx/include/__compare/synth_three_way.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__compare/synth_three_way.h @@ -0,0 +1,51 @@ +// THIS FILE NOT FOR REVIEW (see https://reviews.llvm.org/D107721 ) +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___COMPARE_SYNTH_THREE_WAY_H +#define _LIBCPP___COMPARE_SYNTH_THREE_WAY_H + +#include <__config> +#include <__compare/ordering.h> +#include <__concepts/three_way_comparable.h> +#include <__utility/declval.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS) + +// [expos.only.func] + +constexpr auto __synth_three_way = + [](const _Tp& __t, const _Up& __u) + requires requires { + { __t < __u } -> __boolean_testable; + { __u < __t } -> __boolean_testable; + } + { + if constexpr (three_way_comparable_with<_Tp, _Up>) { + return __t <=> __u; + } else { + if (__t < __u) return weak_ordering::less; + if (__u < __t) return weak_ordering::greater; + return weak_ordering::equivalent; + } + }; + +template +using __synth_three_way_result = decltype(__synth_three_way(declval<_Tp&>(), declval<_Up&>())); + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS) + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___COMPARE_SYNTH_THREE_WAY_H diff --git a/libcxx/include/__concepts/three_way_comparable.h b/libcxx/include/__concepts/three_way_comparable.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__concepts/three_way_comparable.h @@ -0,0 +1,51 @@ +// THIS FILE NOT FOR REVIEW +#ifndef INCLUDE___CONCEPTS_THREE_WAY_COMPARABLE_H_ +#define INCLUDE___CONCEPTS_THREE_WAY_COMPARABLE_H_ + +#include <__compare/common_comparison_category.h> +#include <__compare/ordering.h> +#include <__concepts/common_reference_with.h> +#include <__concepts/equality_comparable.h> +#include <__concepts/totally_ordered.h> +#include <__config> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) + +template +concept __compares_as = + same_as, Cat>; + +template +concept three_way_comparable = + __weakly_equality_comparable_with && + __partially_ordered_with && + requires(const remove_reference_t& a, const remove_reference_t& b) { + { a <=> b } -> __compares_as; + }; + +template +concept three_way_comparable_with = + three_way_comparable && + three_way_comparable && + common_reference_with&, const remove_reference_t&> && + three_way_comparable< + common_reference_t&, const remove_reference_t&>, Cat> && + __weakly_equality_comparable_with && + __partially_ordered_with && + requires(const remove_reference_t& t, const remove_reference_t& u) { + { t <=> u } -> __compares_as; + { u <=> t } -> __compares_as; + }; + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) + +_LIBCPP_END_NAMESPACE_STD + +#endif //INCLUDE___CONCEPTS_THREE_WAY_COMPARABLE_H_ diff --git a/libcxx/include/concepts b/libcxx/include/concepts --- a/libcxx/include/concepts +++ b/libcxx/include/concepts @@ -150,6 +150,7 @@ #include <__concepts/same_as.h> #include <__concepts/semiregular.h> #include <__concepts/swappable.h> +#include <__concepts/three_way_comparable.h> // NOT FOR REVIEW #include <__concepts/totally_ordered.h> #include <__config> #include diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -362,6 +362,7 @@ module __compare { module common_comparison_category { private header "__compare/common_comparison_category.h" } module ordering { private header "__compare/ordering.h" } + module synth_three_way { private header "__compare/synth_three_way.h" } } } module complex { @@ -394,6 +395,7 @@ module same_as { private header "__concepts/same_as.h" } module semiregular { private header "__concepts/semiregular.h" } module swappable { private header "__concepts/swappable.h" } + module three_way_comparable { private header "__concepts/three_way_comparable.h" } module totally_ordered { private header "__concepts/totally_ordered.h" } } } diff --git a/libcxx/include/tuple b/libcxx/include/tuple --- a/libcxx/include/tuple +++ b/libcxx/include/tuple @@ -149,6 +149,8 @@ */ +#include <__compare/common_comparison_category.h> +#include <__compare/synth_three_way.h> #include <__config> #include <__functional/unwrap_ref.h> #include <__functional_base> @@ -156,6 +158,7 @@ #include <__memory/uses_allocator.h> #include <__tuple> #include <__utility/forward.h> +#include <__utility/integer_sequence.h> #include <__utility/move.h> #include #include @@ -1300,6 +1303,30 @@ return __tuple_equal()(__x, __y); } +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS) + +// operator<=> + +template +inline _LIBCPP_INLINE_VISIBILITY constexpr +auto +__tuple_compare_three_way(const tuple<_Tp...>& __x, const tuple<_Up...>& __y, index_sequence<_Is...>) { + common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...> __result = strong_ordering::equal; + static_cast(((__result = _VSTD::__synth_three_way(get<_Is>(__x), get<_Is>(__y)), __result != 0) || ...)); + return __result; +} + +template +inline _LIBCPP_INLINE_VISIBILITY constexpr +auto +operator<=>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) +{ + static_assert (sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes"); + return __tuple_compare_three_way(__x, __y, index_sequence_for<_Tp...>{}); +} + +#else // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS) + template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool @@ -1368,6 +1395,8 @@ return !(__y < __x); } +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS) + // tuple_cat template struct __tuple_cat_type; diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/compare/synth_three_way.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/compare/synth_three_way.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/compare/synth_three_way.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__compare/synth_three_way.h'}} +#include <__compare/synth_three_way.h> diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/concepts/three_way_comparable.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/concepts/three_way_comparable.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/concepts/three_way_comparable.module.verify.cpp @@ -0,0 +1,16 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// REQUIRES: modules-build + +// WARNING: This test was generated by 'generate_private_header_tests.py' +// and should not be edited manually. + +// expected-error@*:* {{use of private header from outside its module: '__concepts/three_way_comparable.h'}} +#include <__concepts/three_way_comparable.h> diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/eq_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/eq_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/eq_incompatible.fail.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator==(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) == std::tuple(1, 2)); // expected-note {{requested here}} + // expected-error-re@tuple:* {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + static_cast(std::tuple(1, 2) == std::tuple(1)); // expected-note {{requested here}} + // expected-error-re@tuple:* {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + // expected-error-re@__tuple:* {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* {{no viable conversion from}} + + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ge_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ge_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ge_incompatible.fail.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator>=(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) >= std::tuple(1, 2)); // expected-note {{requested here}} + static_cast(std::tuple(1, 2) >= std::tuple(1)); // expected-note {{requested here}} + // Always + // expected-error-re@tuple:* 2 {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + // c++11, c++14, c++17 + // expected-error-re@__tuple:* 0-1 {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* 0-1 {{no viable conversion from}} + + // c++14, c++17 + // expected-error@tuple:* 0-1 {{no matching function for call to 'get'}} + + // c++20 onwards + // expected-error@tuple:* 0-2 {{pack expansion contains parameter packs '_Tp' and '_Up' that have different lengths}} + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/gt_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/gt_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/gt_incompatible.fail.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator>(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) > std::tuple(1, 2)); // expected-note {{requested here}} + static_cast(std::tuple(1, 2) > std::tuple(1)); // expected-note {{requested here}} + // Always + // expected-error-re@tuple:* 2 {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + // c++11, c++14, c++17 + // expected-error-re@__tuple:* 0-1 {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* 0-1 {{no viable conversion from}} + + // c++14, c++17 + // expected-error@tuple:* 0-1 {{no matching function for call to 'get'}} + + // c++20 onwards + // expected-error@tuple:* 0-2 {{pack expansion contains parameter packs '_Tp' and '_Up' that have different lengths}} + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/le_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/le_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/le_incompatible.fail.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator<=(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) <= std::tuple(1, 2)); // expected-note {{requested here}} + static_cast(std::tuple(1, 2) <= std::tuple(1)); // expected-note {{requested here}} + // Always + // expected-error-re@tuple:* 2 {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + // c++11, c++14, c++17 + // expected-error-re@__tuple:* 0-1 {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* 0-1 {{no viable conversion from}} + + // c++14, c++17 + // expected-error@tuple:* 0-1 {{no matching function for call to 'get'}} + + // c++20 onwards + // expected-error@tuple:* 0-2 {{pack expansion contains parameter packs '_Tp' and '_Up' that have different lengths}} + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/lt_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/lt_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/lt_incompatible.fail.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator<(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) < std::tuple(1, 2)); // expected-note {{requested here}} + static_cast(std::tuple(1, 2) < std::tuple(1)); // expected-note {{requested here}} + // Always + // expected-error-re@tuple:* 2 {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + // c++11, c++14, c++17 + // expected-error-re@__tuple:* 0-1 {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* 0-1 {{no viable conversion from}} + + // c++14, c++17 + // expected-error@tuple:* 0-1 {{no matching function for call to 'get'}} + + // c++20 onwards + // expected-error@tuple:* 0-2 {{pack expansion contains parameter packs '_Tp' and '_Up' that have different lengths}} + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ne_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ne_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/ne_incompatible.fail.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// bool +// operator!=(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) != std::tuple(1, 2)); // expected-note {{requested here}} + // expected-error-re@tuple:* {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + + static_cast(std::tuple(1, 2) != std::tuple(1)); // expected-note {{requested here}} + // expected-error-re@tuple:* {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + // expected-error-re@__tuple:* {{static_assert failed{{.*}} "tuple_element index out of range"}} + // expected-error@tuple:* {{no viable conversion from}} + + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way.pass.cpp @@ -0,0 +1,178 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// auto +// operator<=>(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03, c++11, c++14, c++17, libcpp-no-concepts + +#include +#include +#include + +#include "test_macros.h" + +// A custom three-way result type +struct CustomResult { + friend constexpr bool operator==(int, const CustomResult&) noexcept { return true; } + friend constexpr bool operator==(const CustomResult&, int) noexcept { return true; } + friend constexpr bool operator<(int, const CustomResult&) noexcept { return false; } + friend constexpr bool operator<(const CustomResult&, int) noexcept { return false; } +}; + +int main(int, char**) +{ + // Empty tuple + { + typedef std::tuple<> T0; + static_assert((T0() <=> T0()) == std::strong_ordering::equal, ""); + // No member types yields strong ordering (all are equal). + ASSERT_SAME_TYPE(decltype(T0() <=> T0()), std::strong_ordering); + } + // Mixed types with integers, which compare strongly ordered + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering); + static_assert((T1(1) <=> T2(1)) == std::strong_ordering::equal, ""); + static_assert((T1(1) <=> T2(0)) == std::strong_ordering::greater, ""); + static_assert((T1(1) <=> T2(2)) == std::strong_ordering::less, ""); + } + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering); + static_assert((T1(1, 2) <=> T2(1, 2)) == std::strong_ordering::equal, ""); + static_assert((T1(1, 2) <=> T2(0, 2)) == std::strong_ordering::greater, ""); + static_assert((T1(1, 2) <=> T2(2, 2)) == std::strong_ordering::less, ""); + static_assert((T1(1, 2) <=> T2(1, 1)) == std::strong_ordering::greater, ""); + static_assert((T1(1, 2) <=> T2(1, 3)) == std::strong_ordering::less, ""); + } + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 3)) == std::strong_ordering::equal, ""); + static_assert((T1(1, 2, 3) <=> T2(0, 2, 3)) == std::strong_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(2, 2, 3)) == std::strong_ordering::less, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 1, 3)) == std::strong_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 3, 3)) == std::strong_ordering::less, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 2)) == std::strong_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 4)) == std::strong_ordering::less, ""); + } + // Mixed types with floating point, which compare partially ordered + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering); + static_assert((T1(1) <=> T2(1)) == std::partial_ordering::equivalent, ""); + static_assert((T1(1) <=> T2(0.9)) == std::partial_ordering::greater, ""); + static_assert((T1(1) <=> T2(1.1)) == std::partial_ordering::less, ""); + static_assert((T1(1) <=> T2(std::numeric_limits::quiet_NaN())) + == std::partial_ordering::unordered, ""); + } + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering); + static_assert((T1(1, 2) <=> T2(1, 2)) == std::partial_ordering::equivalent, ""); + static_assert((T1(1, 2) <=> T2(0.9, 2)) == std::partial_ordering::greater, ""); + static_assert((T1(1, 2) <=> T2(1.1, 2)) == std::partial_ordering::less, ""); + static_assert((T1(1, 2) <=> T2(1, 1)) == std::partial_ordering::greater, ""); + static_assert((T1(1, 2) <=> T2(1, 3)) == std::partial_ordering::less, ""); + static_assert((T1(1, 2) <=> T2(std::numeric_limits::quiet_NaN(), 2)) + == std::partial_ordering::unordered, ""); + static_assert((T1(1, std::numeric_limits::quiet_NaN()) <=> T2(1, 2)) + == std::partial_ordering::unordered, ""); + } + { + typedef std::tuple T1; + typedef std::tuple T2; + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 3)) == std::partial_ordering::equivalent, ""); + static_assert((T1(1, 2, 3) <=> T2(0.9, 2, 3)) == std::partial_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(1.1, 2, 3)) == std::partial_ordering::less, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 1, 3)) == std::partial_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 3, 3)) == std::partial_ordering::less, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 2)) == std::partial_ordering::greater, ""); + static_assert((T1(1, 2, 3) <=> T2(1, 2, 4)) == std::partial_ordering::less, ""); + static_assert((T1(1, 2, 3) <=> T2(std::numeric_limits::quiet_NaN(), 2, 3)) + == std::partial_ordering::unordered, ""); + static_assert((T1(1, std::numeric_limits::quiet_NaN(), 3) <=> T2(1, 2, 3)) + == std::partial_ordering::unordered, ""); + static_assert((T1(1, 2, std::numeric_limits::quiet_NaN()) <=> T2(1, 2, 3)) + == std::partial_ordering::unordered, ""); + } + // Ordering classes and synthesized three way comparison + { + typedef std::tuple T1; + typedef std::tuple T2; + // All strongly ordered members yields strong ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering); + } + { + struct WeakSpaceship { + constexpr bool operator==(const WeakSpaceship&) const { return true; } + constexpr std::weak_ordering operator<=>(const WeakSpaceship&) const { + return std::weak_ordering::equivalent; + } + }; + { + typedef std::tuple T1; + typedef std::tuple T2; + // Strongly ordered members and a weakly ordered member yields weak ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering); + } + { + typedef std::tuple T1; + typedef std::tuple T2; + // Doubles are partially ordered, so one partial, one strong, and one weak ordering + // yields partial ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering); + } + } + { + struct NoSpaceship { + constexpr bool operator==(const NoSpaceship&) const { return true; } + constexpr bool operator<(const NoSpaceship&) const { return false; } + }; + typedef std::tuple T1; + typedef std::tuple T2; + // Strongly ordered members and a weakly ordered member (synthesized) yields weak ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering); + } + { + struct SpaceshipNoEquals { + constexpr std::strong_ordering operator<=>(const SpaceshipNoEquals&) const { + return std::strong_ordering::equal; + } + constexpr bool operator<(const SpaceshipNoEquals&) const { return false; } + }; + typedef std::tuple T1; + typedef std::tuple T2; + // Spaceship operator with no == operator falls back on the < operator and weak ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering); + } + { + struct CustomSpaceship { + constexpr CustomResult operator<=>(const CustomSpaceship&) const { return CustomResult(); } + }; + typedef std::tuple T1; + typedef std::tuple T2; + // Custom three way return types cannot be used in synthesized three way comparison, + // but they can be used for (rewritten) operator< when synthesizing a weak ordering. + ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering); + } + + return 0; +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way_incompatible.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way_incompatible.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/three_way_incompatible.fail.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// auto +// operator<=>(const tuple& t, const tuple& u); + +// UNSUPPORTED: c++03, c++11, c++14, c++17, libcpp-no-concepts + +#include + +#include "test_macros.h" + +int main(int, char**) +{ + static_cast(std::tuple(1) <=> std::tuple(1, 2)); // expected-note {{requested here}} + static_cast(std::tuple(1, 2) <=> std::tuple(1)); // expected-note {{requested here}} + // expected-error-re@tuple:* 2 {{static_assert failed{{.*}} "Can't compare tuples of different sizes"}} + // expected-error@tuple:* 2 {{pack expansion contains parameter packs '_Tp' and '_Up' that have different lengths}} + return 0; +}