Skip to content

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

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 Apr 5, 2023 · 5 comments · Fixed by #486
Labels
bug Something isn't working

Comments

@filipsajdak
Copy link
Contributor

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).

@filipsajdak filipsajdak added the bug Something isn't working label Apr 5, 2023
@filipsajdak
Copy link
Contributor Author

While looking for a place on how to fix it, I have found comments:

cppfront/source/cppfront.cpp

Lines 5110 to 5129 in 827ed79

// *** NOTE =====================================================
//
// This branch to emit the requires-clause should maybe be
// moved to location (A) above, so that it's also emitted
// on the function declaration. But moving it to (A) triggers
// a bug in GCC 10.x (that was fixed in 11.x), where it would
// break using a 'forward' parameter of a concrete type and
// also explicitly user-written requires-clauses that do
// similar decltype tests.
//
// I don't want to neednessly break compatibility with a
// decently conforming C++20 compiler that works well for
// everything else that Cpp2 needs from C++20. If the
// 'requires' down here doesn't cause a problem, I'll keep
// it here for now... if we do encounter a reason it needs to
// also be on the declaration, move this code to (A).
//
// Handle requires clause - an explicit one the user wrote,
// and/or any conditions we generated while processing the
// parameters (i.e., forwarding a concrete type)

And the (A) location is here:

// *** LOCATION (A) -- SEE NOTE REGARDING (A) BELOW

@filipsajdak
Copy link
Contributor Author

filipsajdak commented Apr 5, 2023

@hsutter regarding gcc-11 installation on Ubuntu 20.04 - have you tried #293 (comment) ?

TLDR: you can install gcc-11 and g++-11 with:

add-apt-repository -y ppa:ubuntu-toolchain-r/test && apt-get update && apt-get install -y --no-install-recommends gcc-11 g++-11

@JohelEGP
Copy link
Contributor

I think the test happens to work, despite #323, because the definition overloads with the declaration. So actually not emitting the requires on the declaration wouldn't be a fix even for GCC 10.
-- #477 (comment)

@JohelEGP
Copy link
Contributor

JohelEGP commented Jun 1, 2023

GCC not-10 is also broken: https://cpp2.godbolt.org/z/MsY89sMxh.
We shouldn't be deprived of requires clauses for GCC 10. Nerf it.

@JohelEGP
Copy link
Contributor

JohelEGP commented Jun 7, 2023

Proof that not emitting requires makes an overload set: https://cpp2.godbolt.org/z/PrEqz4nW6.

f: (forward x: std::string) = std::ignore = x;
main: () = f(0);

It wants to call the unconstrained forward declaration!

/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../x86_64-linux-gnu/bin/ld: CMakeFiles/test.dir/_cppfront/main.cpp.o: in function `main':
main.cpp:(.text+0x14): undefined reference to `void f<int>(int&&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

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

Successfully merging a pull request may close this issue.

2 participants