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 Dec 1, 2023
1 parent 5210b2b commit 0ff1dcb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,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 @@ -42,6 +42,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 0ff1dcb

Please sign in to comment.