Skip to content

[BUG] in argument passing to function that uses cpp2 user defined type failed with error #270

Closed
@filipsajdak

Description

@filipsajdak

258190e introduces the possibility of defining user-defined types in cpp2. Unfortunately, it fails with a free functions that use UDT as in parameters.

The following code:

X : type = {
    i : int = 42;
}

fun: (x: X) = {
    std::cout << x.i << std::endl;
}

generates:

#define CPP2_USE_MODULES         Yes

#include "cpp2util.h"


#line 1 "../tests/bug_incomplete_type.cpp2"
class X;
#line 5 "../tests/bug_incomplete_type.cpp2"
auto fun(cpp2::in<X> x) -> void;

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

#line 1 "../tests/bug_incomplete_type.cpp2"
class X   {
    private: int i {42}; 
};

auto fun(cpp2::in<X> x) -> void{
    std::cout << x.i << std::endl;
}

It fails on cpp1 compilation with the error:

In file included from ../tests/bug_incomplete_type.cpp:4:
include/cpp2util.h:496:5: error: invalid application of 'sizeof' to an incomplete type 'X'
    sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>;
    ^~~~~~~~~
include/cpp2util.h:501:9: note: in instantiation of variable template specialization 'cpp2::prefer_pass_by_value<X>' requested here
        prefer_pass_by_value<T>,
        ^
../tests/bug_incomplete_type.cpp2:5:16: note: in instantiation of template type alias 'in' requested here
auto fun(cpp2::in<X> x) -> void;
               ^
../tests/bug_incomplete_type.cpp2:1:7: note: forward declaration of 'X'
class X;
      ^
In file included from ../tests/bug_incomplete_type.cpp:4:
include/cpp2util.h:501:9: error: non-type template argument is not a constant expression
        prefer_pass_by_value<T>,
        ^
../tests/bug_incomplete_type.cpp2:5:16: note: in instantiation of template type alias 'in' requested here
auto fun(cpp2::in<X> x) -> void;
               ^
2 errors generated.

The issue is that in argument passing is trying to figure out if auto fun(cpp2::in<X> x) -> void; passes x by reference or by value - it distinguishes that based on the size of X. Unfortunately, at that point compiler does not know the size of X.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions