Skip to content

Commit db2eb4d

Browse files
authored
[libc++] Simplify std::launder (llvm#147985)
Both Clang and GCC diagnose invalid calls to `__builtin_launder`, which causes duplicate diagnostics when using `std::launder` in an invalid way. While the diagnostic message for the builtin isn't perferct, it's definitely good enough to understand the problem and adding our own diagnostic doesn't really make things any clearer. Because of that, this patch simply removes the `static_assert`s and lets the compiler handle diagnosing incorrect arguments instead. This not only simplifies our implementation, but also improves compile times a bit, since we avoid instantiating some type traits.
1 parent f223411 commit db2eb4d

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

libcxx/include/__new/launder.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#define _LIBCPP___NEW_LAUNDER_H
1111

1212
#include <__config>
13-
#include <__type_traits/is_function.h>
14-
#include <__type_traits/is_void.h>
1513

1614
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1715
# pragma GCC system_header
@@ -20,15 +18,15 @@
2018
_LIBCPP_BEGIN_NAMESPACE_STD
2119
template <class _Tp>
2220
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT {
23-
static_assert(!(is_function<_Tp>::value), "can't launder functions");
24-
static_assert(!is_void<_Tp>::value, "can't launder cv-void");
21+
// The compiler diagnoses misuses of __builtin_launder, so we don't need to add any static_asserts
22+
// to implement the Mandates.
2523
return __builtin_launder(__p);
2624
}
2725

2826
#if _LIBCPP_STD_VER >= 17
2927
template <class _Tp>
3028
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept {
31-
return std::__launder(__p);
29+
return __builtin_launder(__p);
3230
}
3331
#endif
3432
_LIBCPP_END_NAMESPACE_STD

libcxx/test/std/language.support/support.dynamic/ptr.launder/launder.types.verify.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ int main(int, char**) {
2525
(void)std::launder((void*)nullptr);
2626
(void)std::launder((const void*)nullptr);
2727
(void)std::launder((volatile void*)nullptr);
28-
(void)std::launder(
29-
(const volatile void*)nullptr); // expected-error-re@*:* 4 {{static assertion failed{{.*}}can't launder cv-void}}
30-
// expected-error@*:* 0-4 {{void pointer argument to '__builtin_launder' is not allowed}}
28+
(void)std::launder((const volatile void*)nullptr);
29+
// expected-error@*:* 4 {{void pointer argument to '__builtin_launder' is not allowed}}
3130

32-
(void)std::launder(foo); // expected-error-re@*:* 1 {{static assertion failed{{.*}}can't launder functions}}
33-
// expected-error@*:* 0-1 {{function pointer argument to '__builtin_launder' is not allowed}}
31+
(void)std::launder(foo);
32+
// expected-error@*:* 1 {{function pointer argument to '__builtin_launder' is not allowed}}
3433

3534
return 0;
3635
}

0 commit comments

Comments
 (0)