Skip to content

Commit 788db5c

Browse files
hsutterzaucy
authored andcommitted
All parameters default to in
This is the way it used to be, and IMO it's better to have the same default everywhere For a time I tried out making `copy` the default for statement/block parameter, because I had been writing several of those in `reflect.h2` (it was really only 7 places though) But putting back the explicit `copy` in 7 places in `reflect.h2` was not bad, and it actually made the code's value flow clearer -- and clear value flow is super important and a foundational principle throughout Cpp2, from parameter directions to mandatory explicit discard to `const` by default
1 parent e6ec953 commit 788db5c

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class item {
3737
#line 9 "pure2-interpolation.cpp2"
3838
[[nodiscard]] auto main() -> int{
3939
{
40-
auto x = 0;
40+
auto const& x = 0;
4141

4242
#line 11 "pure2-interpolation.cpp2"
4343
{
@@ -55,13 +55,13 @@ auto x = 0;
5555
}
5656
}
5757
{
58-
auto x = item();
58+
auto const& x = item();
5959

6060
#line 25 "pure2-interpolation.cpp2"
6161
{
6262
std::cout << std::left << std::setw(20) << CPP2_UFCS_0(name, x) << " color " << std::left << std::setw(10) << CPP2_UFCS_0(color, x) << " price " << std::setw(10) << std::setprecision(3) << CPP2_UFCS_0(price, x) << " in stock = " << std::boolalpha << (cpp2::cmp_greater(CPP2_UFCS_0(count, x),0)) << "\n";
6363

64-
std::cout << cpp2::to_string(CPP2_UFCS_0(name, x), "{:20}") + " color " + cpp2::to_string(CPP2_UFCS_0(color, x), "{:10}") + " price " + cpp2::to_string(CPP2_UFCS_0(price, x), "{: <10.2f}") + " in stock = " + cpp2::to_string(cpp2::cmp_greater(CPP2_UFCS_0(count, std::move(x)),0)) + "\n";
64+
std::cout << cpp2::to_string(CPP2_UFCS_0(name, x), "{:20}") + " color " + cpp2::to_string(CPP2_UFCS_0(color, x), "{:10}") + " price " + cpp2::to_string(CPP2_UFCS_0(price, x), "{: <10.2f}") + " in stock = " + cpp2::to_string(cpp2::cmp_greater(CPP2_UFCS_0(count, x),0)) + "\n";
6565
}
6666
}
6767

regression-tests/test-results/pure2-statement-scope-parameters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ auto main(int const argc_, char** argv_) -> int{
2424
#line 3 "pure2-statement-scope-parameters.cpp2"
2525
auto local_int {42};
2626
{
27-
auto i = local_int;
27+
auto const& i = local_int;
2828

2929
// 'in' (read-only) statement scope variable
3030
#line 6 "pure2-statement-scope-parameters.cpp2"

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.2.1 Build 8A01:0916
2+
cppfront compiler v0.2.1 Build 8A01:1011
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-
"8A01:0916"
1+
"8A01:1011"

source/parse.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7112,8 +7112,7 @@ class parser
71127112

71137113
auto n = std::make_unique<parameter_declaration_node>();
71147114
n->pass =
7115-
is_returns ? passing_style::out :
7116-
is_statement ? passing_style::copy :
7115+
is_returns ? passing_style::out :
71177116
passing_style::in;
71187117
n->pos = curr().position();
71197118

source/reflect.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class alias_declaration;
3838
#line 827 "reflect.h2"
3939
class value_member_info;
4040

41-
#line 1281 "reflect.h2"
41+
#line 1280 "reflect.h2"
4242
}
4343

4444
}
@@ -693,14 +693,14 @@ auto flag_enum(meta::type_declaration& t) -> void;
693693

694694
auto cpp2_union(meta::type_declaration& t) -> void;
695695

696-
#line 1175 "reflect.h2"
696+
#line 1174 "reflect.h2"
697697
//-----------------------------------------------------------------------
698698
//
699699
// print - output a pretty-printed visualization of t
700700
//
701701
auto print(cpp2::in<meta::type_declaration> t) -> void;
702702

703-
#line 1185 "reflect.h2"
703+
#line 1184 "reflect.h2"
704704
//-----------------------------------------------------------------------
705705
//
706706
// apply_metafunctions
@@ -711,7 +711,7 @@ auto print(cpp2::in<meta::type_declaration> t) -> void;
711711
auto const& error
712712
) -> bool;
713713

714-
#line 1281 "reflect.h2"
714+
#line 1280 "reflect.h2"
715715
}
716716

717717
}
@@ -1552,8 +1552,8 @@ auto value = 0;
15521552
#line 1086 "reflect.h2"
15531553
for (
15541554

1555-
auto const& m : CPP2_UFCS_0(get_members, t) ) { do
1556-
if ( CPP2_UFCS_0(is_member_object, m))
1555+
auto const& m : CPP2_UFCS_0(get_members, t) ) { do
1556+
if ( CPP2_UFCS_0(is_member_object, m))
15571557
{
15581558
CPP2_UFCS(require, m, CPP2_UFCS_0(is_public, m) || CPP2_UFCS_0(is_default_access, m),
15591559
"a union alternative cannot be protected or private");
@@ -1599,22 +1599,22 @@ std::string storage = " _storage: std::aligned_storage_t<cpp2::max( ";
15991599
{
16001600
std::string comma = "";
16011601

1602-
#line 1129 "reflect.h2"
1602+
#line 1128 "reflect.h2"
16031603
for (
16041604

16051605
auto const& e : alternatives ) { do {
16061606
storage += comma + "sizeof(" + cpp2::to_string(e.type) + ")";
16071607
} while (false); comma = ", "; }
16081608
}
16091609

1610-
#line 1135 "reflect.h2"
1610+
#line 1134 "reflect.h2"
16111611
storage += " )> = ();\n";
16121612
CPP2_UFCS(add_member, t, std::move(storage));
16131613
}
16141614
}
16151615

16161616
// Provide discriminator
1617-
#line 1140 "reflect.h2"
1617+
#line 1139 "reflect.h2"
16181618
CPP2_UFCS(add_member, t, " _discriminator: " + cpp2::to_string(std::move(discriminator_type)) + " = -1;\n");
16191619

16201620
// Add the alternatives: is_alternative, get_alternative, and set_alternative
@@ -1636,7 +1636,7 @@ std::string destroy = " private destroy: (inout this) = {\n";
16361636

16371637
// Add destroy
16381638

1639-
#line 1159 "reflect.h2"
1639+
#line 1158 "reflect.h2"
16401640
{
16411641
for (
16421642
auto const& a : alternatives ) {
@@ -1650,17 +1650,17 @@ std::string destroy = " private destroy: (inout this) = {\n";
16501650
}
16511651

16521652
// Add the destructor
1653-
#line 1171 "reflect.h2"
1653+
#line 1170 "reflect.h2"
16541654
CPP2_UFCS(add_member, t, " operator=: (move this) = { destroy(); } ");
16551655
}
16561656

1657-
#line 1179 "reflect.h2"
1657+
#line 1178 "reflect.h2"
16581658
auto print(cpp2::in<meta::type_declaration> t) -> void
16591659
{
16601660
std::cout << CPP2_UFCS_0(print, t) << "\n";
16611661
}
16621662

1663-
#line 1189 "reflect.h2"
1663+
#line 1188 "reflect.h2"
16641664
[[nodiscard]] auto apply_metafunctions(
16651665
declaration_node& n,
16661666
type_declaration& rtype,
@@ -1752,7 +1752,7 @@ auto print(cpp2::in<meta::type_declaration> t) -> void
17521752
return true;
17531753
}
17541754

1755-
#line 1281 "reflect.h2"
1755+
#line 1280 "reflect.h2"
17561756
}
17571757

17581758
}

source/reflect.h2

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ compiler_services: @polymorphic_base @copyable type =
8282

8383
// First split this string into source_lines
8484
//
85-
(newline_pos := source.find('\n'))
85+
(copy newline_pos := source.find('\n'))
8686
if source.ssize() > 1
8787
&& newline_pos != source.npos
8888
{
@@ -847,7 +847,7 @@ basic_enum: (
847847

848848
found_non_numeric := false;
849849

850-
(value: std::string = "-1")
850+
(copy value: std::string = "-1")
851851
for t.get_members()
852852
do (m)
853853
if m.is_member_object()
@@ -960,7 +960,7 @@ basic_enum: (
960960
}
961961

962962
// Provide a 'to_string' function to print enumerator name(s)
963-
(to_string: std::string = " to_string: (this) -> std::string = { \n")
963+
(copy to_string: std::string = " to_string: (this) -> std::string = { \n")
964964
{
965965
if bitwise {
966966
to_string += " ret : std::string = \"(\";\n";
@@ -1082,11 +1082,11 @@ union: (inout t : meta::type_declaration)
10821082

10831083
// 1. Gather: All the user-written members, and find/compute the max size
10841084

1085-
(value := 0)
1086-
for t.get_members()
1087-
next value++
1088-
do (m)
1089-
if m.is_member_object()
1085+
(copy value := 0)
1086+
for t.get_members()
1087+
next value++
1088+
do (m)
1089+
if m.is_member_object()
10901090
{
10911091
m.require( m.is_public() || m.is_default_access(),
10921092
"a union alternative cannot be protected or private");
@@ -1122,10 +1122,9 @@ union: (inout t : meta::type_declaration)
11221122
t.remove_marked_members();
11231123

11241124
// Provide storage
1125-
(storage: std::string = " _storage: std::aligned_storage_t<cpp2::max( ")
1125+
(copy storage: std::string = " _storage: std::aligned_storage_t<cpp2::max( ")
11261126
{
1127-
1128-
(comma: std::string = "")
1127+
(copy comma: std::string = "")
11291128
for alternatives
11301129
next comma = ", "
11311130
do (e) {
@@ -1155,7 +1154,7 @@ union: (inout t : meta::type_declaration)
11551154
}
11561155

11571156
// Add destroy
1158-
(destroy: std::string = " private destroy: (inout this) = {\n")
1157+
(copy destroy: std::string = " private destroy: (inout this) = {\n")
11591158
{
11601159
for alternatives
11611160
do (a) {

0 commit comments

Comments
 (0)