Skip to content

[BUG] requires clause does not appear in "cpp2 type definitions and function declaration" causing out-of-line definition error #323

Closed
@filipsajdak

Description

@filipsajdak

In the current implementation of cppfront (827ed79), the following code:

element: type = {
    name: std::string;

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

Generates:

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


#include "cpp2util.h"

#line 1 "../tests/bug_assignement_operator_5.cpp2"
class element;

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

#line 1 "../tests/bug_assignement_operator_5.cpp2"
class element {
    private: std::string name; 

    public: explicit element(auto&& n); // <--- notice lack of requires clause
#line 4 "../tests/bug_assignement_operator_5.cpp2"
    public: auto operator=(auto&& n) -> element& ; // <--- notice lack of requires clause

#line 7 "../tests/bug_assignement_operator_5.cpp2"
};

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


#line 4 "../tests/bug_assignement_operator_5.cpp2"
    element::element(auto&& n)
requires (std::is_same_v<CPP2_TYPEOF(n), std::string>) // <--- notice requires clause is present
        : name{ CPP2_FORWARD(n) }
#line 4 "../tests/bug_assignement_operator_5.cpp2"
    {

    }
#line 4 "../tests/bug_assignement_operator_5.cpp2"
    auto element::operator=(auto&& n) -> element& 
requires (std::is_same_v<CPP2_TYPEOF(n), std::string>) // <--- notice requires clause is present
#line 4 "../tests/bug_assignement_operator_5.cpp2"
    {
        name = CPP2_FORWARD(n);
        return *this;

#line 6 "../tests/bug_assignement_operator_5.cpp2"
    }

Trying to compile that with cpp1 compiler (Apple clang version 14.0.3 in my case):

../tests/bug_assignement_operator_5.cpp2... ok (all Cpp2, passes safety checks)

../tests/bug_assignement_operator_5.cpp2:4:14: error: out-of-line definition of 'element' does not match any declaration in 'element'
    element::element(auto&& n)
             ^~~~~~~
../tests/bug_assignement_operator_5.cpp2:4:19: error: out-of-line definition of 'operator=' does not match any declaration in 'element'
    auto element::operator=(auto&& n) -> element& 
                  ^~~~~~~~
2 errors generated.

The fix is pretty straightforward - requires clause must also appear in the Cpp2 type definitions and function declarations section (I have changed that manually, and it works).

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