From: Philip Herron Date: Mon, 13 Jan 2025 11:51:51 +0000 (+0000) Subject: gccrs: Add missing name resolution to static items in blocks X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0a935444fbf9e99c7a3eff026e2a7ab910e77151;p=thirdparty%2Fgcc.git gccrs: Add missing name resolution to static items in blocks We need to add name resolution and hir lowering for items as part of blocks in order to typecheck and compile them correctly. Fixes Rust-GCC#3350 gcc/rust/ChangeLog: * hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): hir lowering * hir/rust-ast-lower-stmt.h: likewise * resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): name resolution * resolve/rust-ast-resolve-stmt.h: likewise gcc/testsuite/ChangeLog: * rust/compile/issue-3350.rs: New test. Signed-off-by: Philip Herron --- diff --git a/gcc/rust/hir/rust-ast-lower-stmt.cc b/gcc/rust/hir/rust-ast-lower-stmt.cc index 29da916cefc..761a638a80e 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.cc +++ b/gcc/rust/hir/rust-ast-lower-stmt.cc @@ -163,5 +163,11 @@ ASTLoweringStmt::visit (AST::TraitImpl &impl_block) translated = ASTLoweringItem::translate (impl_block); } +void +ASTLoweringStmt::visit (AST::StaticItem &var) +{ + translated = ASTLoweringItem::translate (var); +} + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h index 111990d7aa8..ed395123859 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.h +++ b/gcc/rust/hir/rust-ast-lower-stmt.h @@ -45,6 +45,7 @@ public: void visit (AST::Trait &trait) override; void visit (AST::InherentImpl &impl_block) override; void visit (AST::TraitImpl &impl_block) override; + void visit (AST::StaticItem &var) override; private: ASTLoweringStmt () : translated (nullptr), terminated (false) {} diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.cc b/gcc/rust/resolve/rust-ast-resolve-stmt.cc index 7b62d1f7f78..ecd19769194 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.cc +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.cc @@ -56,5 +56,26 @@ ResolveStmt::visit (AST::TraitImpl &impl_block) ResolveItem::go (impl_block, prefix, canonical_prefix); } +void +ResolveStmt::visit (AST::StaticItem &var) +{ + auto decl = CanonicalPath::new_seg (var.get_node_id (), + var.get_identifier ().as_string ()); + auto path = decl; + auto cpath = canonical_prefix.append (decl); + mappings.insert_canonical_path (var.get_node_id (), cpath); + + resolver->get_name_scope ().insert ( + path, var.get_node_id (), var.get_locus (), false, Rib::ItemType::Static, + [&] (const CanonicalPath &, NodeId, location_t locus) -> void { + rich_location r (line_table, var.get_locus ()); + r.add_range (locus); + rust_error_at (r, "redefined multiple times"); + }); + + ResolveType::go (var.get_type ()); + ResolveExpr::go (var.get_expr (), path, cpath); +} + } // 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 7118b70d705..fd9880cc21d 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.h +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h @@ -388,6 +388,7 @@ public: void visit (AST::Trait &trait) override; void visit (AST::InherentImpl &impl_block) override; void visit (AST::TraitImpl &impl_block) override; + void visit (AST::StaticItem &var) override; private: ResolveStmt (const CanonicalPath &prefix, diff --git a/gcc/testsuite/rust/compile/issue-3350.rs b/gcc/testsuite/rust/compile/issue-3350.rs new file mode 100644 index 00000000000..8880659afe6 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3350.rs @@ -0,0 +1,10 @@ +static FOO: i32 = 0; + +pub fn bar() -> i32 { + FOO +} + +pub fn baz() -> i32 { + static QUX: i32 = 0; + QUX +}