Skip to content

Commit

Permalink
fix(cpp1): support initializing assignment from expression list in `o…
Browse files Browse the repository at this point in the history
…perator=`
  • Loading branch information
JohelEGP committed Sep 15, 2023
1 parent ecd3726 commit 9ace4d9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 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
@@ -1,4 +1,4 @@
clang version 18.0.0 (https://git.uplinklabs.net/mirrors/llvm-project.git c0abd3814564a568dfc607c216e6407eaa314f46)
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 3723ede3cf5324827f8fbbe7f484c2ee4d7a7204)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /home/johel/root/clang-main/bin
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,73 @@
#include "cpp2util.h"


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


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

auto main() -> int;
using vec = std::vector<int>;

class my_vec {
private: vec v {};
public: explicit my_vec([[maybe_unused]] cpp2::in<std::integral_constant<int,0>> param2);

#line 5 "pure2-bugfix-for-assign-expression-list.cpp2"
public: auto operator=([[maybe_unused]] cpp2::in<std::integral_constant<int,0>> param2) -> my_vec& ;
public: explicit my_vec([[maybe_unused]] cpp2::in<std::integral_constant<int,1>> param2);

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

#line 10 "pure2-bugfix-for-assign-expression-list.cpp2"
auto main() -> int{
using vec = std::vector<int>;
vec v {0};
v = { };
cpp2::Default.expects(v == vec{}, "");
Expand Down
8 changes: 8 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4640,6 +4640,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 9ace4d9

Please sign in to comment.