-
Notifications
You must be signed in to change notification settings - Fork 261
feat: recognize requires
expressions
#596
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// https://youtu.be/CXn02MPkn8Y?t=2337 | ||
negatable: <T> concept = requires(t: T) | ||
{ | ||
_ = -t is T; // Hopefully obviously wrong. Should be `{ -t } is T;`. | ||
}; | ||
|
||
main: () = static_assert(negatable<char>); |
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,8 @@ | ||
// https://youtu.be/CXn02MPkn8Y?t=2418 | ||
int_sized: <T> concept = requires(t: T) | ||
{ | ||
_ = sizeof(T) == 4; // Hopefully obviously wrong. Should be `requires (sizeof(T) == 4);`. | ||
}; | ||
// Could also be `int_sized: <T> concept = sizeof(T) == 4;`. | ||
|
||
main: () = static_assert(int_sized<char>); |
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,7 @@ | ||
// https://youtu.be/CXn02MPkn8Y?t=2455 | ||
nothrow_incrementable: <T> concept = requires(inout t: T) | ||
{ | ||
_ = noexcept(t++); // Hopefully obviously wrong. Should be `requires noexcept(t++);` or `{ t++ } !throws;`. | ||
}; | ||
|
||
main: () = { static_assert(nothrow_incrementable<char>); } |
22 changes: 22 additions & 0 deletions
22
regression-tests/pure2-concept-definition-pit-of-success-1.cpp2
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,22 @@ | ||
// https://quuxplusone.github.io/blog/2021/06/09/another-concepts-chest-mimic/ | ||
has_a_but_not_b: <T> concept = requires(t: T) | ||
{ | ||
_ = a(t); | ||
!requires _ = b(t); // In Cpp2, this works and does the correct thing. | ||
}; | ||
|
||
s1: @struct type = { } | ||
s2: @struct type = { } | ||
a: (_: s2) = { } | ||
s3: @struct type = { } | ||
b: (_: s3) = { } | ||
s4: @struct type = { } | ||
a: (_: s4) = { } | ||
b: (_: s4) = { } | ||
|
||
main: () = { | ||
static_assert(!has_a_but_not_b<s1>); // as expected | ||
static_assert(has_a_but_not_b<s2>); // as expected | ||
static_assert(!has_a_but_not_b<s3>); // as expected | ||
static_assert(!has_a_but_not_b<s4>); // pit of success! | ||
} |
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 |
---|---|---|
@@ -1,5 +1,29 @@ | ||
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>; | ||
main: () = { | ||
assert<testing>( arithmetic<i32> ); | ||
assert<testing>( arithmetic<float> ); | ||
|
||
number_difference_t: <T> type == std::type_identity_t<decltype(T() - T())>; | ||
number: <T> concept = std::regular<T> && requires(c: T) | ||
{ | ||
!requires std::iter_reference_t<T>; // Negative requirement. | ||
{c + c} is std::common_with<T>; // Compound requirement. | ||
number_difference_t<T>; // Type requirement. | ||
_ = c - c; // Expression requirement. | ||
requires std::common_with<number_difference_t<T>, T>; // Nested requirement. | ||
}; | ||
|
||
test_nonthrowing_requirements: <T> concept = requires | ||
{ // clang-format off | ||
{ T() } !throws; | ||
{ -T() } !throws, is std::same_as<T>; | ||
}; // clang-format on | ||
|
||
main: () = { | ||
static_assert(arithmetic<i32>); | ||
static_assert(arithmetic<float>); | ||
static_assert(number<i32>); | ||
static_assert(number<float>); | ||
static_assert(number<std::chrono::seconds>); | ||
static_assert(!number<* i32>); | ||
static_assert(!number<std::reverse_iterator<* i32>>); | ||
static_assert(test_nonthrowing_requirements<i32>); | ||
static_assert(!test_nonthrowing_requirements<std::chrono::seconds>); | ||
} |
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
1 change: 1 addition & 0 deletions
1
...n-tests/test-results/msvc-2022-c++latest/pure2-concept-definition-no-pitfall-1.cpp.output
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 @@ | ||
pure2-concept-definition-no-pitfall-1.cpp |
1 change: 1 addition & 0 deletions
1
...n-tests/test-results/msvc-2022-c++latest/pure2-concept-definition-no-pitfall-2.cpp.output
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 @@ | ||
pure2-concept-definition-no-pitfall-2.cpp |
1 change: 1 addition & 0 deletions
1
...n-tests/test-results/msvc-2022-c++latest/pure2-concept-definition-no-pitfall-3.cpp.output
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 @@ | ||
pure2-concept-definition-no-pitfall-3.cpp |
1 change: 1 addition & 0 deletions
1
...sts/test-results/msvc-2022-c++latest/pure2-concept-definition-pit-of-success-1.cpp.output
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 @@ | ||
pure2-concept-definition-pit-of-success-1.cpp |
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/msvc-2022-c++latest/pure2-concept-definition.cpp.output
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pure2-concept-definition.cpp | ||
pure2-concept-definition.cpp2(28): error C2607: static assertion failed | ||
30 changes: 30 additions & 0 deletions
30
regression-tests/test-results/pure2-concept-definition-no-pitfall-1.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,30 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2" | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2" | ||
// https://youtu.be/CXn02MPkn8Y?t=2337 | ||
#line 2 "pure2-concept-definition-no-pitfall-1.cpp2" | ||
template<typename T> concept negatable = requires(T const& t) { | ||
|
||
cpp2::impl::is<T>(-t); | ||
}; // Hopefully obviously wrong. Should be `{ -t } is T;`. | ||
|
||
auto main() -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2" | ||
|
||
#line 7 "pure2-concept-definition-no-pitfall-1.cpp2" | ||
auto main() -> int { static_assert(negatable<char>); } | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-concept-definition-no-pitfall-1.cpp2.output
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,2 @@ | ||
pure2-concept-definition-no-pitfall-1.cpp2... ok (all Cpp2, passes safety checks) | ||
|
31 changes: 31 additions & 0 deletions
31
regression-tests/test-results/pure2-concept-definition-no-pitfall-2.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,31 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2" | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2" | ||
// https://youtu.be/CXn02MPkn8Y?t=2418 | ||
#line 2 "pure2-concept-definition-no-pitfall-2.cpp2" | ||
template<typename T> concept int_sized = requires(T const& t) { | ||
|
||
sizeof(T) == 4; | ||
}; // Hopefully obviously wrong. Should be `requires (sizeof(T) == 4);`. | ||
// Could also be `int_sized: <T> concept = sizeof(T) == 4;`. | ||
|
||
auto main() -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2" | ||
|
||
#line 8 "pure2-concept-definition-no-pitfall-2.cpp2" | ||
auto main() -> int { static_assert(int_sized<char>); } | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-concept-definition-no-pitfall-2.cpp2.output
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,2 @@ | ||
pure2-concept-definition-no-pitfall-2.cpp2... ok (all Cpp2, passes safety checks) | ||
|
30 changes: 30 additions & 0 deletions
30
regression-tests/test-results/pure2-concept-definition-no-pitfall-3.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,30 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2" | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2" | ||
// https://youtu.be/CXn02MPkn8Y?t=2455 | ||
#line 2 "pure2-concept-definition-no-pitfall-3.cpp2" | ||
template<typename T> concept nothrow_incrementable = requires(T& t) { | ||
|
||
noexcept(++t); | ||
}; // Hopefully obviously wrong. Should be `requires noexcept(t++);` or `{ t++ } !throws;`. | ||
|
||
auto main() -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2" | ||
|
||
#line 7 "pure2-concept-definition-no-pitfall-3.cpp2" | ||
auto main() -> int{static_assert(nothrow_incrementable<char>); } | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-concept-definition-no-pitfall-3.cpp2.output
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,2 @@ | ||
pure2-concept-definition-no-pitfall-3.cpp2... ok (all Cpp2, passes safety checks) | ||
|
64 changes: 64 additions & 0 deletions
64
regression-tests/test-results/pure2-concept-definition-pit-of-success-1.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,64 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
|
||
#line 8 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
class s1; | ||
class s2; | ||
|
||
class s3; | ||
|
||
class s4; | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
// https://quuxplusone.github.io/blog/2021/06/09/another-concepts-chest-mimic/ | ||
#line 2 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
template<typename T> concept has_a_but_not_b = requires(T const& t) { | ||
|
||
a(t); | ||
requires !requires { b(t); }; | ||
}; // In Cpp2, this works and does the correct thing. | ||
|
||
class s1 {}; | ||
class s2 {}; | ||
auto a([[maybe_unused]] cpp2::impl::in<s2> unnamed_param_1) -> void; | ||
class s3 {}; | ||
auto b([[maybe_unused]] cpp2::impl::in<s3> unnamed_param_1) -> void; | ||
class s4 {}; | ||
auto a([[maybe_unused]] cpp2::impl::in<s4> unnamed_param_1) -> void; | ||
auto b([[maybe_unused]] cpp2::impl::in<s4> unnamed_param_1) -> void; | ||
|
||
auto main() -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
|
||
#line 10 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
auto a([[maybe_unused]] cpp2::impl::in<s2> unnamed_param_1) -> void{} | ||
|
||
#line 12 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
auto b([[maybe_unused]] cpp2::impl::in<s3> unnamed_param_1) -> void{} | ||
|
||
#line 14 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
auto a([[maybe_unused]] cpp2::impl::in<s4> unnamed_param_1) -> void{} | ||
#line 15 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
auto b([[maybe_unused]] cpp2::impl::in<s4> unnamed_param_1) -> void{} | ||
|
||
#line 17 "pure2-concept-definition-pit-of-success-1.cpp2" | ||
auto main() -> int{ | ||
static_assert(!(has_a_but_not_b<s1>));// as expected | ||
static_assert(has_a_but_not_b<s2>); // as expected | ||
static_assert(!(has_a_but_not_b<s3>));// as expected | ||
static_assert(!(has_a_but_not_b<s4>));// pit of success! | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-concept-definition-pit-of-success-1.cpp2.output
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,2 @@ | ||
pure2-concept-definition-pit-of-success-1.cpp2... ok (all Cpp2, passes safety checks) | ||
|
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.
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.
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.
This is the only regression test glitch I noticed ... ?
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.
Never noticed, since I don't use MSVC.
Somehow this is failing:
static_assert(!test_nonthrowing_requirements<std::chrono::seconds>);
.