Skip to content

Commit

Permalink
Handle async functions in traits
Browse files Browse the repository at this point in the history
Fixes #2785

gcc/rust/ChangeLog:

	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Added check for `async` functions inside trait.
	* parse/rust-parse-impl.h (Parser::parse_trait_item):
	Added switch-case for ASYNC token.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2785.rs: New test.

Signed-off-by: Kushal Pal <[email protected]>
  • Loading branch information
braw-lee authored and CohenArthur committed Jan 16, 2024
1 parent 3fcd86e commit f54df95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,16 @@ ASTValidation::visit (AST::TraitFunctionDecl &decl)
{
const auto &qualifiers = decl.get_qualifiers ();

if (context.back () == Context::TRAIT && qualifiers.is_const ())
rust_error_at (decl.get_identifier ().get_locus (), ErrorCode::E0379,
"functions in traits cannot be declared const");
if (context.back () == Context::TRAIT)
{
// may change soon
if (qualifiers.is_async ())
rust_error_at (decl.get_identifier ().get_locus (), ErrorCode::E0706,
"functions in traits cannot be declared %<async%>");
if (qualifiers.is_const ())
rust_error_at (decl.get_identifier ().get_locus (), ErrorCode::E0379,
"functions in traits cannot be declared const");
}
}

void
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5096,6 +5096,7 @@ Parser<ManagedTokenSource>::parse_trait_item ()
// else, fallthrough to function
// TODO: find out how to disable gcc "implicit fallthrough" error
gcc_fallthrough ();
case ASYNC:
case UNSAFE:
case EXTERN_KW:
case FN_KW: {
Expand Down
9 changes: 9 additions & 0 deletions gcc/testsuite/rust/compile/issue-2785.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// { dg-additional-options "-frust-edition=2018" }
trait Foo {
async fn foo(){}
// { dg-error "functions in traits cannot be declared .async." "" { target *-*-* } .-1 }
async fn bar();
// { dg-error "functions in traits cannot be declared .async." "" { target *-*-* } .-1 }
}

fn main() {}

0 comments on commit f54df95

Please sign in to comment.