gccrs: Add validation pass for label name

Prevent from using reserved keyword in label name.

gcc/rust/ChangeLog:

	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check if there is
	a label before visit.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Emit an
	error when a label has a forbidden name.
	* checks/errors/rust-ast-validation.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
This commit is contained in:
Pierre-Emmanuel Patry 2023-11-08 16:50:50 +01:00 committed by Arthur Cohen
parent ea59190d54
commit 93ca83c5ef
3 changed files with 19 additions and 1 deletions

View file

@ -494,7 +494,9 @@ void
DefaultASTVisitor::visit (AST::BreakExpr &expr)
{
visit_outer_attrs (expr);
visit (expr.get_label ());
if (expr.has_label ())
visit (expr.get_label ());
if (expr.has_break_expr ())
visit (expr.get_break_expr ());
}

View file

@ -36,6 +36,21 @@ ASTValidation::visit (AST::Lifetime &lifetime)
AST::ContextualASTVisitor::visit (lifetime);
}
void
ASTValidation::visit (AST::LoopLabel &label)
{
auto name = label.get_lifetime ().get_lifetime_name ();
auto lifetime_name = '\'' + name;
auto &keywords = Values::Keywords::keywords;
if (keywords.find (name) != keywords.end ())
rust_error_at (label.get_locus (), "invalid label name %qs",
lifetime_name.c_str ());
// WARNING: Do not call ContextualASTVisitor, we don't want to visit the
// lifetime
// Maybe we should refactor LoopLabel instead ?
}
void
ASTValidation::visit (AST::ConstantItem &const_item)
{

View file

@ -35,6 +35,7 @@ public:
virtual void visit (AST::ConstantItem &const_item);
virtual void visit (AST::Lifetime &lifetime);
virtual void visit (AST::LoopLabel &label);
};
} // namespace Rust