Skip to content

[BUG] New terse function syntax requires adding a semicolon after expression #760

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
gregmarr opened this issue Oct 16, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@gregmarr
Copy link
Contributor

Describe the bug
A trailing semicolon is required when dropping the -> _ = from an inline lambda expression used inside a function call.

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
    // okay
    std::transform(a.begin(), a.end(), b.begin(), :(x) -> _ = { return x + 1; });

    // remove "{ return" and "; }", okay
    std::transform(a.begin(), a.end(), b.begin(), :(x) -> _ = x + 1);

    // remove "-> _ =", error: invalid statement encountered inside a compound-statement (at 'x')
    std::transform(a.begin(), a.end(), b.begin(), :(x) x + 1);

    // put back the ";", okay
    std::transform(a.begin(), a.end(), b.begin(), :(x) x + 1;);
  1. Command lines including which C++ compiler you are using
    https://www.godbolt.org/z/6T9z4nx88

  2. Expected result - what you expected to happen
    Successful compilation.

  3. Actual result/error
    error: invalid statement encountered inside a compound-statement (at 'x')

Additional context
#714

@hsutter
Copy link
Owner

hsutter commented Oct 20, 2023

How about that... somehow I had in my head that the ; was always required. I'm a little surprised that the second example works... perhaps it's looking for any valid expression (which would maximally grab as much as could be a valid expression, which could include an expression_statement) rather than specifically an expression-statement.

Well, for the terse syntax I just changed the check to see if there was a valid expression instead of expression-statement and all seems to be well. Let's try it. Saves another character on the tersest syntax...

Also, just to make sure it still works on a non-first argument, this is fine too now with the latest commit above:

main: () = {
    a: std::vector<int> = (1, 2, 3);
    b: std::vector<int> = (7, 8, 9);

    test( :(x) x+1, a, b );

    for b do (e) std::cout << e;
}

test: (c, a, inout b) =
    std::transform(a.begin(), a.end(), b.begin(), c);

Thanks!

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