Skip to content

Commit feff640

Browse files
committed
Fix default member initialization in assignment, closes #290
Note this change adds `{` `}` around member assignment statements in assignment operators. That change now triggers Clang's `-Wbraced-scalar-init` warning, but this warning appears to be spurious. See the open Clang issue here llvm/llvm-project#54702 which mentions the same justification I had in mind (disallow implicit narrowing, that's the #1 reason to recommend using `{` `}` init where possible). Unfortunately that Clang issue still open without comments after a year and so it's hard to know the status. (BTW, what a coincidence -- I actually hit this on the exact first anniversary of that issue, which also happens to be April 1 but I swear this is not a joke and I think the LLVM issue wasn't either...)
1 parent a75816b commit feff640

14 files changed

+65
-48
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pure2-types-smf-and-that-1-provide-everything.cpp2:24:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
2+
addr = {"123 Ford Dr."};
3+
^~~~~~~~~~~~~~~~
4+
1 warning generated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2:24:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
2+
addr = {"123 Ford Dr."};
3+
^~~~~~~~~~~~~~~~
4+
1 warning generated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2:24:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
2+
addr = {"123 Ford Dr."};
3+
^~~~~~~~~~~~~~~~
4+
1 warning generated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2:24:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
2+
addr = {"123 Ford Dr."};
3+
^~~~~~~~~~~~~~~~
4+
1 warning generated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2:24:16: warning: braces around scalar initializer [-Wbraced-scalar-init]
2+
addr = {"123 Ford Dr."};
3+
^~~~~~~~~~~~~~~~
4+
1 warning generated.

regression-tests/test-results/pure2-types-basics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ namespace N {
8484
}
8585
#line 6 "pure2-types-basics.cpp2"
8686
auto myclass::operator=(cpp2::in<int> x) -> myclass& {
87-
data = x;
88-
more = std::to_string(42 * 12);
87+
data = {x};
88+
more = {std::to_string(42 * 12)};
8989

9090
#line 9 "pure2-types-basics.cpp2"
9191
std::cout << "myclass: implicit from int\n";
@@ -106,8 +106,8 @@ namespace N {
106106
}
107107
#line 13 "pure2-types-basics.cpp2"
108108
auto myclass::operator=(cpp2::in<std::string> s) -> myclass& {
109-
data = 99;
110-
more = s;
109+
data = {99};
110+
more = {s};
111111

112112
#line 16 "pure2-types-basics.cpp2"
113113
std::cout << "myclass: explicit from string\n";

regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace N {
116116
#line 10 "pure2-types-order-independence-and-nesting.cpp2"
117117
auto X::operator=(cpp2::out<Y> y) -> X& {
118118
y.construct(&(*this));
119-
py = &y.value();
119+
py = {&y.value()};
120120

121121
#line 31 "pure2-types-order-independence-and-nesting.cpp2"
122122
std::cout << "made a safely initialized cycle\n";
@@ -140,7 +140,7 @@ namespace N {
140140
{ }
141141
#line 49 "pure2-types-order-independence-and-nesting.cpp2"
142142
auto Y::operator=(cpp2::in<X*> x) -> Y& {
143-
px = x;
143+
px = {x};
144144
return *this;
145145
#line 49 "pure2-types-order-independence-and-nesting.cpp2"
146146
}

regression-tests/test-results/pure2-types-smf-and-that-1-provide-everything.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ auto main() -> int;
6969
}
7070

7171
auto myclass::operator=(myclass const& that) -> myclass& {
72-
name = that.name;
73-
addr = that.addr + "(AC)";
72+
name = {that.name};
73+
addr = {that.addr + "(AC)"};
7474

7575
#line 15 "pure2-types-smf-and-that-1-provide-everything.cpp2"
7676
std::cout << "assign - copy ";
@@ -79,8 +79,8 @@ auto main() -> int;
7979
}
8080

8181
auto myclass::operator=(myclass&& that) -> myclass& {
82-
name = std::move(that).name;
83-
addr = std::move(that).addr;
82+
name = {std::move(that).name};
83+
addr = {std::move(that).addr};
8484
#line 19 "pure2-types-smf-and-that-1-provide-everything.cpp2"
8585
std::cout << "assign - move ";
8686
return *this;
@@ -96,8 +96,8 @@ auto main() -> int;
9696
}
9797
#line 22 "pure2-types-smf-and-that-1-provide-everything.cpp2"
9898
auto myclass::operator=(cpp2::in<std::string> x) -> myclass& {
99-
name = x;
100-
addr = "123 Ford Dr.";
99+
name = {x};
100+
addr = {"123 Ford Dr."};
101101

102102
#line 24 "pure2-types-smf-and-that-1-provide-everything.cpp2"
103103
std::cout << "ctor - from string ";

regression-tests/test-results/pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ auto main() -> int;
7272
}
7373

7474
auto myclass::operator=(myclass const& that) -> myclass& {
75-
name = that.name;
76-
addr = that.addr + "(AC)";
75+
name = {that.name};
76+
addr = {that.addr + "(AC)"};
7777

7878
#line 15 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
7979
std::cout << "assign - copy ";
@@ -82,8 +82,8 @@ auto main() -> int;
8282
}
8383
#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
8484
auto myclass::operator=(myclass&& that) -> myclass& {
85-
name = std::move(that).name;
86-
addr = std::move(that).addr + "(AC)";
85+
name = {std::move(that).name};
86+
addr = {std::move(that).addr + "(AC)"};
8787

8888
#line 15 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
8989
std::cout << "assign - copy ";
@@ -101,8 +101,8 @@ auto main() -> int;
101101
}
102102
#line 22 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
103103
auto myclass::operator=(cpp2::in<std::string> x) -> myclass& {
104-
name = x;
105-
addr = "123 Ford Dr.";
104+
name = {x};
105+
addr = {"123 Ford Dr."};
106106

107107
#line 24 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
108108
std::cout << "ctor - from string ";

regression-tests/test-results/pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ auto main() -> int;
6464
}
6565
#line 4 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
6666
auto myclass::operator=(myclass const& that) -> myclass& {
67-
name = that.name;
68-
addr = that.addr;
67+
name = {that.name};
68+
addr = {that.addr};
6969
#line 5 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
7070
std::cout << "ctor - copy (GENERAL)";
7171
return *this;
@@ -83,8 +83,8 @@ auto main() -> int;
8383

8484
#line 18 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
8585
auto myclass::operator=(myclass&& that) -> myclass& {
86-
name = std::move(that).name;
87-
addr = std::move(that).addr;
86+
name = {std::move(that).name};
87+
addr = {std::move(that).addr};
8888
#line 19 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
8989
std::cout << "assign - move ";
9090
return *this;
@@ -100,8 +100,8 @@ auto main() -> int;
100100
}
101101
#line 22 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
102102
auto myclass::operator=(cpp2::in<std::string> x) -> myclass& {
103-
name = x;
104-
addr = "123 Ford Dr.";
103+
name = {x};
104+
addr = {"123 Ford Dr."};
105105

106106
#line 24 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
107107
std::cout << "ctor - from string ";

regression-tests/test-results/pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ auto main() -> int;
7373

7474
#line 13 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
7575
auto myclass::operator=(myclass const& that) -> myclass& {
76-
name = that.name;
77-
addr = that.addr + "(AC)";
76+
name = {that.name};
77+
addr = {that.addr + "(AC)"};
7878

7979
#line 15 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
8080
std::cout << "assign - copy ";
@@ -83,8 +83,8 @@ auto main() -> int;
8383
}
8484

8585
auto myclass::operator=(myclass&& that) -> myclass& {
86-
name = std::move(that).name;
87-
addr = std::move(that).addr;
86+
name = {std::move(that).name};
87+
addr = {std::move(that).addr};
8888
#line 19 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
8989
std::cout << "assign - move ";
9090
return *this;
@@ -100,8 +100,8 @@ auto main() -> int;
100100
}
101101
#line 22 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
102102
auto myclass::operator=(cpp2::in<std::string> x) -> myclass& {
103-
name = x;
104-
addr = "123 Ford Dr.";
103+
name = {x};
104+
addr = {"123 Ford Dr."};
105105

106106
#line 24 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
107107
std::cout << "ctor - from string ";

regression-tests/test-results/pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ auto main() -> int;
7171
}
7272
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
7373
auto myclass::operator=(myclass const& that) -> myclass& {
74-
name = that.name;
75-
addr = that.addr;
74+
name = {that.name};
75+
addr = {that.addr};
7676
#line 5 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
7777
std::cout << "ctor - copy (GENERAL)";
7878
return *this;
@@ -88,8 +88,8 @@ auto main() -> int;
8888
}
8989
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
9090
auto myclass::operator=(myclass&& that) -> myclass& {
91-
name = std::move(that).name;
92-
addr = std::move(that).addr;
91+
name = {std::move(that).name};
92+
addr = {std::move(that).addr};
9393
#line 5 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
9494
std::cout << "ctor - copy (GENERAL)";
9595
return *this;
@@ -106,8 +106,8 @@ auto main() -> int;
106106
}
107107
#line 22 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
108108
auto myclass::operator=(cpp2::in<std::string> x) -> myclass& {
109-
name = x;
110-
addr = "123 Ford Dr.";
109+
name = {x};
110+
addr = {"123 Ford Dr."};
111111

112112
#line 24 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
113113
std::cout << "ctor - from string ";

regression-tests/test-results/pure2-types-that-parameters.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ auto main() -> int;
5454
}
5555
#line 6 "pure2-types-that-parameters.cpp2"
5656
auto myclass::operator=(myclass const& that) -> myclass& {
57-
name = that.name;
58-
addr = that.addr;
57+
name = {that.name};
58+
addr = {that.addr};
5959
return *this;
6060

6161
#line 9 "pure2-types-that-parameters.cpp2"
@@ -71,8 +71,8 @@ auto main() -> int;
7171
}
7272
#line 11 "pure2-types-that-parameters.cpp2"
7373
auto myclass::operator=(myclass&& that) -> myclass& {
74-
name = std::move(that).name;
75-
addr = std::move(that).addr;
74+
name = {std::move(that).name};
75+
addr = {std::move(that).addr};
7676
return *this;
7777

7878
#line 14 "pure2-types-that-parameters.cpp2"

source/cppfront.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,11 +4288,10 @@ class cppfront
42884288

42894289
current_functions.back().prolog.statements.push_back(
42904290
(*object)->name()->to_string(true) +
4291-
" = " +
4291+
" = {" +
42924292
initializer +
4293-
";"
4293+
"};"
42944294
);
4295-
separator = ", ";
42964295
}
42974296
// (b) ... if this isn't assignment, only need to emit it if it was
42984297
// explicit or is a 'that' initializer
@@ -4356,14 +4355,12 @@ class cppfront
43564355
if (is_assignment)
43574356
{
43584357
auto initializer = print_to_string( *(*object)->initializer, false );
4359-
current_functions.back().prolog.mem_inits.push_back(
4360-
separator +
4358+
current_functions.back().prolog.statements.push_back(
43614359
(*object)->name()->to_string(true) +
4362-
"{ " +
4360+
" = {" +
43634361
initializer +
4364-
" }"
4362+
"};"
43654363
);
4366-
separator = ", ";
43674364
}
43684365
}
43694366
else

0 commit comments

Comments
 (0)