Skip to content

[BUG] variable used in if condition is moved despite it is used in the if statement #311

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 Mar 31, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@filipsajdak
Copy link
Contributor

In the current implementation of cppfront (b370ab8), the following code:

main: () = {
    b : bool = true;

    if b {
        std::cout << b << std::endl;
    }
}

Generates:

auto main() -> int{
    bool b {true}; 

    if (std::move(b)) {
        std::cout << std::move(b) << std::endl;
    }
}

The b variable is moved in the if condition despite its use in the if statement.

Adding the use of b after an if removes the move of b in the if condition and if statement which is correct.

@filipsajdak filipsajdak added the bug Something isn't working label Mar 31, 2023
@filipsajdak filipsajdak changed the title [BUG] variable used in if statement is moved despite it is used in the if body [BUG] variable used in if condition is moved despite it is used in the if statement Mar 31, 2023
@filipsajdak
Copy link
Contributor Author

Adding an else with another use of b in statement-false also adds move:

auto main() -> int{
    bool b {true}; 

    if (std::move(b)) {
        std::cout << std::move(b) << std::endl;
    }else {
        std::cout << !(std::move(b)) << std::endl;
    }
}

@hsutter hsutter closed this as completed in 5cc3326 Apr 2, 2023
@hsutter
Copy link
Owner

hsutter commented Apr 2, 2023

Thanks! These cases should work now, if not please reopen.

@filipsajdak
Copy link
Contributor Author

Confirmed. Fixed.

zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
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

No branches or pull requests

2 participants