Skip to content

Commit

Permalink
fix(parse): permit unbraced function expression as index and to is/as
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed May 31, 2023
1 parent 9584fcc commit cc20bb9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
t: type = {
operator[]: (this, f) = { }
}
main: () -> int = {
(x := t()) { x[:() -> _ = 0]; }
[[assert: !(:() = 0; is int) ]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#define CPP2_USE_MODULES Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "pure2-bugfix-for-unbraced-function-expression.cpp2"
class t;


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

#line 1 "pure2-bugfix-for-unbraced-function-expression.cpp2"
class t {
public: auto operator[](auto const& f) const -> void;

public: t() = default;
public: t(t const&) = delete; /* No 'that' constructor, suppress copy */
public: auto operator=(t const&) -> void = delete;
#line 3 "pure2-bugfix-for-unbraced-function-expression.cpp2"
};
[[nodiscard]] auto main() -> int;


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


#line 2 "pure2-bugfix-for-unbraced-function-expression.cpp2"
auto t::operator[](auto const& f) const -> void{}

[[nodiscard]] auto main() -> int{
{
auto const& x = t();
#line 5 "pure2-bugfix-for-unbraced-function-expression.cpp2"
{cpp2::assert_in_bounds(x, []() -> auto { return 0; }); }
}
#line 6 "pure2-bugfix-for-unbraced-function-expression.cpp2"
cpp2::Default.expects(!((cpp2::is<int>([]() -> void { 0; }))), "");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-bugfix-for-unbraced-function-expression.cpp2... ok (all Cpp2, passes safety checks)

3 changes: 3 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3670,6 +3670,9 @@ class parser
&& 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
&& curr().type() != lexeme::RightBracket // not as the last index argument
&& curr().as_string_view() == "is" // not as the argument to is
&& curr().as_string_view() == "as" // not as the argument to as
) {
// this is a fix for a short function syntax that should have double semicolon used
// (check comment in expression_statement(bool semicolon_required))
Expand Down

0 comments on commit cc20bb9

Please sign in to comment.