Skip to content

Commit

Permalink
Add AST validation check for const in trait
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
P-E-P committed Nov 22, 2023
1 parent d097d19 commit 04d518c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// <http://www.gnu.org/licenses/>.

#include "rust-ast-visitor.h"
#include "rust-ast-full-decls.h"
#include "rust-ast.h"
#include "rust-path.h"
#include "rust-token.h"
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-ast-validation.h"
#include "rust-ast-full-decls.h"
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04d518c

Please sign in to comment.