Skip to content

[BUG] Type with a member that has initializer and defined operator=: (out this, n : std::name ) produces an assignment operator that badly assign member with initializer #291

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

Closed
filipsajdak opened this issue Mar 24, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@filipsajdak
Copy link
Contributor

In the current implementation of cppfront (fbf55ad) the following code:

element: type = {
    children: std::vector<int> = ();
    name: std::string;

    operator=: (out this, n : std::string ) = {
        name = n;
    }
}

Generates:

#include "cpp2util.h"


#line 1 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
class element;

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

#line 1 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
class element  {
    private: std::vector<int> children {}; 
    private: std::string name; 

    public: explicit element(cpp2::in<std::string> n)
        : name{ n }
#line 6 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
{
    }
#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
    public: auto operator=(cpp2::in<std::string> n) -> element& {
        children = ;
        name = n;
#line 6 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
        
        return *this;
#line 7 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
    }
};

The issue is that the assignment operator has one assignment to the children variable that is not complete:

        children = ;
@filipsajdak
Copy link
Contributor Author

I found a workaround and resigned from having a default initializer for my members. The following code:

element: type = {
    children: std::vector<int>;
    name: std::string;

    operator=: (out this, n : std::string ) = {
        children = std::vector<int>(); // it would be good to be able to spell it: children = ();
        name = n;
    }
}

works and generate:

#include "cpp2util.h"

#line 1 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
class element;

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

#line 1 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
class element  {
    private: std::vector<int> children; 
    private: std::string name; 

    public: explicit element(cpp2::in<std::string> n)
        : children{ std::vector<int>() }
        , name{ n }
#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
                                            {

#line 8 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
    }
#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
    public: auto operator=(cpp2::in<std::string> n) -> element& {
        children = std::vector<int>();
        name = n;
        return *this;
#line 8 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_2.cpp2"
    }
};

@hsutter
Copy link
Owner

hsutter commented Apr 1, 2023

Thanks. Like #290, I think this is fixed by feff640. Please check, including the commit warning -- see #290 comment thread.

@hsutter hsutter closed this as completed Apr 1, 2023
@filipsajdak
Copy link
Contributor Author

@hsutter I confirmed it is solved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants