]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add missing name resolution to item statements
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 30 May 2023 12:45:58 +0000 (13:45 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:22 +0000 (18:37 +0100)
This fixes the issue but there are two cleanups to do at some point.

  1. misc namesapce this is a scope AST namespace where we dump resolution
     info when its not defined here. This occurs in the case such as nested
     scopes where the nested scope is popped and we hit an assertion.
     Outside of name resolution this requirement shouldnt really apply
     it should be permissive to allow for this

  2. We reuse our existing name resolution pieces here for Traits and impl
     blocks we should start doing this for the other statements.

Fixes #2238

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): add name resolution
* resolve/rust-ast-resolve-stmt.h: likewise
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): insert resolved node

gcc/testsuite/ChangeLog:

* rust/compile/issue-2238.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/resolve/rust-ast-resolve-stmt.cc
gcc/rust/resolve/rust-ast-resolve-stmt.h
gcc/rust/typecheck/rust-hir-type-check-expr.cc
gcc/testsuite/rust/compile/issue-2238.rs [new file with mode: 0644]

index 01e848424bf0e20a4521b051a49c700668654a5f..04dfdfed0ff63852a233fbfbd3d51768850c8ad3 100644 (file)
@@ -16,6 +16,7 @@
 // along with GCC; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
+#include "rust-ast-resolve-toplevel.h"
 #include "rust-ast-resolve-item.h"
 #include "rust-ast-resolve-stmt.h"
 #include "rust-ast-resolve-implitem.h"
@@ -35,5 +36,26 @@ ResolveStmt::visit (AST::ExternBlock &extern_block)
     }
 }
 
+void
+ResolveStmt::visit (AST::Trait &trait)
+{
+  ResolveTopLevel::go (&trait, prefix, canonical_prefix);
+  ResolveItem::go (&trait, prefix, canonical_prefix);
+}
+
+void
+ResolveStmt::visit (AST::InherentImpl &impl_block)
+{
+  ResolveTopLevel::go (&impl_block, prefix, canonical_prefix);
+  ResolveItem::go (&impl_block, prefix, canonical_prefix);
+}
+
+void
+ResolveStmt::visit (AST::TraitImpl &impl_block)
+{
+  ResolveTopLevel::go (&impl_block, prefix, canonical_prefix);
+  ResolveItem::go (&impl_block, prefix, canonical_prefix);
+}
+
 } // namespace Resolver
 } // namespace Rust
index 382192034502d4f12dc8e48ca9e4ed8c040ea2bb..99680684a59df04e94967c18c3412396bb650355 100644 (file)
@@ -359,6 +359,9 @@ public:
   }
 
   void visit (AST::ExternBlock &extern_block) override;
+  void visit (AST::Trait &trait) override;
+  void visit (AST::InherentImpl &impl_block) override;
+  void visit (AST::TraitImpl &impl_block) override;
 
 private:
   ResolveStmt (const CanonicalPath &prefix,
index 60e63bcd5426ee8d011f433e737cc19485a9653c..101ed082a2776426371e3c8e550ce8d623d76faf 100644 (file)
@@ -1207,8 +1207,16 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
   context->insert_type (expr.get_method_name ().get_mappings (), lookup);
 
   // set up the resolved name on the path
-  resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
-                                 resolved_node_id);
+  if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
+    {
+      resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (),
+                                     resolved_node_id);
+    }
+  else
+    {
+      resolver->insert_resolved_misc (expr.get_mappings ().get_nodeid (),
+                                     resolved_node_id);
+    }
 
   // return the result of the function back
   infered = function_ret_tyty;
diff --git a/gcc/testsuite/rust/compile/issue-2238.rs b/gcc/testsuite/rust/compile/issue-2238.rs
new file mode 100644 (file)
index 0000000..b0c7e36
--- /dev/null
@@ -0,0 +1,15 @@
+fn main() {
+    struct Foo;
+
+    trait Bar {
+        fn foo(&self);
+    }
+
+    impl Bar for Foo {
+        fn foo(&self) {}
+        // { dg-warning "unused name" "" { target *-*-* } .-1 }
+    }
+
+    let s = Foo;
+    s.foo();
+}