Skip to content

Commit 7fff368

Browse files
JohelEGPhsutter
andauthored
fix(to_cpp1): direct-initialize statement parameter and constexpr variable (#924)
* test: add failing test cases due to copy-initialization * fix(to_cpp1): direct-initialize statement parameter * fix(to_cpp1): direct-initialize `constexpr` variable * test: regenerate results * Reran self-host and regressions Noticed that `reflect.h2` needed to be recompiled -- it works --------- Co-authored-by: Herb Sutter <herb.sutter@gmail.com>
1 parent acab5c3 commit 7fff368

11 files changed

+85
-39
lines changed

regression-tests/pure2-print.cpp2

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ outer: @print type = {
7777

7878
object_alias_1: i8 == 42;
7979
object_alias_2: == 42;
80+
object_alias_3: std::array == (4,5,6);
8081

8182

8283
var: ::outer::mytype = ();

regression-tests/pure2-statement-scope-parameters.cpp2

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ main: (args) = {
1212
i++;
1313
}
1414
std::cout << local_int << "\n"; // prints 43
15+
16+
_: std::chrono::duration<i32> = 0;
17+
(copy _: std::chrono::duration<i32> = 0) { }
18+
_: std::in_place_t = ();
19+
(copy _: std::in_place_t = ()) { }
1520
}

regression-tests/test-results/pure2-bugfix-for-ufcs-name-lookup.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ auto main() -> int{
7878
if (cpp2::Default.has_handler() && !(std::move(f) == 0) ) { cpp2::Default.report_violation(""); }
7979
}
8080
{
81-
auto const& f = t().f();
81+
auto const& f{t().f()};
8282
#line 33 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
8383
if (cpp2::Default.has_handler() && !(f == 0) ) { cpp2::Default.report_violation(""); }
8484
}
8585
{
86-
auto const& f = t().f();
86+
auto const& f{t().f()};
8787
#line 34 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
8888
if (cpp2::Default.has_handler() && !(f == 0) ) { cpp2::Default.report_violation(""); }
8989
}
@@ -113,12 +113,12 @@ auto const& f = t().f();
113113
{
114114
auto f {[]() mutable -> void{
115115
{
116-
cpp2::in<identity> f = identity();
116+
cpp2::in<identity> f{identity()};
117117
#line 59 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
118118
if (cpp2::Default.has_handler() && !(CPP2_UFCS(f)(t()) == 0) ) { cpp2::Default.report_violation(""); }
119119
}
120120
{
121-
cpp2::in<identity> f = identity();
121+
cpp2::in<identity> f{identity()};
122122
#line 60 "pure2-bugfix-for-ufcs-name-lookup.cpp2"
123123
static_cast<void>(CPP2_UFCS(f)(u()));
124124
}

regression-tests/test-results/pure2-bugfix-for-unbraced-function-expression.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ extern cpp2::i32 y;
4040
#line 5 "pure2-bugfix-for-unbraced-function-expression.cpp2"
4141
[[nodiscard]] auto main() -> int{
4242
{
43-
auto const& x = t();
43+
auto const& x{t()};
4444
#line 6 "pure2-bugfix-for-unbraced-function-expression.cpp2"
4545
{CPP2_ASSERT_IN_BOUNDS(x, []() mutable -> auto { return 0; }); }
4646
}
4747
{
48-
auto const& x = t();
48+
auto const& x{t()};
4949
#line 7 "pure2-bugfix-for-unbraced-function-expression.cpp2"
5050
{CPP2_ASSERT_IN_BOUNDS(x, []() mutable -> auto { return 0; }); }
5151
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class item {
3939
#line 9 "pure2-interpolation.cpp2"
4040
[[nodiscard]] auto main() -> int{
4141
{
42-
auto const& x = 0;
42+
auto const& x{0};
4343

4444
#line 11 "pure2-interpolation.cpp2"
4545
{
@@ -57,7 +57,7 @@ auto const& x = 0;
5757
}
5858
}
5959
{
60-
auto const& x = item();
60+
auto const& x{item()};
6161

6262
#line 25 "pure2-interpolation.cpp2"
6363
{

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,27 @@ CPP2_REQUIRES_ ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) ;
5555

5656
public: static auto test() -> void;
5757

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

65-
#line 93 "pure2-print.cpp2"
65+
#line 94 "pure2-print.cpp2"
6666
};
6767

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

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

7777

78-
#line 102 "pure2-print.cpp2"
78+
#line 103 "pure2-print.cpp2"
7979
};
8080

8181
auto main() -> int;
@@ -169,8 +169,9 @@ requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) {(std::cout << ..
169169

170170
cpp2::i8 constexpr object_alias_1 = 42;
171171
auto constexpr object_alias_2 = 42;
172+
std::array constexpr object_alias_3{ 4, 5, 6 };
172173

173-
#line 82 "pure2-print.cpp2"
174+
#line 83 "pure2-print.cpp2"
174175
::outer::mytype var {};
175176
cout << CPP2_UFCS(g)(var, 42) << "\n";
176177

@@ -180,17 +181,17 @@ requires ((std::is_convertible_v<CPP2_TYPEOF(x), int> && ...)) {(std::cout << ..
180181
() << "\n";
181182
}
182183

183-
#line 95 "pure2-print.cpp2"
184+
#line 96 "pure2-print.cpp2"
184185
template<typename ...Args> auto outer::print(std::ostream& out, Args const& ...args) -> void
185186
requires (cpp2::cmp_greater_eq(sizeof...(Args),0u)) {
186-
#line 96 "pure2-print.cpp2"
187+
#line 97 "pure2-print.cpp2"
187188
(out << ... << args);
188189
}
189190

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

193-
#line 104 "pure2-print.cpp2"
194+
#line 105 "pure2-print.cpp2"
194195
auto main() -> int{
195196
outer::test();
196197
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ outer:/* @print */ type =
124124
type_alias: type == array<int, 10>;
125125
object_alias_1: i8 == 42;
126126
object_alias_2: == 42;
127+
object_alias_3: std::array == (4, 5, 6);
127128
var: ::outer::mytype = ();
128129
cout << var.g(42) << "\n";
129130
cout << inspect var.g(42) -> namespace_alias::string {

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ auto main(int const argc_, char** argv_) -> int{
2626
#line 3 "pure2-statement-scope-parameters.cpp2"
2727
auto local_int {42};
2828
{
29-
auto const& i = local_int;
29+
auto const& i{local_int};
3030

3131
// 'in' (read-only) statement scope variable
3232
#line 6 "pure2-statement-scope-parameters.cpp2"
@@ -35,7 +35,7 @@ auto const& i = local_int;
3535
}
3636
}
3737
{
38-
auto& i = local_int;
38+
auto& i{local_int};
3939

4040
// 'inout' (read-write) statement scope variable
4141
#line 11 "pure2-statement-scope-parameters.cpp2"
@@ -45,5 +45,20 @@ auto& i = local_int;
4545
}
4646
#line 14 "pure2-statement-scope-parameters.cpp2"
4747
std::cout << std::move(local_int) << "\n";// prints 43
48+
49+
std::chrono::duration<cpp2::i32> auto_16_5 {0};
50+
{
51+
[[maybe_unused]] std::chrono::duration<cpp2::i32> unnamed_param_1{0};
52+
#line 17 "pure2-statement-scope-parameters.cpp2"
53+
{}
54+
}
55+
#line 18 "pure2-statement-scope-parameters.cpp2"
56+
std::in_place_t auto_18_5 {};
57+
{
58+
[[maybe_unused]] std::in_place_t unnamed_param_1{};
59+
#line 19 "pure2-statement-scope-parameters.cpp2"
60+
{}
61+
}
62+
#line 20 "pure2-statement-scope-parameters.cpp2"
4863
}
4964

regression-tests/test-results/pure2-type-and-namespace-aliases.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace N {
7373
auto myfunc() -> void{
7474
N1::pmr_vec<myclass::str> v {"xyzzy", "plugh"};
7575
{
76-
auto const& v2 = std::move(v);
76+
auto const& v2{std::move(v)};
7777

7878
#line 21 "pure2-type-and-namespace-aliases.cpp2"
7979
for ( auto const& s : v2 )

source/reflect.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ namespace meta {
818818
static_cast<void>(CPP2_UFCS(emplace_back)((*cpp2::assert_not_null(_1)), s, source_line::category::cpp2));
819819
}};
820820
{
821-
auto newline_pos = CPP2_UFCS(find)(source, '\n');
821+
auto newline_pos{CPP2_UFCS(find)(source, '\n')};
822822

823823
// First split this string into source_lines
824824
//
@@ -1483,7 +1483,7 @@ auto basic_enum(
14831483

14841484
auto found_non_numeric {false};
14851485
{
1486-
std::string value = "-1";
1486+
std::string value{"-1"};
14871487

14881488
#line 957 "reflect.h2"
14891489
for (
@@ -1613,7 +1613,7 @@ std::string value = "-1";
16131613
CPP2_UFCS(add_member)(t, " operator= : (out this, that) == { }");
16141614
CPP2_UFCS(add_member)(t, " operator<=> : (this, that) -> std::strong_ordering;");
16151615
{
1616-
std::string to_string = " to_string: (this) -> std::string = { \n";
1616+
std::string to_string{" to_string: (this) -> std::string = { \n"};
16171617

16181618
// Provide a 'to_string' function to print enumerator name(s)
16191619

@@ -1696,7 +1696,7 @@ auto cpp2_union(meta::type_declaration& t) -> void
16961696
{
16971697
std::vector<value_member_info> alternatives {};
16981698
{
1699-
auto value = 0;
1699+
auto value{0};
17001700

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

@@ -1745,14 +1745,14 @@ auto value = 0;
17451745

17461746
CPP2_UFCS(remove_marked_members)(t);
17471747
{
1748-
std::string storage = " _storage: cpp2::aligned_storage<cpp2::max( ";
1748+
std::string storage{" _storage: cpp2::aligned_storage<cpp2::max( "};
17491749

17501750
// Provide storage
17511751

17521752
#line 1250 "reflect.h2"
17531753
{
17541754
{
1755-
std::string comma = "";
1755+
std::string comma{""};
17561756

17571757
#line 1252 "reflect.h2"
17581758
for (
@@ -1765,7 +1765,7 @@ std::string comma = "";
17651765
#line 1258 "reflect.h2"
17661766
storage += "), cpp2::max( ";
17671767
{
1768-
std::string comma = "";
1768+
std::string comma{""};
17691769

17701770
#line 1261 "reflect.h2"
17711771
for (
@@ -1800,7 +1800,7 @@ std::string comma = "";
18001800
CPP2_UFCS(add_member)(t, (" set_" + cpp2::to_string(a.name) + ": (inout this, forward _args...: _) = { if !is_" + cpp2::to_string(a.name) + "() { _destroy(); std::construct_at( reinterpret_cast<*" + cpp2::to_string(a.type) + ">(_storage&), _args...); } else { reinterpret_cast<*" + cpp2::to_string(a.type) + ">(_storage&)* = :" + cpp2::to_string(a.type) + " = (_args...); } _discriminator = " + cpp2::to_string(a.value) + "; }\n"));
18011801
}
18021802
{
1803-
std::string destroy = " private _destroy: (inout this) = {\n";
1803+
std::string destroy{" private _destroy: (inout this) = {\n"};
18041804

18051805
// Add destroy
18061806

@@ -1824,7 +1824,7 @@ std::string destroy = " private _destroy: (inout this) = {\n";
18241824
// Add default constructor
18251825
CPP2_UFCS(add_member)(t, " operator=: (out this) = { }");
18261826
{
1827-
std::string value_set = "";
1827+
std::string value_set{""};
18281828

18291829
// Add copy/move construction and assignment
18301830

source/to_cpp1.h

+34-11
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ auto pad(int padding)
5656
};
5757
}
5858

59-
auto multi_return_type_name(declaration_node const& n)
59+
auto multi_return_type_name(declaration_node const& n)
6060
-> std::string
6161
{
6262
// When generating a multi-return struct, also enable multi-return
@@ -1326,7 +1326,7 @@ class cppfront
13261326
printer.print_extra(
13271327
"\n// Generated by cppfront "
13281328
#include "version.info"
1329-
" build "
1329+
" build "
13301330
#include "build.info"
13311331
);
13321332
}
@@ -4083,7 +4083,7 @@ class cppfront
40834083
printer.print_extra( "{");
40844084
for (auto& param : n.parameters->parameters) {
40854085
printer.print_extra( "\n");
4086-
printer.print_extra( print_to_string(*param) );
4086+
printer.print_extra( print_to_string(*param, false, false, true) );
40874087
}
40884088
}
40894089

@@ -4170,7 +4170,8 @@ class cppfront
41704170
auto emit(
41714171
parameter_declaration_node const& n,
41724172
bool is_returns = false,
4173-
bool is_template_parameter = false
4173+
bool is_template_parameter = false,
4174+
bool is_statement = false
41744175
)
41754176
-> void
41764177
{ STACKINSTR
@@ -4492,8 +4493,16 @@ class cppfront
44924493
)
44934494
{
44944495
auto guard = stack_element(current_declarations, &*n.declaration);
4495-
printer.print_cpp2( " = ", n.declaration->initializer->position() );
4496-
emit(*n.declaration->initializer);
4496+
if (is_statement) {
4497+
printer.print_cpp2( "{", n.declaration->initializer->position() );
4498+
}
4499+
else {
4500+
printer.print_cpp2( " = ", n.declaration->initializer->position() );
4501+
}
4502+
emit(*n.declaration->initializer, !is_statement);
4503+
if (is_statement) {
4504+
printer.print_cpp2( "};", n.declaration->initializer->position() );
4505+
}
44974506
}
44984507
}
44994508

@@ -4679,7 +4688,7 @@ class cppfront
46794688
);
46804689
}
46814690
else {
4682-
emit(*n.parameters, false, false, generating_postfix_inc_dec);
4691+
emit(*n.parameters, false, false, generating_postfix_inc_dec);
46834692
}
46844693

46854694
// For an anonymous function, the emitted lambda is 'constexpr' or 'mutable'
@@ -5511,6 +5520,22 @@ class cppfront
55115520
type = print_to_string(*a->type_id);
55125521
}
55135522

5523+
auto print_initializer_to_string = [&](expression_node const& n) -> std::string {
5524+
// If this expression is just a single expression-list, we can
5525+
// take over direct control of emitting it without needing to
5526+
// go through the whole grammar, and surround it with braces
5527+
if (n.is_expression_list()) {
5528+
return "{ "
5529+
+ print_to_string(*n.get_expression_list(), false)
5530+
+ " }";
5531+
}
5532+
// Otherwise, just emit the general expression as usual
5533+
else {
5534+
return " = "
5535+
+ print_to_string(n);
5536+
}
5537+
};
5538+
55145539
// (*) If this is at type scope, Cpp1 requires an out-of-line declaration dance
55155540
// for some cases to work - see https://stackoverflow.com/questions/11928089/
55165541
if (n.parent_is_type())
@@ -5542,8 +5567,7 @@ class cppfront
55425567
+ " "
55435568
+ type_qualification_if_any_for(n)
55445569
+ print_to_string(*n.identifier)
5545-
+ " = "
5546-
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
5570+
+ print_initializer_to_string( *std::get<alias_node::an_object>(a->initializer) )
55475571
+ ";\n",
55485572
n.position()
55495573
);
@@ -5564,8 +5588,7 @@ class cppfront
55645588
type + " "
55655589
+ intro + " "
55665590
+ print_to_string(*n.identifier)
5567-
+ " = "
5568-
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
5591+
+ print_initializer_to_string( *std::get<alias_node::an_object>(a->initializer) )
55695592
+ ";\n",
55705593
n.position()
55715594
);

0 commit comments

Comments
 (0)