Skip to content

[BUG] Local copy/forward parameter not moved/forwarded #825

Closed
@JohelEGP

Description

@JohelEGP

Title: Local copy/forward parameter not moved/forwarded.

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

main: (args) = {
  a := 0;
  _ = a; // Moves.

  _ = :(copy b) _ = b;        // Doesn't move.
  for args do (copy c) _ = c; // Doesn't move.

  _ = :(forward d) _ = d;        // Doesn't forward.
  for args do (forward e) _ = e; // Doesn't forward.
}
f: (copy x) _    = x; // Moves.
f: (forward x) _ = x; // Forwards.
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: Same as local variable/function parameter.

Actual result and error: As indicated in the comments.

Cpp2 lowered to Cpp1:
//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



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

auto main(int const argc_, char** argv_) -> int;
  

[[nodiscard]] auto f(auto x) -> auto;
[[nodiscard]] auto f(auto&& x) -> auto;


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

auto main(int const argc_, char** argv_) -> int{
  auto const args = cpp2::make_args(argc_, argv_); 
  auto a {0}; 
  static_cast<void>(std::move(a));// Moves.

  static_cast<void>([](auto b) -> auto { return static_cast<void>(b);  });// Doesn't move.
  for ( auto c : args ) static_cast<void>(c); // Doesn't move.

  static_cast<void>([](auto&& d) -> auto { return static_cast<void>(d);  });// Doesn't forward.
  for ( auto&& e : args ) static_cast<void>(e); // Doesn't forward.
}
[[nodiscard]] auto f(auto x) -> auto { return static_cast<void>(std::move(x)); }// Moves.
[[nodiscard]] auto f(auto&& x) -> auto { return static_cast<void>(CPP2_FORWARD(x));  }// Forwards.
Output:
Program returned: 0

See also:

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