Skip to content

[BUG] No implicit move or forward on last use in initialization #749

Closed

Description

@JohelEGP

Title: No implicit move or forward on last use in initialization.

Minimal reproducer (https://cpp2.godbolt.org/z/Pb6YaYvsT):

f: (copy x)    = { _ = x; } // OK, moves.
f: (forward x) = { _ = x; } // OK, forwards.
g: (copy x)    = { i := x; } // Wrong, doesn't move.
g: (forward x) = { i := x; } // Wrong, doesn't forward.
main: () = { }
Commands:
cppfront main.cpp2
clang++18 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -I . main.cpp

Expected result:

auto f(auto x) -> void{static_cast<void>(std::move(x));}// OK, moves.
auto f(auto&& x) -> void{static_cast<void>(CPP2_FORWARD(x)); }// OK, forwards.
auto g(auto x) -> void{auto i {std::move(x)}; }// Wrong, doesn't move.
auto g(auto&& x) -> void{auto i {CPP2_FORWARD(x)}; }// Wrong, doesn't forward.

Actual result and error:

Program returned: 0
auto f(auto x) -> void{static_cast<void>(std::move(x));}// OK, moves.
auto f(auto&& x) -> void{static_cast<void>(CPP2_FORWARD(x)); }// OK, forwards.
auto g(auto x) -> void{auto i {x}; }// Wrong, doesn't move.
auto g(auto&& x) -> void{auto i {x}; }// Wrong, doesn't forward.
Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



//=== Cpp2 type definitions and function declarations ===========================

auto f(auto x) -> void;
auto f(auto&& x) -> void;
auto g(auto x) -> void;
auto g(auto&& x) -> void;
auto main() -> int;


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

auto f(auto x) -> void{static_cast<void>(std::move(x));}// OK, moves.
auto f(auto&& x) -> void{static_cast<void>(CPP2_FORWARD(x)); }// OK, forwards.
auto g(auto x) -> void{auto i {x}; }// Wrong, doesn't move.
auto g(auto&& x) -> void{auto i {x}; }// Wrong, doesn't forward.
auto main() -> int{}

Activity

  1. hsutter commented on Nov 14, 2023

    @hsutter
    Owner

    Thanks! Repeating rationale here from #792 comment thread: The issue is that I was trying out suppressing move-from-last-use if the last use was in an initializer, because of examples like view: std::span = local_variable; when that was the last use of local_variable, which is an error for an rvalue. But that also suppresses a righteous move in examples like result := local_variable;. So I now agree those should be treated as rvalues too, and if that causes an error for an rvalue because there is no further us of that variable in scope, then the programmer must write an explicit discard of the variable.

  2. added a commit that references this issue on Dec 5, 2023
    8fb21ca
  3. added a commit that references this issue on Feb 1, 2024
    8a93460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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