Skip to content

[BUG] Missing braces in assignment operator when initialize vector with a list of values #321

Closed
@filipsajdak

Description

@filipsajdak

After f608b78 (done to fix #312). It broke the scenario described here: #312 (comment)

For the following code:

element: type = {
    children: std::vector<int> = (1,2,3);
    tab : std::vector<int>;

    operator=: (out this, t : int ) = {
        tab = (3,2,1);
    }
}

Cppfront will generate (skipping type declarations, type definitions and function declarations):

#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_4.cpp2"
    element::element(cpp2::in<int> t)
        : tab{ 3, 2, 1 }
#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_4.cpp2"
    {

    }
#line 5 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_4.cpp2"
    auto element::operator=(cpp2::in<int> t) -> element& {
        children = 1, 2, 3;
        tab = 3, 2, 1;
        return *this;

#line 7 "/Users/filipsajdak/dev/execspec/external/tests/bug_assignement_operator_4.cpp2"
    }

The issue is with element::operator=() that has the following lines:

        children = 1, 2, 3;
        tab = 3, 2, 1;

And will compile with the error (cut only the part that describes the problem):

../tests/bug_assignement_operator_4.cpp2:6:18: error: no viable overloaded '='
        children = 1, 2, 3;
        ~~~~~~~~ ^ ~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:444:13: note: candidate function not viable: no known conversion from 'int' to 'const std::vector<int>' for 1st argument
    vector& operator=(const vector& __x);
            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:454:13: note: candidate function not viable: no known conversion from 'int' to 'initializer_list<std::vector<int>::value_type>' (aka 'initializer_list<int>') for 1st argument
    vector& operator=(initializer_list<value_type> __il)
            ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:469:13: note: candidate function not viable: no known conversion from 'int' to 'std::vector<int>' for 1st argument
    vector& operator=(vector&& __x)
            ^
../tests/bug_assignement_operator_4.cpp2:7:13: error: no viable overloaded '='
        tab = 3, 2, 1;
        ~~~ ^ ~

Changing problematic lines to the following:

        children = {1, 2, 3};
        tab = {3, 2, 1};

Fixes the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions