Skip to content

Commit

Permalink
Correction for short function syntax, closes hsutter#356
Browse files Browse the repository at this point in the history
Current implementation does not work for the following code:
```cpp
main: () = {
    :() = 1;
    [[assert: 1]]
}
```
It fails with error:
```
error: subscript expression [ ] must not be empty (if you were trying to name a C-style array type, use 'std::array' instead) (at '[')
```

This change introduce small correction that moves back parsing
to semicolon (to simulate double semicolon) for short syntax.

It is not done in the following cases:
```cpp
:() = 1;(); // imediatelly called lambda
f(a,b,:() = 1;); // last argument in function call
f(a,:() = 1;,c); // first or in the middle argument
```

After this change the original issue is solved.
All regression tests pass. Closes hsutter#356
  • Loading branch information
filipsajdak committed Apr 15, 2023
1 parent d7adb8f commit d737f9e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,17 @@ class parser
next();
return {};
}
if (
peek(-1) && peek(-1)->type() != lexeme::RightBrace // it is short function syntax
&& curr().type() != lexeme::LeftParen // not imediatelly called
&& curr().type() != lexeme::RightParen // not as a last argument to function
&& curr().type() != lexeme::Comma // not as first or in-the-middle, function argument
) {
// this is a fix for a short function syntax that should have double semicolon used
// (check comment in expression_statement(bool semicolon_required))
// We simulate double semicolon by moving back to single semicolon.
next(-1);
}
}
else {
error("(temporary alpha limitation) an unnamed declaration at expression scope must be a function or an object");
Expand Down

0 comments on commit d737f9e

Please sign in to comment.