Skip to content

Commit

Permalink
gccrs: Add missing name resolution to static items in blocks
Browse files Browse the repository at this point in the history
We need to add name resolution and hir lowering for items as part of blocks
in order to typecheck and compile them correctly.

Fixes #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 <[email protected]>
  • Loading branch information
philberty committed Jan 13, 2025
1 parent 5db9ab5 commit 901125b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gcc/rust/hir/rust-ast-lower-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-ast-lower-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ASTLoweringStmt : public ASTLoweringBase
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) {}
Expand Down
21 changes: 21 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions gcc/rust/resolve/rust-ast-resolve-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ class ResolveStmt : public ResolverBase
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,
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/issue-3350.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
static FOO: i32 = 0;

pub fn bar() -> i32 {
FOO
}

pub fn baz() -> i32 {
static QUX: i32 = 0;
QUX
}

0 comments on commit 901125b

Please sign in to comment.