Skip to content

Commit 0eb8cb8

Browse files
committed
feat: recognize requires expressions
1 parent 05ce45a commit 0eb8cb8

29 files changed

+838
-17
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://youtu.be/CXn02MPkn8Y?t=2337
2+
negatable: <T> concept = requires(t: T)
3+
{
4+
_ = -t is T; // Hopefully obviously wrong. Should be `{ -t } is T;`.
5+
};
6+
7+
main: () = static_assert(negatable<char>);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// https://youtu.be/CXn02MPkn8Y?t=2418
2+
int_sized: <T> concept = requires(t: T)
3+
{
4+
_ = sizeof(T) == 4; // Hopefully obviously wrong. Should be `requires (sizeof(T) == 4);`.
5+
};
6+
// Could also be `int_sized: <T> concept = sizeof(T) == 4;`.
7+
8+
main: () = static_assert(int_sized<char>);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://youtu.be/CXn02MPkn8Y?t=2455
2+
nothrow_incrementable: <T> concept = requires(inout t: T)
3+
{
4+
_ = noexcept(t++); // Hopefully obviously wrong. Should be `requires noexcept(t++);` or `{ t++ } !throws;`.
5+
};
6+
7+
main: () = { static_assert(nothrow_incrementable<char>); }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://quuxplusone.github.io/blog/2021/06/09/another-concepts-chest-mimic/
2+
has_a_but_not_b: <T> concept = requires(t: T)
3+
{
4+
_ = a(t);
5+
!requires _ = b(t); // In Cpp2, this works and does the correct thing.
6+
};
7+
8+
s1: @struct type = { }
9+
s2: @struct type = { }
10+
a: (_: s2) = { }
11+
s3: @struct type = { }
12+
b: (_: s3) = { }
13+
s4: @struct type = { }
14+
a: (_: s4) = { }
15+
b: (_: s4) = { }
16+
17+
main: () = {
18+
static_assert(!has_a_but_not_b<s1>); // as expected
19+
static_assert(has_a_but_not_b<s2>); // as expected
20+
static_assert(!has_a_but_not_b<s3>); // as expected
21+
static_assert(!has_a_but_not_b<s4>); // pit of success!
22+
}
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>;
2-
main: () = {
3-
assert<Testing>( arithmetic<i32> );
4-
assert<Testing>( arithmetic<float> );
2+
number_difference_t: <T> type == std::type_identity_t<decltype(T() - T())>;
3+
number: <T> concept = std::regular<T> && requires(c: T)
4+
{
5+
!requires std::iter_reference_t<T>; // Negative requirement.
6+
{c + c} is std::common_with<T>; // Compound requirement.
7+
number_difference_t<T>; // Type requirement.
8+
_ = c - c; // Expression requirement.
9+
requires std::common_with<number_difference_t<T>, T>; // Nested requirement.
10+
};
11+
test_nonthrowing_requirements: <T> concept = requires
12+
{ // clang-format off
13+
{ T() } !throws;
14+
{ -T() } !throws, is std::same_as<T>;
15+
}; // clang-format on
16+
main: () = {
17+
static_assert(arithmetic<i32>);
18+
static_assert(arithmetic<float>);
19+
static_assert(number<i32>);
20+
static_assert(number<float>);
21+
static_assert(number<std::chrono::seconds>);
22+
static_assert(!number<* i32>);
23+
static_assert(!number<std::reverse_iterator<* i32>>);
24+
static_assert(test_nonthrowing_requirements<i32>);
25+
static_assert(!test_nonthrowing_requirements<std::chrono::seconds>);
526
}

regression-tests/pure2-print.cpp2

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ outer: @print type = {
8484
is 43 = "forty-and-three";
8585
is _ = "default case";
8686
} << "\n";
87+
88+
_ =
89+
requires { std::vector<int>; }
90+
+ requires { _ = 0; }
91+
+ requires {
92+
requires true;
93+
!requires std::vector<int>;
94+
!requires _ = 0;
95+
{ 0 };
96+
{ 0 } !throws;
97+
{ 0 } is std::regular;
98+
{ 0 } !throws, is std::regular;
99+
};
87100
}
88101

89102
x: <Ts...: type> type = {

regression-tests/pure2-requires-clauses.cpp2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ f: <T: type, U: type>
1717

1818
v: <T> const T requires std::same_as<T, i32> = 0;
1919

20+
g: <T> () requires true = { }
21+
g: () (requires { _ = 0; });
22+
2023
main: () = {
2124
_: X<int,int> = ();
2225
std::cout << f<int,int>(2,5);

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-1.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-1.cpp.output

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-2.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-2.cpp.output

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-3.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-no-pitfall-3.cpp.output

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-pit-of-success-1.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-concept-definition-pit-of-success-1.cpp.output

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2"
10+
11+
12+
//=== Cpp2 type definitions and function declarations ===========================
13+
14+
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2"
15+
// https://youtu.be/CXn02MPkn8Y?t=2337
16+
#line 2 "pure2-concept-definition-no-pitfall-1.cpp2"
17+
template<typename T> concept negatable = requires(T const& t) {
18+
19+
cpp2::is<T>(-t);
20+
}; // Hopefully obviously wrong. Should be `{ -t } is T;`.
21+
22+
auto main() -> int;
23+
24+
//=== Cpp2 function definitions =================================================
25+
26+
#line 1 "pure2-concept-definition-no-pitfall-1.cpp2"
27+
28+
#line 7 "pure2-concept-definition-no-pitfall-1.cpp2"
29+
auto main() -> int { static_assert(negatable<char>); }
30+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-concept-definition-no-pitfall-1.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2"
10+
11+
12+
//=== Cpp2 type definitions and function declarations ===========================
13+
14+
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2"
15+
// https://youtu.be/CXn02MPkn8Y?t=2418
16+
#line 2 "pure2-concept-definition-no-pitfall-2.cpp2"
17+
template<typename T> concept int_sized = requires(T const& t) {
18+
19+
sizeof(T) == 4;
20+
}; // Hopefully obviously wrong. Should be `requires (sizeof(T) == 4);`.
21+
// Could also be `int_sized: <T> concept = sizeof(T) == 4;`.
22+
23+
auto main() -> int;
24+
25+
//=== Cpp2 function definitions =================================================
26+
27+
#line 1 "pure2-concept-definition-no-pitfall-2.cpp2"
28+
29+
#line 8 "pure2-concept-definition-no-pitfall-2.cpp2"
30+
auto main() -> int { static_assert(int_sized<char>); }
31+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-concept-definition-no-pitfall-2.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2"
10+
11+
12+
//=== Cpp2 type definitions and function declarations ===========================
13+
14+
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2"
15+
// https://youtu.be/CXn02MPkn8Y?t=2455
16+
#line 2 "pure2-concept-definition-no-pitfall-3.cpp2"
17+
template<typename T> concept nothrow_incrementable = requires(T& t) {
18+
19+
noexcept(++t);
20+
}; // Hopefully obviously wrong. Should be `requires noexcept(t++);` or `{ t++ } !throws;`.
21+
22+
auto main() -> int;
23+
24+
//=== Cpp2 function definitions =================================================
25+
26+
#line 1 "pure2-concept-definition-no-pitfall-3.cpp2"
27+
28+
#line 7 "pure2-concept-definition-no-pitfall-3.cpp2"
29+
auto main() -> int{static_assert(nothrow_incrementable<char>); }
30+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-concept-definition-no-pitfall-3.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2"
10+
11+
#line 8 "pure2-concept-definition-pit-of-success-1.cpp2"
12+
class s1;
13+
class s2;
14+
15+
class s3;
16+
17+
class s4;
18+
19+
20+
//=== Cpp2 type definitions and function declarations ===========================
21+
22+
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2"
23+
// https://quuxplusone.github.io/blog/2021/06/09/another-concepts-chest-mimic/
24+
#line 2 "pure2-concept-definition-pit-of-success-1.cpp2"
25+
template<typename T> concept has_a_but_not_b = requires(T const& t) {
26+
27+
a(t);
28+
requires !requires { b(t); };
29+
}; // In Cpp2, this works and does the correct thing.
30+
31+
class s1 {};
32+
class s2 {};
33+
auto a([[maybe_unused]] cpp2::in<s2> unnamed_param_1) -> void;
34+
class s3 {};
35+
auto b([[maybe_unused]] cpp2::in<s3> unnamed_param_1) -> void;
36+
class s4 {};
37+
auto a([[maybe_unused]] cpp2::in<s4> unnamed_param_1) -> void;
38+
auto b([[maybe_unused]] cpp2::in<s4> unnamed_param_1) -> void;
39+
40+
auto main() -> int;
41+
42+
//=== Cpp2 function definitions =================================================
43+
44+
#line 1 "pure2-concept-definition-pit-of-success-1.cpp2"
45+
46+
#line 10 "pure2-concept-definition-pit-of-success-1.cpp2"
47+
auto a([[maybe_unused]] cpp2::in<s2> unnamed_param_1) -> void{}
48+
49+
auto b([[maybe_unused]] cpp2::in<s3> unnamed_param_1) -> void{}
50+
51+
auto a([[maybe_unused]] cpp2::in<s4> unnamed_param_1) -> void{}
52+
auto b([[maybe_unused]] cpp2::in<s4> unnamed_param_1) -> void{}
53+
54+
auto main() -> int{
55+
static_assert(!(has_a_but_not_b<s1>));// as expected
56+
static_assert(has_a_but_not_b<s2>); // as expected
57+
static_assert(!(has_a_but_not_b<s3>));// as expected
58+
static_assert(!(has_a_but_not_b<s4>));// pit of success!
59+
}
60+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-concept-definition-pit-of-success-1.cpp2... ok (all Cpp2, passes safety checks)
2+

regression-tests/test-results/pure2-concept-definition.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,36 @@
1414
#line 1 "pure2-concept-definition.cpp2"
1515
template<typename T> concept arithmetic = std::integral<T> || std::floating_point<T>;
1616
#line 2 "pure2-concept-definition.cpp2"
17+
template<typename T> using number_difference_t = std::type_identity_t<decltype(T() - T())>;
18+
template<typename T> concept number = std::regular<T> && requires(T const& c) {
19+
20+
requires !requires { typename std::iter_reference_t<T>; };// Negative requirement.
21+
{ c + c } -> std::common_with<T>; // Compound requirement.
22+
typename number_difference_t<T>; // Type requirement.
23+
c - c; // Expression requirement.
24+
requires std::common_with<number_difference_t<T>,T>;
25+
}; // Nested requirement.
26+
template<typename T> concept test_nonthrowing_requirements = requires {
27+
// clang-format off
28+
{ T() } noexcept;
29+
{ -T() } noexcept -> std::same_as<T>;
30+
}; // clang-format on
1731
auto main() -> int;
1832

1933
//=== Cpp2 function definitions =================================================
2034

2135
#line 1 "pure2-concept-definition.cpp2"
2236

23-
#line 2 "pure2-concept-definition.cpp2"
24-
auto main() -> int {
25-
cpp2::Testing.expects(arithmetic<cpp2::i32>, "");
26-
cpp2::Testing.expects(arithmetic<float>, "");
37+
#line 16 "pure2-concept-definition.cpp2"
38+
auto main() -> int{
39+
static_assert(arithmetic<cpp2::i32>);
40+
static_assert(arithmetic<float>);
41+
static_assert(number<cpp2::i32>);
42+
static_assert(number<float>);
43+
static_assert(number<std::chrono::seconds>);
44+
static_assert(!(number<cpp2::i32*>));
45+
static_assert(!(number<std::reverse_iterator<cpp2::i32*>>));
46+
static_assert(test_nonthrowing_requirements<cpp2::i32>);
47+
static_assert(!(test_nonthrowing_requirements<std::chrono::seconds>));
2748
}
2849

regression-tests/test-results/pure2-print.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,27 @@ CPP2_REQUIRES_ ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) ;
5353

5454
public: static auto test() -> void;
5555

56-
#line 89 "pure2-print.cpp2"
56+
#line 102 "pure2-print.cpp2"
5757
public: template<typename ...Ts> class x {
5858
private: std::tuple<Ts...> tup {};
5959
public: x() = default;
6060
public: x(x const&) = delete; /* No 'that' constructor, suppress copy */
6161
public: auto operator=(x const&) -> void = delete;
6262

63-
#line 91 "pure2-print.cpp2"
63+
#line 104 "pure2-print.cpp2"
6464
};
6565

6666
public: template<typename ...Args> static auto print(std::ostream& out, Args const& ...args) -> void
6767
CPP2_REQUIRES_ (cpp2::cmp_greater_eq(sizeof(Args)...,0)) ;
6868

69-
#line 97 "pure2-print.cpp2"
69+
#line 110 "pure2-print.cpp2"
7070
public: template<typename ...Args> [[nodiscard]] static auto all(Args const& ...args) -> bool;
7171
public: outer() = default;
7272
public: outer(outer const&) = delete; /* No 'that' constructor, suppress copy */
7373
public: auto operator=(outer const&) -> void = delete;
7474

7575

76-
#line 100 "pure2-print.cpp2"
76+
#line 113 "pure2-print.cpp2"
7777
};
7878

7979
auto main() -> int;
@@ -173,19 +173,32 @@ requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) {(std::cout << ..
173173
if (cpp2::is(_expr, 43)) { if constexpr( requires{"forty-and-three";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("forty-and-three")),namespace_alias::string> ) return "forty-and-three"; else return namespace_alias::string{}; else return namespace_alias::string{}; }
174174
else return "default case"; }
175175
() << "\n";
176+
177+
static_cast<void>(
178+
requires {typename std::vector<int>;
179+
} + requires {0;
180+
} + requires {
181+
requires true;
182+
requires !requires { typename std::vector<int>; };
183+
requires !requires { 0; };
184+
{ 0 };
185+
{ 0 } noexcept;
186+
{ 0 } -> std::regular;
187+
{ 0 } noexcept -> std::regular;
188+
});
176189
}
177190

178-
#line 93 "pure2-print.cpp2"
191+
#line 106 "pure2-print.cpp2"
179192
template<typename ...Args> auto outer::print(std::ostream& out, Args const& ...args) -> void
180193
requires (cpp2::cmp_greater_eq(sizeof(Args)...,0)) {
181-
#line 94 "pure2-print.cpp2"
194+
#line 107 "pure2-print.cpp2"
182195
(out << ... << args);
183196
}
184197

185198
template<typename ...Args> [[nodiscard]] auto outer::all(Args const& ...args) -> bool {
186199
return (... && args); }
187200

188-
#line 102 "pure2-print.cpp2"
201+
#line 115 "pure2-print.cpp2"
189202
auto main() -> int{
190203
outer::test();
191204
}

regression-tests/test-results/pure2-print.cpp2.output

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ outer: type =
130130
is 43 = "forty-and-three";
131131
is _ = "default case";
132132
} << "\n";
133+
_ = requires { std::vector<int>; } + requires { _ = 0; } + requires {
134+
requires true;
135+
!requires std::vector<int>;
136+
!requires _ = 0;
137+
{ 0 };
138+
{ 0 } !throws;
139+
{ 0 } is std::regular;
140+
{ 0 } !throws, is std::regular;
141+
};
133142
}
134143
x: <Ts...: type> type =
135144
{

0 commit comments

Comments
 (0)