[libcxx-commits] [libcxx] [llvm] [libcxx] improves diagnostics for containers with bad value types (PR #106296)
Christopher Di Bella via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Sep 8 10:33:52 PDT 2024
================
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___TYPE_TRAITS_DIAGNOSTIC_UTILITIES_H
+#define _LIBCPP___TYPE_TRAITS_DIAGNOSTIC_UTILITIES_H
+
+#include <__config>
+#include <__type_traits/is_bounded_array.h>
+#include <__type_traits/is_const.h>
+#include <__type_traits/is_function.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/is_unbounded_array.h>
+#include <__type_traits/is_void.h>
+#include <__type_traits/is_volatile.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 20
+# define _LIBCPP_CHECK_CONTAINER_VALUE_TYPE_IS_NOT_ARRAY_BEFORE_CXX20(_Template, _Tp, _Verb)
+#else
+# define _LIBCPP_CHECK_CONTAINER_VALUE_TYPE_IS_NOT_ARRAY_BEFORE_CXX20(_Template, _Tp, _Verb) \
+ ; \
+ static_assert(!__libcpp_is_bounded_array<_Tp>::value, "'std::" _Template "' cannot " _Verb " C arrays before C++20")
+#endif
+
+// Per https://eel.is/c++draft/containers#container.reqmts-64, allocator-aware containers must have an
+// allocator that meets the Cpp17Allocator requirements (https://eel.is/c++draft/allocator.requirements).
+// In particular, this means that containers should only accept non-cv-qualified object types, and
+// types that are Cpp17Erasable.
+#define _LIBCPP_CHECK_ALLOCATOR_VALUE_TYPE_REQUIREMENTS(_Template, _Tp, _Verb) \
+ static_assert(!is_const<_Tp>::value, "'std::" _Template "' cannot " _Verb " const types"); \
+ static_assert(!is_volatile<_Tp>::value, "'std::" _Template "' cannot " _Verb " volatile types"); \
+ static_assert(!is_reference<_Tp>::value, "'std::" _Template "' cannot " _Verb " references"); \
+ static_assert(!is_function<_Tp>::value, "'std::" _Template "' cannot " _Verb " functions") \
+ _LIBCPP_CHECK_CONTAINER_VALUE_TYPE_IS_NOT_ARRAY_BEFORE_CXX20(_Template, _Tp, _Verb)
+
+#define _LIBCPP_CHECK_CONTAINER_VALUE_TYPE_REQUIREMENTS(_Container, _Tp) \
+ static_assert( \
+ !__libcpp_is_unbounded_array<_Tp>::value, "'std::" _Container "' cannot hold C arrays of an unknown size"); \
----------------
cjdb wrote:
Hmm, I'd prefer to keep the single quotes so that it's consistent with how Clang outputs type information. For example, this is the diagnostic we get for this erroneous code:
```cpp
namespace std {
template<class T>
struct S1;
template<class T>
struct S2;
}
std::S1<std::S2> y;
```
```
error: use of class template 'std::S2' requires template arguments
```
This helps us remain more consistent as a whole implementation:
```
error: static assertion failed: 'std::vector' cannot hold C arrays of an unknown size
```
WDYT?
https://github.com/llvm/llvm-project/pull/106296
More information about the libcxx-commits
mailing list