Skip to content

fix(cpp1): support assignment from expression list #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions regression-tests/pure2-bugfix-for-assign-expression-list.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
main: () = {
vec: type == std::vector<int>;
v: vec = (0);
v = ();
[[assert: v == :vec = ()]]
v = (1);
[[assert: v == :vec = (1)]]
v = (2, 3);
[[assert: v == :vec = (2, 3)]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#define CPP2_USE_MODULES Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"
auto main() -> int;


//=== Cpp2 function definitions =================================================

#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"
auto main() -> int{
using vec = std::vector<int>;
vec v {0};
v = { };
cpp2::Default.expects(v==vec{}, "");
v = { 1 };
cpp2::Default.expects(v==vec{1}, "");
v = { 2, 3 };
cpp2::Default.expects(std::move(v)==vec{2, 3}, "");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-bugfix-for-assign-expression-list.cpp2... ok (all Cpp2, passes safety checks)

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ auto main() -> int{
CPP2_UFCS(print, z, " cp-assign ", " <- ");
CPP2_UFCS(print, y, "", "\n");

z = std::move(y);
z = { std::move(y) };
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
CPP2_UFCS(print, std::move(y), "", "\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ auto main() -> int{
CPP2_UFCS(print, z, " cp-assign ", " <- ");
CPP2_UFCS(print, y, "", "\n");

z = std::move(y);
z = { std::move(y) };
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
CPP2_UFCS(print, std::move(y), "", "\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ auto main() -> int{
CPP2_UFCS(print, z, " cp-assign ", " <- ");
CPP2_UFCS(print, y, "", "\n");

z = std::move(y);
z = { std::move(y) };
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
CPP2_UFCS(print, std::move(y), "", "\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ auto main() -> int{
CPP2_UFCS(print, z, " cp-assign ", " <- ");
CPP2_UFCS(print, y, "", "\n");

z = std::move(y);
z = { std::move(y) };
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
CPP2_UFCS(print, std::move(y), "", "\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ auto main() -> int{
CPP2_UFCS(print, z, " cp-assign ", " <- ");
CPP2_UFCS(print, y, "", "\n");

z = std::move(y);
z = { std::move(y) };
CPP2_UFCS(print, std::move(z), " mv-assign ", " <- ");
CPP2_UFCS(print, std::move(y), "", "\n");
}
Expand Down
18 changes: 17 additions & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3525,7 +3525,23 @@ class cppfront
emit(*x.op);
}
printer.print_cpp2(" ", n.position());
emit(*x.expr);

// When assigning a single expression-list, we can
// take over direct control of emitting it without needing to
// go through the whole grammar, and surround it with braces
if (
x.op->type() == lexeme::Assignment
&& x.expr->is_expression_list()
Copy link
Owner

@hsutter hsutter Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but it needs

                && std::ssize(x.expr->get_expression_list()->expressions.size()) != 1

otherwise x = (move y); will grow braces we don't need. I'll make the change directly and merge.

Thanks!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I was wrong -- we want those { } in other cases. Will revert my changes...

)
{
printer.print_cpp2( "{ ", n.position() );
emit(*x.expr->get_expression_list(), false);
printer.print_cpp2( " }", n.position() );
}
// Otherwise, just emit the general expression as usual
else {
emit(*x.expr);
}
}
}
}
Expand Down