]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Handle `async` functions in traits
authorKushal Pal <kushalpal109@gmail.com>
Wed, 3 Jan 2024 10:32:07 +0000 (16:02 +0530)
committerCohenArthur <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 14:43:32 +0000 (14:43 +0000)
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 <kushalpal109@gmail.com>
gcc/rust/checks/errors/rust-ast-validation.cc
gcc/rust/parse/rust-parse-impl.h
gcc/testsuite/rust/compile/issue-2785.rs [new file with mode: 0644]

index b50e9cdf8581b8c3e67c190be38b1c1d60c48d09..0ed5d06089431346bb2706e201cba0f15253c926 100644 (file)
@@ -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
index 872c1fae2a3b353d1c47272ba5a9195b3bba5436..54885221e18d125943540a76275836af08e308d4 100644 (file)
@@ -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: {
diff --git a/gcc/testsuite/rust/compile/issue-2785.rs b/gcc/testsuite/rust/compile/issue-2785.rs
new file mode 100644 (file)
index 0000000..08f6346
--- /dev/null
@@ -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() {}