cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Double move when passing by move

Open JohelEGP opened this issue 2 years ago • 6 comments
trafficstars

Title: Double move when passing by move.

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

main: () = {
  x := 0;
  :(y) = 0;
  (move x);
}

Commands:

cppfront -clean-cpp1 main.cpp2

Expected result:

  [](auto const& y) -> void { 0;  }
  (std::move(x));

Actual result and error:

  [](auto const& y) -> void { 0;  }
  (std::move(std::move(x)));

JohelEGP avatar May 03 '23 20:05 JohelEGP

I fixed it locally, I don't know if it breaks something else in the process. In cppfront.h, line under 1628:

line 1624          //  For an explicit 'forward' apply forwarding to correct identifier
                           assert (!current_args.empty());
                           if (current_args.back().pass == passing_style::forward) {
                                        add_forward = current_args.back().ptoken == n.identifier;
line 1628              }

I have added :

  // If explicit 'move' is used then don't add move
   if (current_args.back().pass == passing_style::move) {
            add_move = false;
    }  

Let me know if this works or not, I look forward to contributing more actively, but for the time being I'm familiarizing my self with the compiler code.

HALL9kv0 avatar May 04 '23 00:05 HALL9kv0

Let me know if this works or not, I look forward to contributing more actively, but for the time being I'm familiarizing my self with the compiler code.

I can't know, as I don't have working code. Just lots of snippets compliant to the grammar.

JohelEGP avatar May 04 '23 01:05 JohelEGP

My apologies, I was referring to the contributors.

HALL9kv0 avatar May 04 '23 01:05 HALL9kv0

Now I can confirm that it works, and there's just one change to existing tests:

-    v = X<19>(); std::cout << "move(v) as X<19> = " + cpp2::to_string(int(cpp2::as_<X<19>>((std::move(std::move(v)))))) << std::endl;
+    v = X<19>(); std::cout << "move(v) as X<19> = " + cpp2::to_string(int(cpp2::as_<X<19>>((std::move(v))))) << std::endl;

JohelEGP avatar May 31 '23 02:05 JohelEGP

An alternative Expected result is a diagnostic saying that x will be moved, and thus the explicit move is redundant.

JohelEGP avatar Jul 26 '23 13:07 JohelEGP

An alternative Expected result is a diagnostic saying that x will be moved, and thus the explicit move is redundant.

This isn't true in a generic context. So I expect move x to always work in a generic context.

JohelEGP avatar Dec 14 '23 03:12 JohelEGP