[libcxx-commits] [clang] [libcxx] [Clang] Add __builtin_common_type (PR #99473)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 30 07:54:38 PDT 2024


================
@@ -14,16 +14,30 @@
 #include <__type_traits/decay.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/remove_cvref.h>
+#include <__type_traits/type_identity.h>
 #include <__type_traits/void_t.h>
 #include <__utility/declval.h>
+#include <__utility/empty.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-#if _LIBCPP_STD_VER >= 20
+#if __has_builtin(__builtin_common_type)
+
+template <class... _Args>
+struct common_type;
+
+template <class... _Args>
+using __common_type_t = typename common_type<_Args...>::type;
+
+template <class... _Args>
+struct common_type : __builtin_common_type<__common_type_t, __type_identity, __empty, _Args...> {};
----------------
ldionne wrote:

@philnik777 This causes a problem when modules are enabled, because we'd have to export `__builtin_common_type` from this module but I don't think there's any way of doing that. That leads to errors of this kind:

```
# | In module 'std' imported from /llvm/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp:19:
# | /llvm/build-runtimes/libcxx/test-suite-install/include/c++/v1/__chrono/duration.h:90:101: error: no type named 'type' in 'std::common_type<long double, long double, long>'
# |    90 |     typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
# |       |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# | /llvm/build-runtimes/libcxx/test-suite-install/include/c++/v1/__chrono/duration.h:107:10: note: in instantiation of member function 'std::chrono::__duration_cast<std::chrono::duration<long double>, std::chrono::duration<long double, std::ratio<1, 1000>>>::operator()' requested here
# |   107 |   return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
# |       |          ^
# | /llvm/build-runtimes/libcxx/test-suite-install/include/c++/v1/__chrono/duration.h:220:24: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long double, std::ratio<1, 1000>>, long double, std::ratio<1>, 0>' requested here
# |   220 |       : __rep_(chrono::duration_cast<duration>(__d).count()) {}
# |       |                        ^
# | /llvm/build-runtimes/libcxx/test-suite-install/include/c++/v1/__chrono/duration.h:385:33: note: in instantiation of function template specialization 'std::chrono::duration<long double, std::ratio<1, 1000>>::duration<long double, std::ratio<1>, 0>' requested here
# |   385 |   return _Ct(__lhs).count() <=> _Ct(__rhs).count();
# |       |                                 ^
# | /llvm/build-runtimes/libcxx/test-suite-install/include/c++/v1/__thread/this_thread.h:45:13: note: in instantiation of function template specialization 'std::chrono::operator<=><long long, std::ratio<1, 1000>, long double, std::ratio<1>>' requested here
# |    45 |     if (__d < __max) {
# |       |             ^
# | /llvm/libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.destr/dtor.pass.cpp:63:29: note: in instantiation of function template specialization 'std::this_thread::sleep_for<long long, std::ratio<1, 1000>>' requested here
# |    63 |           std::this_thread::sleep_for(std::chrono::milliseconds(250));
# |       |                             ^
# | 1 error generated.
```

This didn't show up in the CI here because the modules CI doesn't run against Clang trunk.

https://github.com/llvm/llvm-project/pull/99473


More information about the libcxx-commits mailing list