Using requires { sizeof(T); } in __new_allocator::allocate gives a
better diagnostic when the static_assert fails.
We can also reduce the total number of diagnostics due to invalid
sizeof(T) and alignof(T) by using the same requires-expression to make
the body of the function discarded statement when the static_assert
already failed. This fixes a regression in diagnostic quality due to
making __new_allocator::allocate constexpr in r16-7271-g7197d0cce70525,
which caused a cascade of seven errors instead of just one for
std::allocator<incomplete-type>().allocate(1).
libstdc++-v3/ChangeLog:
* include/bits/allocator.h (allocator::allocate): Use specific
feature test macro for constexpr allocate and deallocate. Make
consteval path a discarded statement if sizeof(T) is ill-formed.
* include/bits/new_allocator.h (__new_allocator::allocate): Use
requires-expression for static_cast. Make function body a
discarded stament if sizeof(T) is ill-formed. Use if-constexpr
for alignment checks.
---
Tested x86_64-linux.
libstdc++-v3/include/bits/allocator.h | 5 ++++-
libstdc++-v3/include/bits/new_allocator.h | 22 +++++++++++++++++-----
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/libstdc++-v3/include/bits/allocator.h
b/libstdc++-v3/include/bits/allocator.h
index a5e115265911..9c22c805ebe7 100644
--- a/libstdc++-v3/include/bits/allocator.h
+++ b/libstdc++-v3/include/bits/allocator.h
@@ -188,11 +188,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
~allocator() _GLIBCXX_NOTHROW { }
-#if __cplusplus > 201703L
+#if __cpp_constexpr_dynamic_alloc // >= C++20
[[nodiscard,__gnu__::__always_inline__]]
constexpr _Tp*
allocate(size_t __n)
{
+#if __cpp_concepts
+ if constexpr (requires { sizeof(_Tp); })
+#endif
if (std::__is_constant_evaluated())
{
if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
diff --git a/libstdc++-v3/include/bits/new_allocator.h
b/libstdc++-v3/include/bits/new_allocator.h
index 4fe67f99e5c0..fbe03e392aae 100644
--- a/libstdc++-v3/include/bits/new_allocator.h
+++ b/libstdc++-v3/include/bits/new_allocator.h
@@ -120,6 +120,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
# define _GLIBCXX_OPERATOR_DELETE ::operator delete
#endif
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
+
// NB: __n is permitted to be 0. The C++ standard says nothing
// about what the return value is when __n == 0.
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR _Tp*
@@ -128,9 +131,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#if __cplusplus >= 201103L
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3308. std::allocator<void>().allocate(n)
+#if ! __cpp_concepts
static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
-#endif
+#else
+ static_assert(requires { sizeof(_Tp); },
+ "cannot allocate incomplete types");
+ if constexpr (!requires { sizeof(_Tp); })
+ return nullptr; // static_assert already failed
+ else
+#endif
+#endif
if (__builtin_expect(__n > this->_M_max_size(), false))
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
@@ -139,16 +150,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
std::__throw_bad_array_new_length();
std::__throw_bad_alloc();
}
-
#if __cpp_aligned_new && __cplusplus >= 201103L
- if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ else if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
{
std::align_val_t __al = std::align_val_t(alignof(_Tp));
return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp),
__al));
}
#endif
- return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
+ else
+ return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
}
// __p is not permitted to be a null pointer.
@@ -162,7 +173,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
#if __cpp_aligned_new && __cplusplus >= 201103L
- if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ if constexpr (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
{
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
std::align_val_t(alignof(_Tp)));
@@ -172,6 +183,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
}
+#pragma GCC diagnostic pop
#undef _GLIBCXX_SIZED_DEALLOC
#undef _GLIBCXX_OPERATOR_DELETE
#undef _GLIBCXX_OPERATOR_NEW
--
2.53.0