]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Emit error on lonely self use declaration
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 26 Aug 2025 15:15:47 +0000 (17:15 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 19:58:51 +0000 (20:58 +0100)
gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::visit): Emit an error
on top level rebind self use declaration.

gcc/testsuite/ChangeLog:

* rust/compile/use_self_alone.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/testsuite/rust/compile/use_self_alone.rs [new file with mode: 0644]

index 0f1766c18158838bee6e3de5e3a12192672518f7..6036f474afc60a2104873426242172a538d26049 100644 (file)
@@ -459,6 +459,20 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
 void
 Early::visit (AST::UseDeclaration &decl)
 {
+  // We do not want to visit the use trees, we're only looking for top level
+  // rebind. eg. `use something;` or `use something::other;`
+  if (decl.get_tree ()->get_kind () == AST::UseTree::Kind::Rebind)
+    {
+      auto &rebind = static_cast<AST::UseTreeRebind &> (*decl.get_tree ());
+      if (rebind.get_path ().get_final_segment ().is_lower_self_seg ())
+       {
+         collect_error (
+           Error (decl.get_locus (), ErrorCode::E0429,
+                  "%<self%> imports are only allowed within a { } list"));
+         return;
+       }
+    }
+
   auto &imports = toplevel.get_imports_to_resolve ();
   auto current_import = imports.find (decl.get_node_id ());
   if (current_import != imports.end ())
diff --git a/gcc/testsuite/rust/compile/use_self_alone.rs b/gcc/testsuite/rust/compile/use_self_alone.rs
new file mode 100644 (file)
index 0000000..1df923c
--- /dev/null
@@ -0,0 +1,2 @@
+use self;
+// { dg-error ".self. imports are only allowed within a { } list" "" { target *-*-* } .-1 }