diff mbox series

[COMMITTED,20/25] gccrs: Add checks for Trait functions

Message ID 20240207114419.1100894-21-arthur.cohen@embecosm.com
State New
Headers show
Series [COMMITTED,01/25] gccrs: Parse normal functions with `self` parameter correctly | expand

Commit Message

Arthur Cohen Feb. 7, 2024, 11:44 a.m. UTC
From: Kushal Pal <kushalpal109@gmail.com>

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 | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc
index ccb071f74b0..d1c8273a0b3 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.cc
+++ b/gcc/rust/checks/errors/rust-ast-validation.cc
@@ -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 (),