Skip to content

Commit

Permalink
fix(to_cpp1): support initializing assignment from expression list in…
Browse files Browse the repository at this point in the history
… `operator=`
  • Loading branch information
JohelEGP committed Oct 31, 2024
1 parent 1032a5b commit 8324e25
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
10 changes: 9 additions & 1 deletion regression-tests/pure2-bugfix-for-assign-expression-list.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
vec: type == std::vector<int>;

my_vec: type = {
v: vec = ();
operator=: (out this, _: std::integral_constant<int, 0>) = v = ();
operator=: (out this, _: std::integral_constant<int, 1>) = v = (1);
operator=: (out this, _: std::integral_constant<int, 2>) = v = (2, 3);
}

main: () = {
vec: type == std::vector<int>;
v: vec = (0);
v = ();
assert( v == :vec = () );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,63 @@

#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"

#line 3 "pure2-bugfix-for-assign-expression-list.cpp2"
class my_vec;


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

#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"
using vec = std::vector<int>;
#line 2 "pure2-bugfix-for-assign-expression-list.cpp2"

class my_vec {
private: vec v {};
public: explicit my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,0>> unnamed_param_2);
#line 5 "pure2-bugfix-for-assign-expression-list.cpp2"
public: auto operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,0>> unnamed_param_2) -> my_vec& ;
public: explicit my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,1>> unnamed_param_2);
#line 6 "pure2-bugfix-for-assign-expression-list.cpp2"
public: auto operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,1>> unnamed_param_2) -> my_vec& ;
public: explicit my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,2>> unnamed_param_2);
#line 7 "pure2-bugfix-for-assign-expression-list.cpp2"
public: auto operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,2>> unnamed_param_2) -> my_vec& ;
public: my_vec(my_vec const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(my_vec const&) -> void = delete;

#line 8 "pure2-bugfix-for-assign-expression-list.cpp2"
};

auto main() -> int;

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

#line 1 "pure2-bugfix-for-assign-expression-list.cpp2"

#line 5 "pure2-bugfix-for-assign-expression-list.cpp2"
my_vec::my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,0>> unnamed_param_2)
: v{ } { }
#line 5 "pure2-bugfix-for-assign-expression-list.cpp2"
auto my_vec::operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,0>> unnamed_param_2) -> my_vec& {
v = { };
return *this; }
#line 6 "pure2-bugfix-for-assign-expression-list.cpp2"
my_vec::my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,1>> unnamed_param_2)
: v{ 1 } { }
#line 6 "pure2-bugfix-for-assign-expression-list.cpp2"
auto my_vec::operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,1>> unnamed_param_2) -> my_vec& {
v = { 1 };
return *this; }
#line 7 "pure2-bugfix-for-assign-expression-list.cpp2"
my_vec::my_vec([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,2>> unnamed_param_2)
: v{ 2, 3 } { }
#line 7 "pure2-bugfix-for-assign-expression-list.cpp2"
auto my_vec::operator=([[maybe_unused]] cpp2::impl::in<std::integral_constant<int,2>> unnamed_param_2) -> my_vec& {
v = { 2, 3 };
return *this; }

#line 10 "pure2-bugfix-for-assign-expression-list.cpp2"
auto main() -> int{
#line 2 "pure2-bugfix-for-assign-expression-list.cpp2"
using vec = std::vector<int>;
vec v {0};
v = { };
if (cpp2::cpp2_default.is_active() && !(v == vec{}) ) { cpp2::cpp2_default.report_violation(""); }
Expand Down
8 changes: 8 additions & 0 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -5451,6 +5451,14 @@ class cppfront
if (found_explicit_init)
{
initializer = rhs;
if (
is_assignment
&& std::get<statement_node::expression>((*statement)->statement)
->expr->expr->terms[0].expr->is_expression_list()
)
{
initializer = "{ " + rhs + " }";
}

// We've used this statement, so note it
// and move 'statement' forward
Expand Down

0 comments on commit 8324e25

Please sign in to comment.