]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add validation pass for label name
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 8 Nov 2023 15:50:50 +0000 (16:50 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:13:13 +0000 (19:13 +0100)
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>
gcc/rust/ast/rust-ast-visitor.cc
gcc/rust/checks/errors/rust-ast-validation.cc
gcc/rust/checks/errors/rust-ast-validation.h

index 63f850f8c7c2f85cc82b3970706789db23f65952..b9ff2f972803d83e1210a342a40f6a22f203e66d 100644 (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 ());
 }
index 3af5655d7f318d67aa106ec131ffba484f4e934a..44dec61b921b9407202d2e23cfaabc4f082ce09a 100644 (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)
 {
index ef0b2ac05386ae5f5cc5ba23640e9660b0cd15a2..a21bcc44f68bb8b7bb0c71089ffe23a0e50a7a8f 100644 (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