Skip to content

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

Open
@MatthieuHernandez

Description

@MatthieuHernandez

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?

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