Skip to content

Commit 4dff91c

Browse files
committed
fix(cpp1): support assignment from expression list
1 parent 52a2798 commit 4dff91c

11 files changed

+63
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
main: () = {
2+
vec: type == std::vector<int>;
3+
v: vec = (0);
4+
v = ();
5+
[[assert: v == :vec = ()]]
6+
v = (1);
7+
[[assert: v == :vec = (1)]]
8+
v = (2, 3);
9+
[[assert: v == :vec = (2, 3)]]
10+
}

regression-tests/test-results/gcc-13/pure2-bugfix-for-assign-expression-list.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-bugfix-for-assign-expression-list.cpp.output

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"
14+
auto main() -> int;
15+
16+
17+
//=== Cpp2 function definitions =================================================
18+
19+
#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"
20+
auto main() -> int{
21+
using vec = std::vector<int>;
22+
vec v {0};
23+
v = { };
24+
cpp2::Default.expects(v==vec{}, "");
25+
v = { 1 };
26+
cpp2::Default.expects(v==vec{1}, "");
27+
v = { 2, 3 };
28+
cpp2::Default.expects(std::move(v)==vec{2, 3}, "");
29+
}
30+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-assign-expression-list.cpp2... ok (all Cpp2, passes safety checks)
2+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ auto main() -> int{
143143
CPP2_UFCS(print, z, " cp-assign ", " <- ");
144144
CPP2_UFCS(print, y, "", "\n");
145145

146-
z = std::move(y);
146+
z = { std::move(y) };
147147
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
148148
CPP2_UFCS(print, std::move(y), "", "\n");
149149
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ auto main() -> int{
148148
CPP2_UFCS(print, z, " cp-assign ", " <- ");
149149
CPP2_UFCS(print, y, "", "\n");
150150

151-
z = std::move(y);
151+
z = { std::move(y) };
152152
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
153153
CPP2_UFCS(print, std::move(y), "", "\n");
154154
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ auto main() -> int{
147147
CPP2_UFCS(print, z, " cp-assign ", " <- ");
148148
CPP2_UFCS(print, y, "", "\n");
149149

150-
z = std::move(y);
150+
z = { std::move(y) };
151151
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
152152
CPP2_UFCS(print, std::move(y), "", "\n");
153153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ auto main() -> int{
147147
CPP2_UFCS(print, z, " cp-assign ", " <- ");
148148
CPP2_UFCS(print, y, "", "\n");
149149

150-
z = std::move(y);
150+
z = { std::move(y) };
151151
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
152152
CPP2_UFCS(print, std::move(y), "", "\n");
153153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ auto main() -> int{
153153
CPP2_UFCS(print, z, " cp-assign ", " <- ");
154154
CPP2_UFCS(print, y, "", "\n");
155155

156-
z = std::move(y);
156+
z = { std::move(y) };
157157
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
158158
CPP2_UFCS(print, std::move(y), "", "\n");
159159
}

source/cppfront.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3525,7 +3525,22 @@ class cppfront
35253525
emit(*x.op);
35263526
}
35273527
printer.print_cpp2(" ", n.position());
3528-
emit(*x.expr);
3528+
// When assigning a single expression-list, we can
3529+
// take over direct control of emitting it without needing to
3530+
// go through the whole grammar, and surround it with braces
3531+
if (
3532+
x.op->type() == lexeme::Assignment
3533+
&& x.expr->is_expression_list()
3534+
)
3535+
{
3536+
printer.print_cpp2( "{ ", n.position() );
3537+
emit(*x.expr->get_expression_list(), false);
3538+
printer.print_cpp2( " }", n.position() );
3539+
}
3540+
// Otherwise, just emit the general expression as usual
3541+
else {
3542+
emit(*x.expr);
3543+
}
35293544
}
35303545
}
35313546
}

0 commit comments

Comments
 (0)