[libcxx-commits] [libcxx] [libc++] Disallow character types being index types of `extents` (PR #105832)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 23 07:36:47 PDT 2024


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/105832

>From e33f05ff475cc693bac28a1678f9d2cd480e2902 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Fri, 23 Aug 2024 22:36:10 +0800
Subject: [PATCH] [libc++] Disallow character types being index types of
 `extents`

PR 78086 (bddd8f46f81477a52ff7ef2873e5671db71c431e) provided the IMO
exact approach - `__libcpp_integer` - for this.
---
 libcxx/include/__mdspan/extents.h             |  5 +-
 .../mdspan/extents/index_type.verify.cpp      | 49 +++++++++++++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 libcxx/test/std/containers/views/mdspan/extents/index_type.verify.cpp

diff --git a/libcxx/include/__mdspan/extents.h b/libcxx/include/__mdspan/extents.h
index 95082ef3d11ac9..8fcfb4b09a2b18 100644
--- a/libcxx/include/__mdspan/extents.h
+++ b/libcxx/include/__mdspan/extents.h
@@ -19,6 +19,8 @@
 
 #include <__assert>
 #include <__config>
+
+#include <__concepts/arithmetic.h>
 #include <__type_traits/common_type.h>
 #include <__type_traits/is_convertible.h>
 #include <__type_traits/is_nothrow_constructible.h>
@@ -282,8 +284,7 @@ class extents {
   using size_type  = make_unsigned_t<index_type>;
   using rank_type  = size_t;
 
-  static_assert(is_integral<index_type>::value && !is_same<index_type, bool>::value,
-                "extents::index_type must be a signed or unsigned integer type");
+  static_assert(__libcpp_integer<index_type>, "extents::index_type must be a signed or unsigned integer type");
   static_assert(((__mdspan_detail::__is_representable_as<index_type>(_Extents) || (_Extents == dynamic_extent)) && ...),
                 "extents ctor: arguments must be representable as index_type and nonnegative");
 
diff --git a/libcxx/test/std/containers/views/mdspan/extents/index_type.verify.cpp b/libcxx/test/std/containers/views/mdspan/extents/index_type.verify.cpp
new file mode 100644
index 00000000000000..e902118aaefaaa
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/extents/index_type.verify.cpp
@@ -0,0 +1,49 @@
+//
+// 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, c++17, c++20
+
+// <mdspan>
+
+// template<class IndexType, size_t... Extents>
+//  class extents;
+//
+// Mandates:
+//   - IndexType is a signed or unsigned integer type, and
+//   - each element of Extents is either equal to dynamic_extent, or is representable as a value of type IndexType.
+
+#include <cstddef>
+#include <climits>
+#include <mdspan>
+
+void invalid_index_types() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents::index_type must be a signed or unsigned integer type}}
+  [[maybe_unused]] std::extents<char, '*'> ec;
+#ifndef TEST_HAS_NO_CHAR8_T
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents::index_type must be a signed or unsigned integer type}}
+  [[maybe_unused]] std::extents<char8_t, u8'*'> ec8;
+#endif
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents::index_type must be a signed or unsigned integer type}}
+  [[maybe_unused]] std::extents<char16_t, u'*'> ec16;
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents::index_type must be a signed or unsigned integer type}}
+  [[maybe_unused]] std::extents<char32_t, U'*'> ec32;
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents::index_type must be a signed or unsigned integer type}}
+  [[maybe_unused]] std::extents<wchar_t, L'*'> ewc;
+#endif
+}
+
+void invalid_extent_values() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents ctor: arguments must be representable as index_type and nonnegative}}
+  [[maybe_unused]] std::extents<signed char, static_cast<std::size_t>(SCHAR_MAX) + 1> esc1;
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents ctor: arguments must be representable as index_type and nonnegative}}
+  [[maybe_unused]] std::extents<signed char, std::dynamic_extent - 1> esc2;
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents ctor: arguments must be representable as index_type and nonnegative}}
+  [[maybe_unused]] std::extents<unsigned char, static_cast<std::size_t>(UCHAR_MAX) + 1> euc1;
+  // expected-error-re@*:* {{static assertion failed {{.*}}extents ctor: arguments must be representable as index_type and nonnegative}}
+  [[maybe_unused]] std::extents<unsigned char, std::dynamic_extent - 1> euc2;
+}



More information about the libcxx-commits mailing list