From: Philip Herron Date: Tue, 30 May 2023 12:45:58 +0000 (+0100) Subject: gccrs: Add missing name resolution to item statements X-Git-Tag: basepoints/gcc-15~2514 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56e29f4d5de8134db5f7772adc13154f3efccf43;p=thirdparty%2Fgcc.git gccrs: Add missing name resolution to item statements 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 --- diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.cc b/gcc/rust/resolve/rust-ast-resolve-stmt.cc index 01e848424bf0..04dfdfed0ff6 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.cc +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // . +#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 diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h index 382192034502..99680684a59d 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.h +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h @@ -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, diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 60e63bcd5426..101ed082a277 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -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 index 000000000000..b0c7e36ea445 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-2238.rs @@ -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(); +}