[libcxx-commits] [libcxx] [libc++][ranges] add static_assert for ranges::to (PR #135802)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 21 03:58:22 PDT 2025


https://github.com/Yuzhiy05 updated https://github.com/llvm/llvm-project/pull/135802

>From 7515cdb203f3c2f1c0734c68de8e6b05d4573655 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Tue, 15 Apr 2025 23:36:08 +0800
Subject: [PATCH 01/11] [libc++][ranges]add static_assert for ranges::to

---
 libcxx/include/__ranges/to.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__ranges/to.h b/libcxx/include/__ranges/to.h
index c937b0656de87..2adae5e0edbfb 100644
--- a/libcxx/include/__ranges/to.h
+++ b/libcxx/include/__ranges/to.h
@@ -28,6 +28,8 @@
 #include <__type_traits/add_pointer.h>
 #include <__type_traits/is_const.h>
 #include <__type_traits/is_volatile.h>
+#include <__type_traits/is_class.h>
+#include <__type_traits/is_union.h>
 #include <__type_traits/type_identity.h>
 #include <__utility/declval.h>
 #include <__utility/forward.h>
@@ -81,7 +83,7 @@ template <class _Container, input_range _Range, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-
+  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
   // First see if the non-recursive case applies -- the conversion target is either:
   // - a range with a convertible value type;
   // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
@@ -208,7 +210,7 @@ template <class _Container, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-
+  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
   auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail) static
     requires requires { //
       /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);

>From 44d6a6533fbc22e9dcbc2899ef631c0700564440 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Tue, 15 Apr 2025 23:47:10 +0800
Subject: [PATCH 02/11] [libc++][ranges]test: add static_assert verification
 for ranges::to

---
 .../range.utility.conv/to.verfiy.cpp            | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
new file mode 100644
index 0000000000000..9ff01b10a759d
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
@@ -0,0 +1,17 @@
+#include <ranges>
+
+
+
+void test(){
+    struct R {
+        int* begin() const{reurn nullptr;};
+        int* end() const{return nullptr;};
+    
+        operator int() const { return 0; }
+      };
+      (void)std::ranges::to<int>(R{});
+        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+      (void)(R{} | std::ranges::to<int>());
+        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+    
+}
\ No newline at end of file

>From 7fe72642b7e7f1860673ec5364366ab91dde75ab Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Wed, 16 Apr 2025 18:50:11 +0800
Subject: [PATCH 03/11] format added file and correct name

---
 .../range.utility.conv/to.verfiy.cpp            | 17 -----------------
 .../range.utility.conv/to.verify.cpp            | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 17 deletions(-)
 delete mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
 create mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
deleted file mode 100644
index 9ff01b10a759d..0000000000000
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <ranges>
-
-
-
-void test(){
-    struct R {
-        int* begin() const{reurn nullptr;};
-        int* end() const{return nullptr;};
-    
-        operator int() const { return 0; }
-      };
-      (void)std::ranges::to<int>(R{});
-        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-      (void)(R{} | std::ranges::to<int>());
-        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-    
-}
\ No newline at end of file
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
new file mode 100644
index 0000000000000..01d211df340c4
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -0,0 +1,15 @@
+#include <ranges>
+
+void test() {
+  struct R {
+    int* begin() const { reurn nullptr; };
+    int* end() const { return nullptr; };
+
+    operator int() const { return 0; }
+  };
+  (void)std::ranges::to<int>(R{});
+  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<int>());
+  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  
+}
\ No newline at end of file

>From f2c50573773471d35bffb4214117d21c7075d737 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Wed, 16 Apr 2025 19:33:45 +0800
Subject: [PATCH 04/11] add banner

---
 .../range.utility/range.utility.conv/to.verify.cpp   | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index 01d211df340c4..8e047f9cddf7b 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -1,3 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+
+// Test that the "mandates" requirements on the given class are checked using `static_assert`.
 #include <ranges>
 
 void test() {

>From ec5c94ad2fd5423d260775e48546436a6c605cb1 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Wed, 16 Apr 2025 23:59:55 +0800
Subject: [PATCH 05/11] format file and test for other type be rejected

---
 libcxx/include/__ranges/to.h                  |  8 +--
 .../range.utility.conv/to.verify.cpp          | 68 ++++++++++++++++---
 2 files changed, 64 insertions(+), 12 deletions(-)

diff --git a/libcxx/include/__ranges/to.h b/libcxx/include/__ranges/to.h
index 2adae5e0edbfb..bd0ee6a60497b 100644
--- a/libcxx/include/__ranges/to.h
+++ b/libcxx/include/__ranges/to.h
@@ -26,10 +26,10 @@
 #include <__ranges/size.h>
 #include <__ranges/transform_view.h>
 #include <__type_traits/add_pointer.h>
-#include <__type_traits/is_const.h>
-#include <__type_traits/is_volatile.h>
 #include <__type_traits/is_class.h>
+#include <__type_traits/is_const.h>
 #include <__type_traits/is_union.h>
+#include <__type_traits/is_volatile.h>
 #include <__type_traits/type_identity.h>
 #include <__utility/declval.h>
 #include <__utility/forward.h>
@@ -83,7 +83,7 @@ template <class _Container, input_range _Range, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
+  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type");
   // First see if the non-recursive case applies -- the conversion target is either:
   // - a range with a convertible value type;
   // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
@@ -210,7 +210,7 @@ template <class _Container, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
+  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type");
   auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail) static
     requires requires { //
       /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index 8e047f9cddf7b..45d967cc07ad4 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -8,20 +8,72 @@
 
 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
 
-
 // Test that the "mandates" requirements on the given class are checked using `static_assert`.
 #include <ranges>
-
+void ff() {}
 void test() {
+  struct C {
+    int member;
+    int f() { return 0; }
+  };
+
+  enum color { red, green, blue };
+  using member_func_ptr = decltype(&C::f);
+  using member_ptr      = decltype(&C::member);
+  using func_ptr        = decltype(&ff);
+  using func_t          = decltype(ff);
+
   struct R {
-    int* begin() const { reurn nullptr; };
+    int* begin() const { return nullptr; };
     int* end() const { return nullptr; };
 
     operator int() const { return 0; }
+    operator int*() const { return nullptr; }
+    operator func_ptr() const { return nullptr; }
+    operator void() const {}
+    operator member_func_ptr() const { return nullptr; }
+    operator member_ptr() const { return nullptr; }
+    operator color() const { return color::red; }
   };
-  (void)std::ranges::to<int>(R{});
-  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<int>());
-  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  
+  (void)std::ranges::to<int>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   int>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<int*>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   int*>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<void>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<member_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             member_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<member_func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             member_func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<color>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_t>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
 }
\ No newline at end of file

>From e73c9582c108eccd087f3d8be53ade18b8412360 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Mon, 21 Apr 2025 01:12:56 +0800
Subject: [PATCH 06/11] clarify assert information and correct the test

---
 libcxx/include/__ranges/to.h                  |  4 +--
 .../range.utility.conv/to.verify.cpp          | 28 +++++++------------
 2 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/libcxx/include/__ranges/to.h b/libcxx/include/__ranges/to.h
index bd0ee6a60497b..3a2bc42969f7b 100644
--- a/libcxx/include/__ranges/to.h
+++ b/libcxx/include/__ranges/to.h
@@ -83,7 +83,7 @@ template <class _Container, input_range _Range, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type");
+  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type or union type");
   // First see if the non-recursive case applies -- the conversion target is either:
   // - a range with a convertible value type;
   // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
@@ -210,7 +210,7 @@ template <class _Container, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type");
+  static_assert(is_class_v<_Container> || is_union_v<_Container>, "The target must be a class type or union type");
   auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail) static
     requires requires { //
       /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index 45d967cc07ad4..24185ca600c97 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -21,7 +21,6 @@ void test() {
   using member_func_ptr = decltype(&C::f);
   using member_ptr      = decltype(&C::member);
   using func_ptr        = decltype(&ff);
-  using func_t          = decltype(ff);
 
   struct R {
     int* begin() const { return nullptr; };
@@ -30,7 +29,6 @@ void test() {
     operator int() const { return 0; }
     operator int*() const { return nullptr; }
     operator func_ptr() const { return nullptr; }
-    operator void() const {}
     operator member_func_ptr() const { return nullptr; }
     operator member_ptr() const { return nullptr; }
     operator color() const { return color::red; }
@@ -49,31 +47,25 @@ void test() {
          std::ranges::to<
              func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
 
-  (void)std::ranges::to<void>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)std::ranges::to<member_ptr>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)(R{} |
          std::ranges::to<
              member_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)std::ranges::to<member_func_ptr>(
+
+  (void)std::ranges::to<func_t>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} |
-         std::ranges::to<
-             member_func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)std::ranges::to<func_ptr>(
+  (void)(R{} | std::ranges::to<
+                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<void>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} |
-         std::ranges::to<
-             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  //expected-error-re@*:* {{static assertion failed{{.*}}ranges::to: unable to convert to the given container type.}}
+
   (void)std::ranges::to<color>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)(R{} | std::ranges::to<
                    color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)std::ranges::to<func_t>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
 }
\ No newline at end of file

>From 1aa41f51c286ecc666d3628b9f59fa42f6e22194 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Mon, 21 Apr 2025 01:15:51 +0800
Subject: [PATCH 07/11] add empty line for test

---
 .../libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index 24185ca600c97..f7331c81f5766 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -68,4 +68,5 @@ void test() {
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)(R{} | std::ranges::to<
                    color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+                   
 }
\ No newline at end of file

>From aabcd20b03c4dd332e4520ff3a055c1f6f75ee61 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Mon, 21 Apr 2025 01:49:06 +0800
Subject: [PATCH 08/11] correct the test

---
 .../libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index f7331c81f5766..f17b0b1def11d 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -21,6 +21,7 @@ void test() {
   using member_func_ptr = decltype(&C::f);
   using member_ptr      = decltype(&C::member);
   using func_ptr        = decltype(&ff);
+  using func_t          = decltype(ff);
 
   struct R {
     int* begin() const { return nullptr; };

>From 9740ad0901b5404a20fe7b16d2b9c99bbccc0ed7 Mon Sep 17 00:00:00 2001
From: Yuzhiy <44502685+Yuzhiy05 at users.noreply.github.com>
Date: Mon, 21 Apr 2025 09:56:47 +0800
Subject: [PATCH 09/11] correct format

---
 .../ranges/range.utility/range.utility.conv/to.verify.cpp    | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index f17b0b1def11d..4bdc2a416d3e9 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -68,6 +68,5 @@ void test() {
   (void)std::ranges::to<color>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)(R{} | std::ranges::to<
-                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-                   
-}
\ No newline at end of file
+                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}                  
+}

>From 10c5fa46ea206752d4629d38b510cd9a7e43bb9c Mon Sep 17 00:00:00 2001
From: Yuzhiy <44502685+Yuzhiy05 at users.noreply.github.com>
Date: Mon, 21 Apr 2025 12:33:27 +0800
Subject: [PATCH 10/11] remove trailing spaces

Co-authored-by: A. Jiang <de34 at live.cn>
---
 .../ranges/range.utility/range.utility.conv/to.verify.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
index 4bdc2a416d3e9..db73b0021fbfb 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -68,5 +68,5 @@ void test() {
   (void)std::ranges::to<color>(
       R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
   (void)(R{} | std::ranges::to<
-                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}                  
+                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
 }

>From da8a6ba25d78391c8e8c9b431b7df5a5e58a135d Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Mon, 21 Apr 2025 18:54:51 +0800
Subject: [PATCH 11/11] merge the unexpected_types test into unified file

---
 .../to.static_assert.verify.cpp               | 79 +++++++++++++++++--
 .../range.utility.conv/to.verify.cpp          | 73 -----------------
 2 files changed, 73 insertions(+), 79 deletions(-)
 delete mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.static_assert.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.static_assert.verify.cpp
index c3ab002558a03..4a1cf9ad4032b 100644
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.static_assert.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.static_assert.verify.cpp
@@ -14,12 +14,79 @@
 #include <ranges>
 #include <vector>
 
-void test() {
+void test_cv_qualifications() {
   using R = std::vector<int>;
-  R in = {1, 2, 3};
+  R in    = {1, 2, 3};
 
-  (void)std::ranges::to<const R>(in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
-  (void)(in | std::ranges::to<const R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
-  (void)std::ranges::to<volatile R>(in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
-  (void)(in | std::ranges::to<volatile R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+  (void)std::ranges::to<const R>(
+      in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
+  (void)(in |
+         std::ranges::to<
+             const R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
+  (void)std::ranges::to<volatile R>(
+      in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+  (void)(in |
+         std::ranges::to<
+             volatile R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+}
+//unexpected_types
+void ff();
+void test_unexpected_types() {
+  struct C {
+    int member;
+    int f();
+  };
+
+  enum color { red, green, blue };
+  using member_func_ptr = decltype(&C::f);
+  using member_ptr      = decltype(&C::member);
+  using func_ptr        = decltype(&ff);
+  using func_t          = decltype(ff);
+
+  struct R {
+    int* begin() const { return nullptr; };
+    int* end() const { return nullptr; };
+
+    operator int() const;
+    operator int*() const;
+    operator func_ptr() const;
+    operator member_func_ptr() const;
+    operator member_ptr() const;
+    operator color() const;
+  };
+  (void)std::ranges::to<int>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   int>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<int*>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   int*>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<member_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             member_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<func_t>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<void>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  //expected-error-re@*:* {{static assertion failed{{.*}}ranges::to: unable to convert to the given container type.}}
+
+  (void)std::ranges::to<color>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
 }
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
deleted file mode 100644
index f17b0b1def11d..0000000000000
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
+++ /dev/null
@@ -1,73 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-
-// Test that the "mandates" requirements on the given class are checked using `static_assert`.
-#include <ranges>
-void ff() {}
-void test() {
-  struct C {
-    int member;
-    int f() { return 0; }
-  };
-
-  enum color { red, green, blue };
-  using member_func_ptr = decltype(&C::f);
-  using member_ptr      = decltype(&C::member);
-  using func_ptr        = decltype(&ff);
-  using func_t          = decltype(ff);
-
-  struct R {
-    int* begin() const { return nullptr; };
-    int* end() const { return nullptr; };
-
-    operator int() const { return 0; }
-    operator int*() const { return nullptr; }
-    operator func_ptr() const { return nullptr; }
-    operator member_func_ptr() const { return nullptr; }
-    operator member_ptr() const { return nullptr; }
-    operator color() const { return color::red; }
-  };
-  (void)std::ranges::to<int>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   int>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)std::ranges::to<int*>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   int*>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)std::ranges::to<func_ptr>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} |
-         std::ranges::to<
-             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-
-  (void)std::ranges::to<member_ptr>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} |
-         std::ranges::to<
-             member_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-
-  (void)std::ranges::to<func_t>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-
-  (void)std::ranges::to<void>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  //expected-error-re@*:* {{static assertion failed{{.*}}ranges::to: unable to convert to the given container type.}}
-
-  (void)std::ranges::to<color>(
-      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-  (void)(R{} | std::ranges::to<
-                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-                   
-}
\ No newline at end of file



More information about the libcxx-commits mailing list