cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] using a lamba as a parameter for a function in a for loop causes std::move from non-last use

Open farmerpiki opened this issue 1 year ago • 2 comments

This is the best I could trim the example down...

test: (inout view) -> bool = {
  v: std::vector<function> = (a, b, c);
  for view do (copy line) {
      use_v(v, : (field) = std::string_view);
  }
  return true;
}

without the lambda function parameter it just works, but if I add that it translates into cpp2::move(v) resulting in losing the value of v after the first use in the for loop. further testing revealed that even just declaring a lambda or using it in a parameter to another function results in the same erroneous behaviour.

test: (inout view) -> bool = {
  v: std::vector<function> = (a, b, c);
  for view do (copy line) {
      use_v(v);
      : (field) = std::string_view;
  }
  return true;
}

Also I didn't test but it probably happens with other loops as well.

farmerpiki avatar Aug 20 '24 15:08 farmerpiki

That's very hard to read without a code block:

validate_file_type: (inout view: gzip_decompress_view) -> bool = {
  v: std::vector<validating_function> = (a, b, c);
  for view do (copy line) {
    if !validate_data_line(zip(v, line
      | split('\t')
      | transform(: (field) -> std::string_view = { return std::string_view(field); }))) {
        return false;
      }
    }
  return true;
}

gregmarr avatar Aug 20 '24 16:08 gregmarr

managed to clean up the example quite a bit, also added a code block, updated OP, thanks @gregmarr

farmerpiki avatar Aug 21 '24 11:08 farmerpiki