Skip to content

[BUG] Last use of member function cannot be inout this #999

Closed
@ProgrammingRainbow

Description

@ProgrammingRainbow

I think this is similar to this bug #888 because i believe it's to do with the implicit move.

If the second to last member function is (inout this) the code runs as expected. But if you comment out or remove the "x.print();" making "x.inc();" that is an (inout this) the last use of x. then it falls apart.

myclass : type = {
    data: int = 42;
    more: std::string = std::to_string(42);

    // method
    print: (this) = {
        std::cout << "data: " << data << ", more: " << more << std::endl;
    }

    // non-const method
    inc: (inout this) = {
        data++;
    }
}

main: () = {
    x: myclass = ();
    x.inc();
    x.print();
}
❯ ../cppfront test3.cpp2
test3.cpp2... ok (all Cpp2, passes safety checks)

❯ g++ -I../include -std=c++23 test3.cpp -o test3

❯ ./test3
data: 43, more: 42
myclass : type = {
    data: int = 42;
    more: std::string = std::to_string(42);

    // method
    print: (this) = {
        std::cout << "data: " << data << ", more: " << more << std::endl;
    }

    // non-const method
    inc: (inout this) = {
        data++;
    }
}

main: () = {
    x: myclass = ();
    x.inc();
}
❯ ../cppfront test3.cpp2
test3.cpp2... ok (all Cpp2, passes safety checks)

❯ g++ -I../include -std=c++23 test3.cpp -o test3
test3.cpp2: In function ‘int main()’:
test3.cpp2:18:19: error: no match for call to ‘(main()::<lambda(Obj&&, Params&& ...)>) (std::remove_reference<myclass&>::type)’
   18 |     x.inc();
In file included from test3.cpp:6:
../include/cpp2util.h:927:1: note: candidate: ‘template<class Obj, class ... Params, bool IsNothrow> main()::<lambda(Obj&&, Params&& ...)>’
  927 | [LAMBDADEFCAPT]< \
      | ^
../include/cpp2util.h:942:59: note: in expansion of macro ‘CPP2_UFCS_’
  942 | #define CPP2_UFCS(...)                                    CPP2_UFCS_(&,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
      |                                                           ^~~~~~~~~~
test3.cpp2:18:5: note: in expansion of macro ‘CPP2_UFCS’
   18 |     x.inc();
      |     ^~~~~~~~ 
../include/cpp2util.h:927:1: note:   template argument deduction/substitution failed:
  927 | [LAMBDADEFCAPT]< \
      | ^
../include/cpp2util.h:942:59: note: in expansion of macro ‘CPP2_UFCS_’
  942 | #define CPP2_UFCS(...)                                    CPP2_UFCS_(&,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
      |                                                           ^~~~~~~~~~
test3.cpp2:18:5: note: in expansion of macro ‘CPP2_UFCS’
   18 |     x.inc();
      |     ^~~~~~~~ 
../include/cpp2util.h:927:1: note: constraints not satisfied
  927 | [LAMBDADEFCAPT]< \
      | ^
../include/cpp2util.h:942:59: note: in expansion of macro ‘CPP2_UFCS_’
  942 | #define CPP2_UFCS(...)                                    CPP2_UFCS_(&,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
      |                                                           ^~~~~~~~~~
test3.cpp2:18:5: note: in expansion of macro ‘CPP2_UFCS’
   18 |     x.inc();
      |     ^~~~~~~~ 
test3.cpp2: In substitution of ‘template<class Obj, class ... Params, bool IsNothrow> main()::<lambda(Obj&&, Params&& ...)> [with Obj = myclass; Params = {}; bool IsNothrow = false]’:
test3.cpp2:18:19:   required from here
test3.cpp2:18:5:   required by the constraints of ‘template<class Obj, class ... Params, bool IsNothrow> main()::<lambda(Obj&&, Params&& ...)>’
../include/cpp2util.h:916:1: note: no operand of the disjunction is satisfied
  915 |    requires { CPP2_FORWARD(obj).CPP2_UFCS_REMPARENS QUALID TEMPKW __VA_ARGS__(CPP2_FORWARD(params)...); } \
      |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  916 | || requires { MVFWD(CPP2_UFCS_REMPARENS QUALID __VA_ARGS__)(CPP2_FORWARD(obj), CPP2_FORWARD(params)...); }
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../include/cpp2util.h:934:14: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
  934 |     requires CPP2_UFCS_CONSTRAINT_ARG(MVFWD,QUALID,TEMPKW,__VA_ARGS__) { \
      |              ^~~~~~~~~~~~~~~~~~~~~~~~
../include/cpp2util.h:942:59: note: in expansion of macro ‘CPP2_UFCS_’
  942 | #define CPP2_UFCS(...)                                    CPP2_UFCS_(&,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
      |                                                           ^~~~~~~~~~
test3.cpp2:18:5: note: in expansion of macro ‘CPP2_UFCS’
   18 |     x.inc();
      |     ^~~~~~~~ 
cc1plus: note: set ‘-fconcepts-diagnostics-depth=’ to at least 2 for more detail

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