Skip to content

Conversation

philnik777
Copy link
Contributor

@philnik777 philnik777 commented Sep 17, 2025

All of the .fail.cpp tests are actually testing constraints, so we should just test that the overloads SFINAE away correctly.

@philnik777 philnik777 force-pushed the refactor_fail_cpp_tests branch from c30a5aa to ab689e6 Compare September 18, 2025 07:50
@ldionne ldionne marked this pull request as ready for review September 23, 2025 14:43
@ldionne ldionne requested a review from a team as a code owner September 23, 2025 14:43
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 23, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

All of the .fail.cpp tests are actually testing constraints, so we should just test that the overloads SFINAE away correctly.


Full diff: https://github.com/llvm/llvm-project/pull/159288.diff

11 Files Affected:

  • (modified) libcxx/include/__chrono/time_point.h (+1-1)
  • (removed) libcxx/test/std/time/time.duration/time.duration.alg/abs.compile.fail.cpp (-28)
  • (modified) libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp (+10)
  • (removed) libcxx/test/std/time/time.point/time.point.cast/ceil.compile.fail.cpp (-27)
  • (modified) libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp (+9)
  • (removed) libcxx/test/std/time/time.point/time.point.cast/floor.compile.fail.cpp (-27)
  • (modified) libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp (+9)
  • (removed) libcxx/test/std/time/time.point/time.point.cast/round.compile.fail.cpp (-27)
  • (modified) libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp (+9)
  • (modified) libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp (+10)
  • (removed) libcxx/test/std/time/time.point/time.point.cast/toduration.compile.fail.cpp (-29)
diff --git a/libcxx/include/__chrono/time_point.h b/libcxx/include/__chrono/time_point.h
index fc4408d23dbf1..bc2c7798a630b 100644
--- a/libcxx/include/__chrono/time_point.h
+++ b/libcxx/include/__chrono/time_point.h
@@ -95,7 +95,7 @@ struct common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_C
 
 namespace chrono {
 
-template <class _ToDuration, class _Clock, class _Duration>
+template <class _ToDuration, class _Clock, class _Duration, __enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
 time_point_cast(const time_point<_Clock, _Duration>& __t) {
   return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
diff --git a/libcxx/test/std/time/time.duration/time.duration.alg/abs.compile.fail.cpp b/libcxx/test/std/time/time.duration/time.duration.alg/abs.compile.fail.cpp
deleted file mode 100644
index 8d807c7a9b395..0000000000000
--- a/libcxx/test/std/time/time.duration/time.duration.alg/abs.compile.fail.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <chrono>
-
-// ceil
-
-// template <class Rep, class Period>
-//   constexpr duration<Rep, Period> abs(duration<Rep, Period> d)
-
-// This function shall not participate in overload resolution unless numeric_limits<Rep>::is_signed is true.
-
-#include <chrono>
-
-typedef std::chrono::duration<unsigned> unsigned_secs;
-
-int main(int, char**)
-{
-    std::chrono::abs(unsigned_secs(0));
-
-  return 0;
-}
diff --git a/libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp b/libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp
index f95d42b52638e..42f4323b5bd27 100644
--- a/libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp
+++ b/libcxx/test/std/time/time.duration/time.duration.alg/abs.pass.cpp
@@ -19,9 +19,19 @@
 #include <cassert>
 #include <ratio>
 #include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
+template <class T, class = void>
+inline constexpr bool has_abs_v = false;
+
+template <class T>
+inline constexpr bool has_abs_v<T, decltype((void)std::chrono::abs(std::declval<T>()))> = true;
+
+static_assert(has_abs_v<std::chrono::milliseconds>);
+static_assert(!has_abs_v<std::chrono::duration<unsigned>>);
+
 template <class Duration>
 void
 test(const Duration& f, const Duration& d)
diff --git a/libcxx/test/std/time/time.point/time.point.cast/ceil.compile.fail.cpp b/libcxx/test/std/time/time.point/time.point.cast/ceil.compile.fail.cpp
deleted file mode 100644
index fb82fdffe4d2a..0000000000000
--- a/libcxx/test/std/time/time.point/time.point.cast/ceil.compile.fail.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <chrono>
-
-// ceil
-
-// template <class ToDuration, class Clock, class Duration>
-//   time_point<Clock, ToDuration>
-//   ceil(const time_point<Clock, Duration>& t);
-
-// ToDuration shall be an instantiation of duration.
-
-#include <chrono>
-
-int main(int, char**)
-{
-    std::chrono::ceil<int>(std::chrono::system_clock::now());
-
-  return 0;
-}
diff --git a/libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp b/libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp
index 58bcf73234f91..4e383ba923759 100644
--- a/libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp
+++ b/libcxx/test/std/time/time.point/time.point.cast/ceil.pass.cpp
@@ -21,6 +21,15 @@
 
 #include "test_macros.h"
 
+template <class T, class = void>
+inline constexpr bool has_ceil_v = false;
+
+template <class T>
+inline constexpr bool has_ceil_v<T, decltype((void)std::chrono::ceil<T>(std::chrono::system_clock::now()))> = true;
+
+static_assert(has_ceil_v<std::chrono::seconds>);
+static_assert(!has_ceil_v<int>);
+
 template <class FromDuration, class ToDuration>
 void
 test(const FromDuration& df, const ToDuration& d)
diff --git a/libcxx/test/std/time/time.point/time.point.cast/floor.compile.fail.cpp b/libcxx/test/std/time/time.point/time.point.cast/floor.compile.fail.cpp
deleted file mode 100644
index 12b1dec9fd509..0000000000000
--- a/libcxx/test/std/time/time.point/time.point.cast/floor.compile.fail.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <chrono>
-
-// floor
-
-// template <class ToDuration, class Clock, class Duration>
-//   time_point<Clock, ToDuration>
-//   floor(const time_point<Clock, Duration>& t);
-
-// ToDuration shall be an instantiation of duration.
-
-#include <chrono>
-
-int main(int, char**)
-{
-    std::chrono::floor<int>(std::chrono::system_clock::now());
-
-  return 0;
-}
diff --git a/libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp b/libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp
index e53ce86869f7a..69e4d096b7441 100644
--- a/libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp
+++ b/libcxx/test/std/time/time.point/time.point.cast/floor.pass.cpp
@@ -21,6 +21,15 @@
 
 #include "test_macros.h"
 
+template <class T, class = void>
+inline constexpr bool has_floor_v = false;
+
+template <class T>
+inline constexpr bool has_floor_v<T, decltype((void)std::chrono::floor<T>(std::chrono::system_clock::now()))> = true;
+
+static_assert(has_floor_v<std::chrono::seconds>);
+static_assert(!has_floor_v<int>);
+
 template <class FromDuration, class ToDuration>
 void
 test(const FromDuration& df, const ToDuration& d)
diff --git a/libcxx/test/std/time/time.point/time.point.cast/round.compile.fail.cpp b/libcxx/test/std/time/time.point/time.point.cast/round.compile.fail.cpp
deleted file mode 100644
index a5436c684040d..0000000000000
--- a/libcxx/test/std/time/time.point/time.point.cast/round.compile.fail.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14
-// <chrono>
-
-// round
-
-// template <class ToDuration, class Clock, class Duration>
-//   time_point<Clock, ToDuration>
-//   round(const time_point<Clock, Duration>& t);
-
-// ToDuration shall be an instantiation of duration.
-
-#include <chrono>
-
-int main(int, char**)
-{
-    std::chrono::round<int>(std::chrono::system_clock::now());
-
-  return 0;
-}
diff --git a/libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp b/libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp
index 4523abd27d035..74f05f72daf7c 100644
--- a/libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp
+++ b/libcxx/test/std/time/time.point/time.point.cast/round.pass.cpp
@@ -21,6 +21,15 @@
 
 #include "test_macros.h"
 
+template <class T, class = void>
+inline constexpr bool has_round_v = false;
+
+template <class T>
+inline constexpr bool has_round_v<T, decltype((void)std::chrono::round<T>(std::chrono::system_clock::now()))> = true;
+
+static_assert(has_round_v<std::chrono::seconds>);
+static_assert(!has_round_v<int>);
+
 template <class FromDuration, class ToDuration>
 void
 test(const FromDuration& df, const ToDuration& d)
diff --git a/libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp b/libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp
index 0d82e2db8b7b7..d82cf42c145f9 100644
--- a/libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp
+++ b/libcxx/test/std/time/time.point/time.point.cast/time_point_cast.pass.cpp
@@ -21,6 +21,16 @@
 
 #include "test_macros.h"
 
+template <class T, class = void>
+struct has_time_point_cast : std::false_type {};
+
+template <class T>
+struct has_time_point_cast<T, decltype((void)std::chrono::time_point_cast<T>(std::chrono::system_clock::now()))>
+    : std::true_type {};
+
+static_assert(has_time_point_cast<std::chrono::seconds>::value, "");
+static_assert(!has_time_point_cast<int>::value, "");
+
 template <class FromDuration, class ToDuration>
 void
 test(const FromDuration& df, const ToDuration& d)
diff --git a/libcxx/test/std/time/time.point/time.point.cast/toduration.compile.fail.cpp b/libcxx/test/std/time/time.point/time.point.cast/toduration.compile.fail.cpp
deleted file mode 100644
index c16492f730a17..0000000000000
--- a/libcxx/test/std/time/time.point/time.point.cast/toduration.compile.fail.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// <chrono>
-
-// time_point
-
-// template <class ToDuration, class Clock, class Duration>
-//   time_point<Clock, ToDuration>
-//   time_point_cast(const time_point<Clock, Duration>& t);
-
-// ToDuration shall be an instantiation of duration.
-
-#include <chrono>
-
-int main(int, char**)
-{
-    typedef std::chrono::system_clock Clock;
-    typedef std::chrono::time_point<Clock, std::chrono::milliseconds> FromTimePoint;
-    typedef std::chrono::time_point<Clock, std::chrono::minutes> ToTimePoint;
-    std::chrono::time_point_cast<ToTimePoint>(FromTimePoint(std::chrono::milliseconds(3)));
-
-  return 0;
-}

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the nice cleanup!

@philnik777 philnik777 force-pushed the refactor_fail_cpp_tests branch from ab689e6 to cd9454a Compare September 24, 2025 08:48
@philnik777 philnik777 merged commit bb1d15e into llvm:main Sep 25, 2025
78 checks passed
@philnik777 philnik777 deleted the refactor_fail_cpp_tests branch September 25, 2025 07:25
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…INAEing away (llvm#159288)

All of the `.fail.cpp` tests are actually testing constraints, so we
should just test that the overloads SFINAE away correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants