Skip to content

Commit 2c03e41

Browse files
committed
Require '=' for all funcs, default single-expr bodies to -> forward _
Requiring `=` has been a widely-requested change, and I think it does make the code clearer to read. See the Cpp2 code changes in this commit for examples. For example, `f: () expr;` must now be written as `f: () = expr;` For a function whose body is a single expression, the default return type (i.e., if not specified) is now `-> forward _`, which is Cpp1 `-> decltype(auto)` (which, importantly, can deduce a value). With this change, single-expression function bodies without `{ }` are still legal for any function, but as of this commit we have a clearer distinction in their use (which is reflected in the updates to the regression tests and other Cpp2 code in this commit): - It further encourages single-expression function bodies without `{ }` for unnamed function expressions (lambdas), by making more of those cases Do the Right Thing that the programmer intended. - It naturally discourages their overuse for named functions, because it will more often cause a compiler warning or error: - a warning that callers are not using a returned results, when the function is called without using its value - an error that a deduced return type makes the function order-dependent, when the function is called from earlier in the source file... this is because a deduced return types creates a dependency on the body, and it is inherent (not an artifact of either Cpp1 or Cpp2) But for those who liked that style, fear not, usually the answer is to just put the two characters `{ }` around the body... see examples in this commit, which I have to admit most are probably more readable even though I'm one of the ones who liked omitting the braces. Note: I realize that further evolution may not allow the `{ }`-free style for named functions. That may well be a reasonable further outcome. I think both of these are positive improvements.
1 parent 4f399a7 commit 2c03e41

File tree

90 files changed

+763
-585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+763
-585
lines changed

include/cpp2regex.h

+66-66
Large diffs are not rendered by default.

include/cpp2regex.h2

+33-33
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ match_context: <CharT, Iter, max_groups: int> type =
7474

7575
// Getter and setter for groups
7676
//
77-
get_group: (in this, group) groups[group];
77+
get_group: (in this, group) = groups[group];
7878

7979
get_group_end: (in this, group) -> int = {
8080
if group >= max_groups || !groups[group].matched {
@@ -108,20 +108,20 @@ match_context: <CharT, Iter, max_groups: int> type =
108108
groups[group].start = pos;
109109
}
110110

111-
size: (in this) max_groups;
111+
size: (in this) = max_groups;
112112

113113
// Misc functions
114114
//
115-
fail: (in this) match_return<Iter>(false, end);
116-
pass: (in this, cur: Iter) match_return<Iter>(true, cur);
115+
fail: (in this) = match_return<Iter>(false, end);
116+
pass: (in this, cur: Iter) = match_return<Iter>(true, cur);
117117
}
118118

119119

120120
// End function that returns a valid match.
121121
//
122122
true_end_func: @struct<noforward> type =
123123
{
124-
operator(): (in this, cur, inout ctx) ctx..pass(cur);
124+
operator(): (in this, cur, inout ctx) = ctx..pass(cur);
125125
}
126126

127127

@@ -149,7 +149,7 @@ on_return: <Func> type =
149149

150150

151151
// Helper for auto deduction of the Func type.
152-
make_on_return: <Func> (func: Func) on_return<Func>(func);
152+
make_on_return: <Func> (func: Func) = on_return<Func>(func);
153153

154154

155155
//-----------------------------------------------------------------------
@@ -163,60 +163,60 @@ make_on_return: <Func> (func: Func) on_return<Func>(func);
163163
//
164164
single_class_entry: <CharT, C: CharT> type =
165165
{
166-
includes : (c: CharT) c == C;
167-
to_string: () bstring<CharT>(1, C);
166+
includes : (c: CharT) = c == C;
167+
to_string: () = bstring<CharT>(1, C);
168168
}
169169

170170

171171
// Class syntax: - Example: a-c
172172
//
173173
range_class_entry: <CharT, Start: CharT, End: CharT> type =
174174
{
175-
includes : (c: CharT) Start <= c <= End;
176-
to_string: () "(Start)$-(End)$";
175+
includes : (c: CharT) = Start <= c <= End;
176+
to_string: () = "(Start)$-(End)$";
177177
}
178178

179179

180180
// Helper for combining two character classes
181181
//
182182
combined_class_entry: <CharT, List ...> type =
183183
{
184-
includes : (c: CharT) (false || ... || List::includes(c));
185-
to_string: () (bstring<CharT>() + ... + List::to_string());
184+
includes : (c: CharT) = (false || ... || List::includes(c));
185+
to_string: () = (bstring<CharT>() + ... + List::to_string());
186186
}
187187

188188

189189
// Class syntax: <list of characters> Example: abcd
190190
//
191191
list_class_entry: <CharT, List ... : CharT> type =
192192
{
193-
includes : (c: CharT) (false || ... || (List == c));
194-
to_string: () (bstring<CharT>() + ... + List);
193+
includes : (c: CharT) = (false || ... || (List == c));
194+
to_string: () = (bstring<CharT>() + ... + List);
195195
}
196196

197197

198198
// Class syntax: [:<class name:] Example: [:alnum:]
199199
//
200200
named_class_entry: <CharT, Name: string_util::fixed_string, Inner> type =
201201
{
202-
includes : (c: CharT) Inner::includes(c);
203-
to_string: () "[:(Name..data())$:]";
202+
includes : (c: CharT) = Inner::includes(c);
203+
to_string: () = "[:(Name..data())$:]";
204204
}
205205

206206

207207
negated_class_entry: <CharT, Inner> type =
208208
{
209209
this : Inner = ();
210-
includes: (c: CharT) !Inner::includes(c);
210+
includes: (c: CharT) = !Inner::includes(c);
211211
}
212212

213213

214214
// Short class syntax: \<character> Example: \w
215215
//
216216
shorthand_class_entry: <CharT, Name: string_util::fixed_string, Inner> type =
217217
{
218-
includes : (c: CharT) Inner::includes(c);
219-
to_string: () Name..str();
218+
includes : (c: CharT) = Inner::includes(c);
219+
to_string: () = Name..str();
220220
}
221221

222222

@@ -692,14 +692,14 @@ regular_expression: <CharT, matcher_wrapper> type =
692692
pos = unsafe_narrow<int>(std::distance(ctx_.begin, pos_));
693693
}
694694

695-
group_number: (this) ctx..size();
696-
group: (this, g: int) ctx..get_group_string(g);
697-
group_start: (this, g: int) ctx..get_group_start(g);
698-
group_end: (this, g: int) ctx..get_group_end(g);
695+
group_number: (this) = ctx..size();
696+
group: (this, g: int) = ctx..get_group_string(g);
697+
group_start: (this, g: int) = ctx..get_group_start(g);
698+
group_end: (this, g: int) = ctx..get_group_end(g);
699699

700-
group: (this, g: bstring<CharT>) group(get_group_id(g));
701-
group_start: (this, g: bstring<CharT>) group_start(get_group_id(g));
702-
group_end: (this, g: bstring<CharT>) group_end(get_group_id(g));
700+
group: (this, g: bstring<CharT>) = group(get_group_id(g));
701+
group_start: (this, g: bstring<CharT>) = group_start(get_group_id(g));
702+
group_end: (this, g: bstring<CharT>) = group_end(get_group_id(g));
703703

704704
private get_group_id: (this, g: bstring<CharT>) -> _ = {
705705
group_id := matcher<Iter>::get_named_group_index(g);
@@ -710,9 +710,9 @@ regular_expression: <CharT, matcher_wrapper> type =
710710
}
711711
}
712712

713-
match: (in this, str: bview<CharT>) match(str..begin(), str..end());
714-
match: (in this, str: bview<CharT>, start) match(get_iter(str, start), str..end());
715-
match: (in this, str: bview<CharT>, start, length) match(get_iter(str, start), get_iter(str, start + length));
713+
match: (in this, str: bview<CharT>) = match(str..begin(), str..end());
714+
match: (in this, str: bview<CharT>, start) = match(get_iter(str, start), str..end());
715+
match: (in this, str: bview<CharT>, start, length) = match(get_iter(str, start), get_iter(str, start + length));
716716
match: <Iter> (in this, start: Iter, end: Iter) -> search_return<Iter> =
717717
{
718718
ctx: context<Iter> = (start, end);
@@ -721,9 +721,9 @@ regular_expression: <CharT, matcher_wrapper> type =
721721
return search_return<Iter>(r.matched && r.pos == end, ctx, r.pos);
722722
}
723723

724-
search: (in this, str: bview<CharT>) search(str..begin(), str..end());
725-
search: (in this, str: bview<CharT>, start) search(get_iter(str, start), str..end());
726-
search: (in this, str: bview<CharT>, start, length) search(get_iter(str, start), get_iter(str, start + length));
724+
search: (in this, str: bview<CharT>) = search(str..begin(), str..end());
725+
search: (in this, str: bview<CharT>, start) = search(get_iter(str, start), str..end());
726+
search: (in this, str: bview<CharT>, start, length) = search(get_iter(str, start), get_iter(str, start + length));
727727
search: <Iter> (in this, start: Iter, end: Iter) -> search_return<Iter> =
728728
{
729729
ctx: context<Iter> = (start, end);
@@ -744,7 +744,7 @@ regular_expression: <CharT, matcher_wrapper> type =
744744
return search_return<Iter>(r.matched, ctx, r.pos);
745745
}
746746

747-
to_string: (in this) matcher_wrapper::to_string();
747+
to_string: (in this) = matcher_wrapper::to_string();
748748

749749
// Helper functions
750750
//

regression-tests/mixed-bugfix-for-ufcs-non-local.cpp2

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ d: _ == t<o.f()>(); // Fails on Clang 12 (lambda in unevaluated context).
3838

3939
u: @struct type = {
4040
b: bool == o.f();
41-
c: bool == :(x: decltype(o.f())) x;(true); // Fails on Clang 12 (lambda in unevaluated context).
41+
c: bool == :(forward x: decltype(f(o))) = x;(true); // Fails on Clang 12 (lambda in unevaluated context).
4242
g: (s, sz) pre(s.sz() != 0) = { }
4343
}
4444

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ fill: (
2525
x = value.substr(0, count);
2626
}
2727

28-
print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";
28+
print_decorated: (x:_) = {
29+
std::cout << ">> [" << x << "]\n";
30+
}
2931

3032
// for test determinism, force "fill" branch
3133
bool flip_a_coin() {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ fill: (
2323
x = value.substr(0, count);
2424
}
2525

26-
print_decorated: (x:_) = std::cout << ">> [" << x << "]\n";
26+
print_decorated: (x:_) = {
27+
std::cout << ">> [" << x << "]\n";
28+
}
2729

2830
// for test determinism, force "xyzzy" branch
2931
// the standard mandates that std::mt19937()() == 3499211612

regression-tests/mixed-is-as-variant.cpp2

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
test: (forward v) = {
22
std::cout << "v is empty = (v is void)$" << std::endl;
33
std::cout << "v is std::monostate = (v is std::monostate)$" << std::endl;
4-
std::cout << "v is X< 0> = (v is X< 0>)$,\t(v as X< 1>) = " << expect_no_throw(forward v, :(forward v) v as X<0>) << std::endl;
4+
std::cout << "v is X< 0> = (v is X< 0>)$,\t(v as X< 1>) = " << expect_no_throw(forward v, :(forward v) = v as X<0>) << std::endl;
55
std::cout << "v is X< 1> = (v is X< 1>)$,\t(v as X< 1>).to_string() = (expect_no_throw(forward v, :(forward v) -> std::string = { return (v as X< 1>).to_string();}))$" << std::endl;
66
std::cout << "v is X<19> = (v is X<19>)$,\t(v as X<19>).to_string() = (expect_no_throw(forward v, :(forward v) -> std::string = { return (v as X<19>).to_string();}))$" << std::endl;
7-
std::cout << "v is X<20> = (v is X<20>)$,\t(v as X<20>) = " << expect_no_throw(forward v, :(forward v) v as X<20>) << std::endl;
7+
std::cout << "v is X<20> = (v is X<20>)$,\t(v as X<20>) = " << expect_no_throw(forward v, :(forward v) = v as X<20>) << std::endl;
88
std::cout << std::endl;
99
}
1010

regression-tests/mixed-lifetime-safety-pointer-init-4.cpp2

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ main: () -> int = {
1818
print_and_decorate( p* );
1919
}
2020

21-
print_and_decorate: (thing:_) =
22-
std::cout << ">> " << thing << "\n";
21+
print_and_decorate: (thing:_) = {
22+
std::cout << ">> " << thing << "\n";
23+
}
2324

2425
bool flip_a_coin() {
2526
// Change std::mt19937 to std::random_device for non-deterministic PRNG

regression-tests/mixed-multiple-return-values.cpp2

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ f: () -> (i: int, s: std::string) = {
1919
return;
2020
}
2121

22-
do_print: (name: std::string, value:_)
23-
= std::cout << name << " is " << value << "\n";
22+
do_print: (name: std::string, value:_) = {
23+
std::cout << name << " is " << value << "\n";
24+
}
2425

2526
int main() {
2627
auto [a,b] = f();

regression-tests/mixed-type-safety-1.cpp2

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class Circle : public Shape { };
99
class Square : public Shape { };
1010

1111

12-
print: <T : type> ( msg: std::string, x: T ) =
12+
print: <T : type> ( msg: std::string, x: T ) = {
1313
std::cout << "(msg)$ (x)$\n";
14+
}
1415

1516
main: () -> int =
1617
{

regression-tests/pure2-bounds-safety-span.cpp2

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ main: () -> int
1212
}
1313
}
1414

15-
print_and_decorate: (thing:_) =
15+
print_and_decorate: (thing:_) = {
1616
std::cout << ">> " << thing << "\n";
17+
}
1718

regression-tests/pure2-bugfix-for-ufcs-arguments.cpp2

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ A: @struct type = {
6767

6868
B: @struct type = {
6969
m: A;
70-
f: (this) = m.f();
70+
f: (this) = { m.f(); }
7171
}

regression-tests/pure2-bugfix-for-unbraced-function-expression.cpp2

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ main: () -> int = {
66
(x := t()) { x[:() -> _ = 0]; }
77
(x := t()) { x[:() -> _ = 0;]; }
88

9-
assert(!(:() 0; is int));
9+
assert(!(:() = 0; is int));
1010

1111
return :i32 = 0;
1212
}

regression-tests/pure2-default-arguments.cpp2

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ main: (args) = {
5656
m2: std::map<int, int> = ();
5757
m2[1] = 22;
5858

59-
combine_maps( m1, m2, :(x,y) x+y+33 );
59+
combine_maps( m1, m2, :(x,y) = x+y+33 );
6060

6161
std::cout << "(m1.size())$, (m2.size())$, (m1[1])$\n";
6262
}

regression-tests/pure2-for-loop-range-with-lambda.cpp2

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ main: (args) = {
1313
}
1414

1515
// Used to cause Error
16-
for ints.first(:(x) x) do (i) {
16+
for ints.first(:(forward x) = x) do (i) {
1717
std::cout << i;
1818
}
1919

2020
// OK
21-
temp := ints.first(:(x) x);
21+
temp := ints.first(:(forward x) = x);
2222
for temp do (i) {
2323
std::cout << i;
2424
}
2525

26-
for :() args$;() do (i) _ = i;
27-
for :(x) x;(args) do (j) _ = j;
28-
for :(x) x;(args) next _ = :() args$;() do (k) _ = k;
29-
for :(x) x;(args) next _ = :(x) x;(args) do (l) _ = l;
26+
for :( ) = args$;() do (i) _ = i;
27+
for :(forward x) = x;(args) do (j) _ = j;
28+
for :(forward x) = x;(args) next _ = :( ) = args$;() do (k) _ = k;
29+
for :(forward x) = x;(args) next _ = :(forward x) = x;(args) do (l) _ = l;
3030
}

0 commit comments

Comments
 (0)