Skip to content

Commit

Permalink
BREAKING CHANGE: Change contracts syntax to align with P2661R1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hsutter committed Nov 8, 2023
1 parent cdf71bd commit a812dcc
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 110 deletions.
4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert-2.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ main: () -> int = {

add_42_to_subrange: (inout rng:_, start:int, end:int)
= {
[[assert Bounds: 0 <= start]]
[[assert Bounds: end <= rng.ssize()]]
assert<Bounds>( 0 <= start )
assert<Bounds>( end <= rng.ssize() )

count := 0;
for rng
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ main: () -> int = {
}

print_subrange: (rng:_, start:int, end:int) = {
[[assert Bounds: 0 <= start]]
[[assert Bounds: end <= rng.ssize()]]
assert<Bounds>( 0 <= start )
assert<Bounds>( end <= rng.ssize() )

count := 0;
for rng
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-bugfix-for-literal-as-nttp.cpp2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <chrono>
using namespace std::chrono_literals;
main: () = {
[[assert: 10 as i32 == 10]]
[[assert: 10LL as i32 == 10]]
[[assert: 10s as std::chrono::seconds == 10s]]
assert( 10 as i32 == 10 )
assert( 10LL as i32 == 10 )
assert( 10s as std::chrono::seconds == 10s )
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ main: () -> int = {
vec: std::vector<int> = ();

insert_at: (where: int, val: int)
[[pre: 0 <= where && where <= vec.ssize()]]
[[post: vec.ssize() == vec.ssize()$ + 1]]
pre( 0 <= where && where <= vec.ssize() )
post( vec.ssize() == vec.ssize()$ + 1 )
= {
_ = vec.insert( vec.begin()+where, val );
}
4 changes: 2 additions & 2 deletions regression-tests/mixed-initialization-safety-1-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fill: (
out x: std::string,
in value: std::string,
in count: int
)
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
)
pre( value.size() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/mixed-initialization-safety-2-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fill: (
out x: std::string,
in value: std::string,
in count: int
)
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
)
pre( value.size() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fill: (
value: std::string,
count: int
)
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-initialization-safety-3.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fill: (
value: std::string,
count: int
)
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-postexpression-with-capture.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ main: () -> int = {

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

insert_at: (where: int, val: int)
[[pre: 0 <= where && where <= vec.ssize()]]
[[post: vec.size() == vec.size()$ + 1]]
insert_at: (where: int, val: int)
pre ( 0 <= where && where <= vec.ssize() )
post( vec.size() == vec.size()$ + 1 )
= {
vec.push_back(val);
}
6 changes: 3 additions & 3 deletions regression-tests/pure2-bugfix-for-assign-expression-list.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ main: () = {
vec: type == std::vector<int>;
v: vec = (0);
v = ();
[[assert: v == :vec = ()]]
assert( v == :vec = () )
v = (1);
[[assert: v == :vec = (1)]]
assert( v == :vec = (1) )
v = (2, 3);
[[assert: v == :vec = (2, 3)]]
assert( v == :vec = (2, 3) )
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ t: @struct type = {
this: std::integral_constant<u, :u = (17, 29)>;
}
main: () = {
[[assert Testing: t::value[0] == 17]]
[[assert Testing: t::value[1] == 29]]
assert<Testing>( t::value[0] == 17 )
assert<Testing>( t::value[1] == 29 )
}
4 changes: 2 additions & 2 deletions regression-tests/pure2-concept-definition.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>;
main: () = {
[[assert Testing: arithmetic<i32>]]
[[assert Testing: arithmetic<float>]]
assert<Testing>( arithmetic<i32> )
assert<Testing>( arithmetic<float> )
}
2 changes: 1 addition & 1 deletion regression-tests/pure2-forward-return.cpp2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

first: (forward rng) -> forward _
[[pre Bounds: !std::empty(rng)]]
pre<Bounds>( !std::empty(rng) )
=
std::begin(rng)*;

Expand Down
6 changes: 3 additions & 3 deletions regression-tests/pure2-print.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ outer: @print type = {
}

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

[[assert: true]]
assert( true )

return :() -> std::string = (s + m[0])$; ();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
extern std::vector<int> vec;

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


//=== Cpp2 function definitions =================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
extern std::vector<int> vec;

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


//=== Cpp2 function definitions =================================================

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/pure2-print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ requires (true)

#line 34 "pure2-print.cpp2"
{
cpp2::Default.expects(CPP2_UFCS_0(empty, m) == false || false, "");
cpp2::Default.expects(CPP2_UFCS_0(empty, m) == false || false, "message");
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, "");
#line 35 "pure2-print.cpp2"
auto a {[]() -> void{}};
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/test-results/pure2-print.cpp2.output
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ outer: type =
in s: std::string,
inout m: std::map<const int, std::string>
) -> move std::string
[[pre: m.empty() == false || false]]
[[pre Bounds: 0 < m.ssize() < 100 && true != false]] =
pre( m.empty() == false || false, "message" )
pre<Bounds>( 0 < m.ssize() < 100 && true != false ) =
{
a: = :() =
{
Expand Down Expand Up @@ -75,7 +75,7 @@ outer: type =
c();
}
}
[[assert: true]]
assert( true )
return :() -> move std::string = (s + m[0])$;();
}

Expand Down
16 changes: 8 additions & 8 deletions regression-tests/test-results/pure2-union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ auto main() -> int;

[[nodiscard]] auto name_or_number::is_name() const& -> bool { return _discriminator == 0; }
[[nodiscard]] auto name_or_number::name() const& -> std::string const& {
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
[[nodiscard]] auto name_or_number::name() & -> std::string& {
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
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;}
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;}
[[nodiscard]] auto name_or_number::is_num() const& -> bool { return _discriminator == 1; }
[[nodiscard]] auto name_or_number::num() const& -> cpp2::i32 const& {
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32 const*>(&_storage)); }
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32 const*>(&_storage)); }
[[nodiscard]] auto name_or_number::num() & -> cpp2::i32& {
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)); }
cpp2::Default.expects(is_num(), "");return *cpp2::assert_not_null(reinterpret_cast<cpp2::i32*>(&_storage)); }
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;}
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;}
auto name_or_number::_destroy() & -> void{
Expand Down Expand Up @@ -138,16 +138,16 @@ name_or_number::name_or_number(name_or_number const& that)

template <typename T> [[nodiscard]] auto name_or_other<T>::is_name() const& -> bool { return _discriminator == 0; }
template <typename T> [[nodiscard]] auto name_or_other<T>::name() const& -> std::string const& {
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string const*>(&_storage)); }
template <typename T> [[nodiscard]] auto name_or_other<T>::name() & -> std::string& {
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
cpp2::Default.expects(is_name(), "");return *cpp2::assert_not_null(reinterpret_cast<std::string*>(&_storage)); }
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;}
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;}
template <typename T> [[nodiscard]] auto name_or_other<T>::is_other() const& -> bool { return _discriminator == 1; }
template <typename T> [[nodiscard]] auto name_or_other<T>::other() const& -> T const& {
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T const*>(&_storage)); }
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T const*>(&_storage)); }
template <typename T> [[nodiscard]] auto name_or_other<T>::other() & -> T& {
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)); }
cpp2::Default.expects(is_other(), "");return *cpp2::assert_not_null(reinterpret_cast<T*>(&_storage)); }
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;}
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;}
template <typename T> auto name_or_other<T>::_destroy() & -> void{
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8A27:1514"
"8B07:1638"
Loading

0 comments on commit a812dcc

Please sign in to comment.