Skip to content

fix(parse): recognize parameter named after contextual keyword #505

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f: (virtual) = { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
t: type = {
f: (in) -> _ = in;
}
g: (final) -> _ = final.first;
h: (out, x) -> _ = out(x);
main: () = {
[[assert: t::f(0) == 0]]
[[assert: g(std::make_pair(1, 2)) == 1]]
[[assert: h(:(y) -> _ = y, 3) == 3]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pure2-bugfix-for-parameter-named-after-contextual-keyword-error.cpp2...
pure2-bugfix-for-parameter-named-after-contextual-keyword-error.cpp2(1,5): error: invalid parameter identifier (at 'virtual')

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-parameter-named-after-contextual-keyword.cpp2"
class t;


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

#line 1 "pure2-bugfix-for-parameter-named-after-contextual-keyword.cpp2"
class t {
public: [[nodiscard]] static auto f(auto const& in) -> auto;

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-parameter-named-after-contextual-keyword.cpp2"
};
[[nodiscard]] auto g(auto const& final) -> auto;
[[nodiscard]] auto h(auto const& out, auto const& x) -> auto;
auto main() -> int;


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


#line 2 "pure2-bugfix-for-parameter-named-after-contextual-keyword.cpp2"
[[nodiscard]] auto t::f(auto const& in) -> auto { return in; }

[[nodiscard]] auto g(auto const& final) -> auto { return final.first; }
[[nodiscard]] auto h(auto const& out, auto const& x) -> auto { return out(x); }
auto main() -> int{
cpp2::Default.expects(t::f(0)==0, "");
cpp2::Default.expects(g(std::make_pair(1, 2))==1, "");
cpp2::Default.expects(h([](auto const& y) -> auto { return y; }, 3)==3, "");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-bugfix-for-parameter-named-after-contextual-keyword.cpp2... ok (all Cpp2, passes safety checks)

19 changes: 19 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5787,6 +5787,7 @@ class parser

// Handle optional this-specifier
//
auto this_specifier_pos = pos; // For backtracking if it's to be an identifier.
if (curr() == "implicit") {
n->mod = parameter_declaration_node::modifier::implicit;
next();
Expand All @@ -5806,6 +5807,7 @@ class parser

// Handle optional parameter-direction
//
auto parameter_direction_pos = pos; // For backtracking if it's to be an identifier.
if (auto dir = to_passing_style(curr());
dir != passing_style::invalid
)
Expand Down Expand Up @@ -5841,6 +5843,23 @@ class parser
next();
}

// Before the main declaration
// If we're not at an identifier, backtrack to a parsed one.
if (curr().type() != lexeme::Identifier && curr().type() != lexeme::Keyword) {
if (pos != parameter_direction_pos)
{
n->pass = passing_style::in;
pos = parameter_direction_pos;
}
else if (pos != this_specifier_pos)
{
auto mod = std::exchange(n->mod, parameter_declaration_node::modifier::none);
pos = this_specifier_pos;
if (mod == parameter_declaration_node::modifier::virtual_) {
error("invalid parameter identifier");
}
}
}
// Now the main declaration
//
if (!(n->declaration = declaration(false, true, is_template))) {
Expand Down