]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add checks for Trait functions
authorKushal Pal <kushalpal109@gmail.com>
Fri, 26 Jan 2024 05:55:10 +0000 (11:25 +0530)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 7 Feb 2024 11:40:23 +0000 (12:40 +0100)
Since we want to use AST::Function class for trait functions as well, we
need to check against specific conditions in ASTValidation phase.

gcc/rust/ChangeLog:

* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
Add checks for Trait functions.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
gcc/rust/checks/errors/rust-ast-validation.cc

index ccb071f74b09055067f24dacea46abb5393eefc3..d1c8273a0b3f8c24c8d2d07d7acb8e366ef4c36f 100644 (file)
@@ -95,24 +95,28 @@ ASTValidation::visit (AST::Union &item)
 void
 ASTValidation::visit (AST::Function &function)
 {
-  std::set<Context> valid_context
-    = {Context::INHERENT_IMPL, Context::TRAIT_IMPL};
-
   const auto &qualifiers = function.get_qualifiers ();
   if (qualifiers.is_async () && qualifiers.is_const ())
     rust_error_at (function.get_locus (),
                   "functions cannot be both %<const%> and %<async%>");
 
-  if (qualifiers.is_const () && context.back () == Context::TRAIT_IMPL)
+  if (qualifiers.is_const ()
+      && (context.back () == Context::TRAIT_IMPL
+         || context.back () == Context::TRAIT))
     rust_error_at (function.get_locus (), ErrorCode::E0379,
-                  "functions in traits cannot be declared const");
+                  "functions in traits cannot be declared %<const%>");
 
   // may change soon
-  if (qualifiers.is_async () && context.back () == Context::TRAIT_IMPL)
+  if (qualifiers.is_async ()
+      && (context.back () == Context::TRAIT_IMPL
+         || context.back () == Context::TRAIT))
     rust_error_at (function.get_locus (), ErrorCode::E0706,
                   "functions in traits cannot be declared %<async%>");
 
-  if (valid_context.find (context.back ()) == valid_context.end ()
+  // if not an associated function but has a self parameter
+  if (context.back () != Context::TRAIT
+      && context.back () != Context::TRAIT_IMPL
+      && context.back () != Context::INHERENT_IMPL
       && function.has_self_param ())
     rust_error_at (
       function.get_self_param ()->get_locus (),