From 04d518c6f15a1147bc205f1b5abbbaf023a10b9e Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 22 Nov 2023 13:08:13 +0100 Subject: [PATCH] Add AST validation check for const in trait Add a new check in AST validation pass that checks that no function declaration in traits are declared const. gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add const check. * checks/errors/rust-ast-validation.h: Add visit function prototype. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-ast-visitor.cc | 1 - gcc/rust/checks/errors/rust-ast-validation.cc | 11 +++++++++++ gcc/rust/checks/errors/rust-ast-validation.h | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index 4ec5c7cf1d0a..c1d6ec339231 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -17,7 +17,6 @@ // . #include "rust-ast-visitor.h" -#include "rust-ast-full-decls.h" #include "rust-ast.h" #include "rust-path.h" #include "rust-token.h" diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 477e889c5129..8eac7b494025 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-validation.h" +#include "rust-ast-full-decls.h" #include "rust-diagnostics.h" #include "rust-item.h" #include "rust-keyword-values.h" @@ -116,6 +117,16 @@ ASTValidation::visit (AST::Function &function) AST::ContextualASTVisitor::visit (function); } +void +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"); +} + void ASTValidation::visit (AST::Trait &trait) { diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h index 1052168ea728..f1c3018d142d 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.h +++ b/gcc/rust/checks/errors/rust-ast-validation.h @@ -41,6 +41,7 @@ class ASTValidation : public AST::ContextualASTVisitor virtual void visit (AST::Union &item); virtual void visit (AST::Function &function); virtual void visit (AST::Trait &trait); + virtual void visit (AST::TraitFunctionDecl &decl); }; } // namespace Rust