cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Call a function with inout parameters cannot compile when cpp2::move is invoked.

Open MatthieuHernandez opened this issue 11 months ago • 1 comments

I'm not sure if what I'm about to present is a bug or not, but without any workaround, it seems impossible to write recursive functions.

When I use functions with inout parameters, I get a compilation errors when cpp2::move is invoked. Here's a Minimal, Reproducible Example:

A: type = {
    public value: i32 = 4;
}

increase_and_print: (inout a: A) = {
    a.value++;
    std::cout << a.value << "\n";
}

main: () = {
    a:A = ();
    increase_and_print(a);
    increase_and_print(a);
    assert(a&); // Workaround to avoid moving a on previous line.
}

https://cpp2.godbolt.org/z/d371E16Es

Without the assert, compilation fails with this error:

error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'A'

This is because the Cpp2 is trying to move a because the object a is no longer used in main:

auto main() -> int{
    A a {}; 
    increase_and_print(a);
    increase_and_print(cpp2::move(a));
}

Adding a assert as workaround solves the problem, but is it a bug?

MatthieuHernandez avatar Feb 16 '25 21:02 MatthieuHernandez