-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[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
Conversation
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 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. |
@llvm/pr-subscribers-libcxx Author: Yuzhiy (Yuzhiy05) Changesadd static_assert using is_class_v and is_union_v to reject no-class type template parameter and test for this Why does a test expecting a failed static assertion pass in lit? Full diff: https://github.com/llvm/llvm-project/pull/135802.diff 2 Files Affected:
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
|
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
Outdated
Show resolved
Hide resolved
Compilation failure is exactly expected. Just go ahead.
We want to verify that
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 |
✅ With the latest revision this PR passed the C/C++ code formatter. |
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 |
I understand now. but the tests for const and volatile type qualifications are in a separate test file ( |
I think adding to the existing |
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
Outdated
Show resolved
Hide resolved
Co-authored-by: A. Jiang <de34@live.cn>
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
There was a problem hiding this 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.
libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.static_assert.verify.cpp
Outdated
Show resolved
Hide resolved
@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! |
This patch adds
static_assert
usingis_class_v
andis_union_v
to reject no-class type template parameters.Fixes #132133