Skip to content

[libc++][ranges] Reject non-class types in ranges::to #135802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 24, 2025

Conversation

Yuzhiy05
Copy link
Contributor

@Yuzhiy05 Yuzhiy05 commented Apr 15, 2025

This patch adds static_assert using is_class_v and is_union_v to reject no-class type template parameters.

Fixes #132133

@Yuzhiy05 Yuzhiy05 requested a review from a team as a code owner April 15, 2025 16:06
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 15, 2025

@llvm/pr-subscribers-libcxx

Author: Yuzhiy (Yuzhiy05)

Changes

add static_assert using is_class_v and is_union_v to reject no-class type template parameter and test for this
Close #132133
@frederick-vs-ja
Sorry for the delayed response on the previous PR —I spent some time learning how to build libc++ and run its tests. I’ve written a test referencing the to.static_assert.verify.cpp file in the same directory, but it fails to compile. I have two questions:

Why does a test expecting a failed static assertion pass in lit?
What might be wrong with my test implementation?


Full diff: https://github.com/llvm/llvm-project/pull/135802.diff

2 Files Affected:

  • (modified) libcxx/include/__ranges/to.h (+4-2)
  • (added) libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp (+17)
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)...);
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

@frederick-vs-ja
Copy link
Contributor

I’ve written a test referencing the to.static_assert.verify.cpp file in the same directory, but it fails to compile.

Compilation failure is exactly expected. Just go ahead.

Why does a test expecting a failed static assertion pass in lit?

We want to verify that static_assert (usually corresponding to Mandates in the standard wording) works.

What might be wrong with my test implementation?

See other review comments. Also, perhaps we want to verify that const types, volatile types, other arithmetic types, pointer types, pointer-to-member types, enumeration types, cv void, reference types, and function types are also rejected.

Copy link

github-actions bot commented Apr 16, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Yuzhiy05
Copy link
Contributor Author

I’ve written a test referencing the to.static_assert.verify.cpp file in the same directory, but it fails to compile.

Compilation failure is exactly expected. Just go ahead.

sorry,my previous statement was unclear, I meant to say is that in my LLVM LIT tests, the results show failures (test failures), even expect compilation errors.but the to.static_assert.verify.cpp is passed and this expect failures

@Yuzhiy05 Yuzhiy05 closed this Apr 16, 2025
@Yuzhiy05 Yuzhiy05 reopened this Apr 16, 2025
@Yuzhiy05
Copy link
Contributor Author

What might be wrong with my test implementation?

See other review comments. Also, perhaps we want to verify that const types, volatile types, other arithmetic types, pointer types, pointer-to-member types, enumeration types, cv void, reference types, and function types are also rejected.

I understand now. but the tests for const and volatile type qualifications are in a separate test file (to.static_assert.verify.cpp). perhaps I should consolidate these tests into a unified file ?

@frederick-vs-ja
Copy link
Contributor

What might be wrong with my test implementation?

See other review comments. Also, perhaps we want to verify that const types, volatile types, other arithmetic types, pointer types, pointer-to-member types, enumeration types, cv void, reference types, and function types are also rejected.

I understand now. but the tests for const and volatile type qualifications are in a separate test file (to.static_assert.verify.cpp). perhaps I should consolidate these tests into a unified file ?

I think adding to the existing to.static_assert.verify.cpp would be better.

Co-authored-by: A. Jiang <de34@live.cn>
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Although I'd wait for @philnik777.

@Yuzhiy05 Yuzhiy05 requested a review from philnik777 April 23, 2025 06:37
@philnik777 philnik777 changed the title [libc++][ranges] add static_assert for ranges::to [libc++][ranges] Reject non-class types in ranges::to Apr 23, 2025
Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with formatting change.

@Yuzhiy05 Yuzhiy05 requested a review from H-G-Hristov April 24, 2025 01:14
@philnik777 philnik777 merged commit 03c2862 into llvm:main Apr 24, 2025
85 checks passed
Copy link

@Yuzhiy05 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++][ranges] ranges::to can have non-class return type
5 participants