Skip to content

Commit a812dcc

Browse files
committed
BREAKING CHANGE: Change contracts syntax to align with P2661R1
From `[[kind group: expression]]` to `kind<group>(expression)` Sorry for the breaking change to anyone who has been writing contacts. I'm trying to support gaining usage experience with P2961 Reference link: https://wg21.link/p2961r1
1 parent cdf71bd commit a812dcc

24 files changed

+113
-110
lines changed

regression-tests/mixed-bounds-safety-with-assert-2.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ main: () -> int = {
99

1010
add_42_to_subrange: (inout rng:_, start:int, end:int)
1111
= {
12-
[[assert Bounds: 0 <= start]]
13-
[[assert Bounds: end <= rng.ssize()]]
12+
assert<Bounds>( 0 <= start )
13+
assert<Bounds>( end <= rng.ssize() )
1414

1515
count := 0;
1616
for rng

regression-tests/mixed-bounds-safety-with-assert.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ main: () -> int = {
77
}
88

99
print_subrange: (rng:_, start:int, end:int) = {
10-
[[assert Bounds: 0 <= start]]
11-
[[assert Bounds: end <= rng.ssize()]]
10+
assert<Bounds>( 0 <= start )
11+
assert<Bounds>( end <= rng.ssize() )
1212

1313
count := 0;
1414
for rng
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <chrono>
22
using namespace std::chrono_literals;
33
main: () = {
4-
[[assert: 10 as i32 == 10]]
5-
[[assert: 10LL as i32 == 10]]
6-
[[assert: 10s as std::chrono::seconds == 10s]]
4+
assert( 10 as i32 == 10 )
5+
assert( 10LL as i32 == 10 )
6+
assert( 10s as std::chrono::seconds == 10s )
77
}

regression-tests/mixed-captures-in-expressions-and-postconditions.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ main: () -> int = {
1717
vec: std::vector<int> = ();
1818

1919
insert_at: (where: int, val: int)
20-
[[pre: 0 <= where && where <= vec.ssize()]]
21-
[[post: vec.ssize() == vec.ssize()$ + 1]]
20+
pre( 0 <= where && where <= vec.ssize() )
21+
post( vec.ssize() == vec.ssize()$ + 1 )
2222
= {
2323
_ = vec.insert( vec.begin()+where, val );
2424
}

regression-tests/mixed-initialization-safety-1-error.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ fill: (
1010
out x: std::string,
1111
in value: std::string,
1212
in count: int
13-
)
14-
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
13+
)
14+
pre( value.size() >= count, "fill: value must contain at least count elements" )
1515
= {
1616
x = value.substr(0, count);
1717
}

regression-tests/mixed-initialization-safety-2-error.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ fill: (
1010
out x: std::string,
1111
in value: std::string,
1212
in count: int
13-
)
14-
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
13+
)
14+
pre( value.size() >= count, "fill: value must contain at least count elements" )
1515
= {
1616
x = value.substr(0, count);
1717
}

regression-tests/mixed-initialization-safety-3-contract-violation.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fill: (
2020
value: std::string,
2121
count: int
2222
)
23-
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
23+
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
2424
= {
2525
x = value.substr(0, count);
2626
}

regression-tests/mixed-initialization-safety-3.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fill: (
1818
value: std::string,
1919
count: int
2020
)
21-
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
21+
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
2222
= {
2323
x = value.substr(0, count);
2424
}

regression-tests/mixed-postexpression-with-capture.cpp2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ main: () -> int = {
1111

1212
vec: std::vector<int> = ();
1313

14-
insert_at: (where: int, val: int)
15-
[[pre: 0 <= where && where <= vec.ssize()]]
16-
[[post: vec.size() == vec.size()$ + 1]]
14+
insert_at: (where: int, val: int)
15+
pre ( 0 <= where && where <= vec.ssize() )
16+
post( vec.size() == vec.size()$ + 1 )
1717
= {
1818
vec.push_back(val);
1919
}

regression-tests/pure2-bugfix-for-assign-expression-list.cpp2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ main: () = {
22
vec: type == std::vector<int>;
33
v: vec = (0);
44
v = ();
5-
[[assert: v == :vec = ()]]
5+
assert( v == :vec = () )
66
v = (1);
7-
[[assert: v == :vec = (1)]]
7+
assert( v == :vec = (1) )
88
v = (2, 3);
9-
[[assert: v == :vec = (2, 3)]]
9+
assert( v == :vec = (2, 3) )
1010
}

regression-tests/pure2-bugfix-for-non-local-initialization.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ t: @struct type = {
33
this: std::integral_constant<u, :u = (17, 29)>;
44
}
55
main: () = {
6-
[[assert Testing: t::value[0] == 17]]
7-
[[assert Testing: t::value[1] == 29]]
6+
assert<Testing>( t::value[0] == 17 )
7+
assert<Testing>( t::value[1] == 29 )
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>;
22
main: () = {
3-
[[assert Testing: arithmetic<i32>]]
4-
[[assert Testing: arithmetic<float>]]
3+
assert<Testing>( arithmetic<i32> )
4+
assert<Testing>( arithmetic<float> )
55
}

regression-tests/pure2-forward-return.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
first: (forward rng) -> forward _
3-
[[pre Bounds: !std::empty(rng)]]
3+
pre<Bounds>( !std::empty(rng) )
44
=
55
std::begin(rng)*;
66

regression-tests/pure2-print.cpp2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ outer: @print type = {
2929
}
3030

3131
private h: (s: std::string, inout m: std::map<const int,std::string> ) -> std::string
32-
[[pre: m.empty() == false || false]]
33-
[[pre Bounds: 0 < m.ssize() < 100 && true != false]]
32+
pre( m.empty() == false || false, "message" )
33+
pre<Bounds>( 0 < m.ssize() < 100 && true != false )
3434
= {
3535
a := :()={};
3636
b := :()={};
@@ -46,7 +46,7 @@ outer: @print type = {
4646
else if !m.empty() { b(); }
4747
else { c(); }
4848

49-
[[assert: true]]
49+
assert( true )
5050

5151
return :() -> std::string = (s + m[0])$; ();
5252
}

regression-tests/test-results/mixed-captures-in-expressions-and-postconditions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
extern std::vector<int> vec;
2222

2323
auto insert_at(cpp2::in<int> where, cpp2::in<int> val) -> void;
24-
24+
2525

2626
//=== Cpp2 function definitions =================================================
2727

regression-tests/test-results/mixed-postexpression-with-capture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
extern std::vector<int> vec;
2525

2626
auto insert_at(cpp2::in<int> where, cpp2::in<int> val) -> void;
27-
27+
2828

2929
//=== Cpp2 function definitions =================================================
3030

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ requires (true)
122122

123123
#line 34 "pure2-print.cpp2"
124124
{
125-
cpp2::Default.expects(CPP2_UFCS_0(empty, m) == false || false, "");
125+
cpp2::Default.expects(CPP2_UFCS_0(empty, m) == false || false, "message");
126126
cpp2::Bounds.expects([_0 = 0, _1 = CPP2_UFCS_0(ssize, m), _2 = 100]{ return cpp2::cmp_less(_0,_1) && cpp2::cmp_less(_1,_2); }() && true != false, "");
127127
#line 35 "pure2-print.cpp2"
128128
auto a {[]() -> void{}};

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ outer: type =
3232
in s: std::string,
3333
inout m: std::map<const int, std::string>
3434
) -> move std::string
35-
[[pre: m.empty() == false || false]]
36-
[[pre Bounds: 0 < m.ssize() < 100 && true != false]] =
35+
pre( m.empty() == false || false, "message" )
36+
pre<Bounds>( 0 < m.ssize() < 100 && true != false ) =
3737
{
3838
a: = :() =
3939
{
@@ -75,7 +75,7 @@ outer: type =
7575
c();
7676
}
7777
}
78-
[[assert: true]]
78+
assert( true )
7979
return :() -> move std::string = (s + m[0])$;();
8080
}
8181

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ auto main() -> int;
8282

8383
[[nodiscard]] auto name_or_number::is_name() const& -> bool { return _discriminator == 0; }
8484
[[nodiscard]] auto name_or_number::name() const& -> std::string const& {
85-
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
85+
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
8686
[[nodiscard]] auto name_or_number::name() & -> std::string& {
87-
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
87+
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
8888
auto name_or_number::set_name(cpp2::in<std::string> _value) & -> void{if (!(is_name())) {_destroy();std::construct_at(reinterpret_cast<std::string*>(&_storage), _value);}else {*cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)) = _value;}_discriminator = 0;}
8989
auto name_or_number::set_name(auto&& ..._args) & -> void{if (!(is_name())) {_destroy();std::construct_at(reinterpret_cast<std::string*>(&_storage), _args...);}else {*cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)) = std::string{_args...};}_discriminator = 0;}
9090
[[nodiscard]] auto name_or_number::is_num() const& -> bool { return _discriminator == 1; }
9191
[[nodiscard]] auto name_or_number::num() const& -> cpp2::i32 const& {
92-
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32 const*>(&_storage)); }
92+
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32 const*>(&_storage)); }
9393
[[nodiscard]] auto name_or_number::num() & -> cpp2::i32& {
94-
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)); }
94+
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)); }
9595
auto name_or_number::set_num(cpp2::in<cpp2::i32> _value) & -> void{if (!(is_num())) {_destroy();std::construct_at(reinterpret_cast<cpp2::i32*>(&_storage), _value);}else {*cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)) = _value;}_discriminator = 1;}
9696
auto name_or_number::set_num(auto&& ..._args) & -> void{if (!(is_num())) {_destroy();std::construct_at(reinterpret_cast<cpp2::i32*>(&_storage), _args...);}else {*cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)) = cpp2::i32{_args...};}_discriminator = 1;}
9797
auto name_or_number::_destroy() & -> void{
@@ -138,16 +138,16 @@ name_or_number::name_or_number(name_or_number const& that)
138138

139139
template <typename T> [[nodiscard]] auto name_or_other<T>::is_name() const& -> bool { return _discriminator == 0; }
140140
template <typename T> [[nodiscard]] auto name_or_other<T>::name() const& -> std::string const& {
141-
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
141+
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
142142
template <typename T> [[nodiscard]] auto name_or_other<T>::name() & -> std::string& {
143-
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
143+
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
144144
template <typename T> auto name_or_other<T>::set_name(cpp2::in<std::string> _value) & -> void{if (!(is_name())) {_destroy();std::construct_at(reinterpret_cast<std::string*>(&_storage), _value);}else {*cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)) = _value;}_discriminator = 0;}
145145
template <typename T> auto name_or_other<T>::set_name(auto&& ..._args) & -> void{if (!(is_name())) {_destroy();std::construct_at(reinterpret_cast<std::string*>(&_storage), _args...);}else {*cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)) = std::string{_args...};}_discriminator = 0;}
146146
template <typename T> [[nodiscard]] auto name_or_other<T>::is_other() const& -> bool { return _discriminator == 1; }
147147
template <typename T> [[nodiscard]] auto name_or_other<T>::other() const& -> T const& {
148-
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T const*>(&_storage)); }
148+
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T const*>(&_storage)); }
149149
template <typename T> [[nodiscard]] auto name_or_other<T>::other() & -> T& {
150-
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)); }
150+
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)); }
151151
template <typename T> auto name_or_other<T>::set_other(cpp2::in<T> _value) & -> void{if (!(is_other())) {_destroy();std::construct_at(reinterpret_cast<T*>(&_storage), _value);}else {*cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)) = _value;}_discriminator = 1;}
152152
template <typename T> auto name_or_other<T>::set_other(auto&& ..._args) & -> void{if (!(is_other())) {_destroy();std::construct_at(reinterpret_cast<T*>(&_storage), _args...);}else {*cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)) = T{_args...};}_discriminator = 1;}
153153
template <typename T> auto name_or_other<T>::_destroy() & -> void{

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.3.0 Build 8A27:1514
2+
cppfront compiler v0.3.0 Build 8B07:1638
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8A27:1514"
1+
"8B07:1638"

0 commit comments

Comments
 (0)