Skip to content

Commit

Permalink
Added support to Parse ASYNC function
Browse files Browse the repository at this point in the history
Fixes issue Rust-GCC#2650
The parser now parses ASYNC functions. Added ASYNC case to parse_item
Added a new function parse_async_item which is called in
parse_vis_item to handle the ASYNC case. Parse_async_item
also checks the current Rust edition and generates an error if the
edition is 2015

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_item): Likewise.
	(Parser::parse_vis_item): Likewise.
	(Parser::parse_async_item): Likewise.
	* parse/rust-parse.h: Made declaration for parse_async_item.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2650-1.rs: New test.(edition=2018)
	* rust/compile/issue-2650-2.rs: New test.(edition=2015)

Signed-off-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
  • Loading branch information
mvvsmk committed Nov 21, 2023
1 parent 5f0e77a commit 03fa081
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "rust-dir-owner.h"
#include "rust-attribute-values.h"
#include "rust-keyword-values.h"
#include "rust-session-manager.h"

#include "optional.h"

Expand Down Expand Up @@ -1112,6 +1113,8 @@ Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
add_error (std::move (error));
}
return nullptr;

case ASYNC:
case PUB:
case MOD:
case EXTERN_TOK:
Expand Down Expand Up @@ -1388,6 +1391,10 @@ Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
lexer.skip_token (1); // TODO: is this right thing to do?
return nullptr;
}
// for async functions
case ASYNC:
return parse_async_item (std::move (vis), std::move (outer_attrs));

case STATIC_TOK:
return parse_static_item (std::move (vis), std::move (outer_attrs));
case AUTO:
Expand Down Expand Up @@ -1428,6 +1435,39 @@ Parser<ManagedTokenSource>::parse_vis_item (AST::AttrVec outer_attrs)
return nullptr;
}

template <typename ManagedTokenSource>
std::unique_ptr<AST::Function>
Parser<ManagedTokenSource>::parse_async_item (AST::Visibility vis,
AST::AttrVec outer_attrs)
{
const_TokenPtr t = lexer.peek_token ();
if (Session::get_instance ().options.get_edition ()
== CompileOptions::Edition::E2015)
{
add_error (Error (t->get_locus (), ErrorCode::E0670,
"%<async fn%> is not permitted in Rust 2015"));
add_error (
Error::Hint (t->get_locus (),
"to use %<async fn%>, switch to Rust 2018 or later"));
}

t = lexer.peek_token (1);

switch (t->get_id ())
{
case UNSAFE:
case FN_TOK:
return parse_function (std::move (vis), std::move (outer_attrs));

default:
add_error (
Error (t->get_locus (), "expected item, found keyword %<async%>"));

lexer.skip_token (1);
return nullptr;
}
}

// Parses a macro rules definition syntax extension whatever thing.
template <typename ManagedTokenSource>
std::unique_ptr<AST::MacroRulesDefinition>
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ template <typename ManagedTokenSource> class Parser
std::unique_ptr<AST::ExternBlock>
parse_extern_block (AST::Visibility vis, AST::AttrVec outer_attrs);
std::unique_ptr<AST::Function> parse_method ();
std::unique_ptr<AST::Function> parse_async_item (AST::Visibility vis,
AST::AttrVec outer_attrs);

// Expression-related (Pratt parsed)
std::unique_ptr<AST::Expr>
Expand Down
5 changes: 5 additions & 0 deletions gcc/testsuite/rust/compile/issue-2650-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// { dg-additional-options "-frust-edition=2018" }

pub async fn a() -> u32 {
1
}
5 changes: 5 additions & 0 deletions gcc/testsuite/rust/compile/issue-2650-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// { dg-additional-options "-frust-edition=2015" }

pub async fn a() -> u32 { // { dg-error "'async fn' is not permitted in Rust 2015" }
1
}

0 comments on commit 03fa081

Please sign in to comment.