-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Check correctly ref-qualified __is_callable in algorithms #73451
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
+227
−84
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
libcxx/test/libcxx/algorithms/callable-requirements-rvalue.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <algorithm> | ||
|
||
// Make sure that we don't error out when passing a comparator that is lvalue callable | ||
// but not rvalue callable to algorithms. While it is technically ill-formed for users | ||
// to provide us such predicates, this test is useful for libc++ to ensure that we check | ||
// predicate requirements correctly (i.e. that we check them on lvalues and not on | ||
// rvalues). See https://github.com/llvm/llvm-project/issues/69554 for additional | ||
// context. | ||
|
||
#include <algorithm> | ||
|
||
#include "test_macros.h" | ||
|
||
struct NotRvalueCallable { | ||
bool operator()(int a, int b) const& { return a < b; } | ||
bool operator()(int, int) && = delete; | ||
}; | ||
|
||
void f() { | ||
int a[] = {1, 2, 3, 4}; | ||
(void)std::lower_bound(a, a + 4, 0, NotRvalueCallable{}); | ||
(void)std::upper_bound(a, a + 4, 0, NotRvalueCallable{}); | ||
(void)std::minmax({1, 2, 3}, NotRvalueCallable{}); | ||
(void)std::minmax_element(a, a + 4, NotRvalueCallable{}); | ||
(void)std::min_element(a, a + 4, NotRvalueCallable{}); | ||
(void)std::max_element(a, a + 4, NotRvalueCallable{}); | ||
(void)std::is_permutation(a, a + 4, a, NotRvalueCallable{}); | ||
#if TEST_STD_VER >= 14 | ||
(void)std::is_permutation(a, a + 4, a, a + 4, NotRvalueCallable{}); | ||
#endif | ||
(void)std::includes(a, a + 4, a, a + 4, NotRvalueCallable{}); | ||
(void)std::equal_range(a, a + 4, 0, NotRvalueCallable{}); | ||
(void)std::partial_sort_copy(a, a + 4, a, a + 4, NotRvalueCallable{}); | ||
(void)std::search(a, a + 4, a, a + 4, NotRvalueCallable{}); | ||
(void)std::search_n(a, a + 4, 4, 0, NotRvalueCallable{}); | ||
} |
118 changes: 118 additions & 0 deletions
118
libcxx/test/libcxx/algorithms/callable-requirements.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// Ignore spurious errors after the initial static_assert failure. | ||
// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error | ||
|
||
// <algorithm> | ||
|
||
// Check that calling a classic STL algorithm with various non-callable comparators is diagnosed. | ||
|
||
#include <algorithm> | ||
|
||
#include "test_macros.h" | ||
|
||
struct NotCallable { | ||
bool compare(int a, int b) const { return a < b; } | ||
}; | ||
|
||
struct NotMutableCallable { | ||
bool operator()(int a, int b) = delete; | ||
bool operator()(int a, int b) const { return a < b; } | ||
}; | ||
|
||
void f() { | ||
int a[] = {1, 2, 3, 4}; | ||
{ | ||
(void)std::lower_bound(a, a + 4, 0, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::upper_bound(a, a + 4, 0, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::minmax({1, 2, 3}, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::minmax_element(a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::min_element(a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::max_element(a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::is_permutation(a, a + 4, a, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
#if TEST_STD_VER >= 14 | ||
(void)std::is_permutation(a, a + 4, a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
#endif | ||
|
||
(void)std::includes(a, a + 4, a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::equal_range(a, a + 4, 0, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::partial_sort_copy(a, a + 4, a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::search(a, a + 4, a, a + 4, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::search_n(a, a + 4, 4, 0, &NotCallable::compare); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
} | ||
|
||
{ | ||
(void)std::lower_bound(a, a + 4, 0, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::upper_bound(a, a + 4, 0, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::minmax({1, 2, 3}, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::minmax_element(a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::min_element(a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::max_element(a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::is_permutation(a, a + 4, a, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
#if TEST_STD_VER >= 14 | ||
(void)std::is_permutation(a, a + 4, a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
#endif | ||
|
||
(void)std::includes(a, a + 4, a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::equal_range(a, a + 4, 0, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::partial_sort_copy(a, a + 4, a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::search(a, a + 4, a, a + 4, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
|
||
(void)std::search_n(a, a + 4, 4, 0, NotMutableCallable{}); | ||
// expected-error@*:* {{The comparator has to be callable}} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.