Skip to content

Commit 05bcd32

Browse files
committed
nr: Add modules to types NS
gcc/rust/ChangeLog: * util/rust-hir-map.cc (Mappings::insert_module_id): New function. (Mappings::is_module): Likewise. * util/rust-hir-map.h: Store a set of AST modules, declare functions for adding and retrieving them. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Insert modules in the type namespace and store them in mappings. * resolve/rust-late-name-resolver-2.0.cc (resolve_type_path_like): Error out when an expected type resolves to a module. gcc/testsuite/ChangeLog: * rust/compile/mod_in_types_ns.rs: New test. * rust/compile/mod_in_types_ns2.rs: New test.
1 parent d2f8f64 commit 05bcd32

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

gcc/rust/resolve/rust-late-name-resolver-2.0.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,14 @@ resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self,
517517
"declared identifiers");
518518
}
519519

520+
if (Analysis::Mappings::get ().is_module (resolved->get_node_id ()))
521+
{
522+
rust_error_at (type.get_locus (), ErrorCode::E0573,
523+
"expected type, found module %qs",
524+
unwrap_segment_error_string (type).c_str ());
525+
return;
526+
}
527+
520528
ctx.map_usage (Usage (type.get_node_id ()),
521529
Definition (resolved->get_node_id ()));
522530
}

gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ TopLevel::go (AST::Crate &crate)
106106
void
107107
TopLevel::visit (AST::Module &module)
108108
{
109-
DefaultResolver::visit (module);
110-
111109
if (Analysis::Mappings::get ().lookup_glob_container (module.get_node_id ())
112110
== tl::nullopt)
113111
Analysis::Mappings::get ().insert_glob_container (module.get_node_id (),
114112
&module);
113+
114+
insert_or_error_out (module.get_name (), module, Namespace::Types);
115+
116+
Analysis::Mappings::get ().insert_module_id (module.get_node_id ());
117+
118+
DefaultResolver::visit (module);
115119
}
116120

117121
void

gcc/rust/util/rust-hir-map.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,18 @@ Mappings::insert_glob_container (NodeId id, AST::GlobContainer *container)
11681168
glob_containers[id] = container;
11691169
}
11701170

1171+
void
1172+
Mappings::insert_module_id (NodeId id)
1173+
{
1174+
module_ids.insert (id);
1175+
}
1176+
1177+
bool
1178+
Mappings::is_module (NodeId id)
1179+
{
1180+
return module_ids.find (id) != module_ids.end ();
1181+
}
1182+
11711183
tl::optional<AST::GlobContainer *>
11721184
Mappings::lookup_glob_container (NodeId id)
11731185
{

gcc/rust/util/rust-hir-map.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ class Mappings
323323

324324
void insert_glob_container (NodeId, AST::GlobContainer *);
325325
tl::optional<AST::GlobContainer *> lookup_glob_container (NodeId id);
326+
327+
void insert_module_id (NodeId);
328+
bool is_module (NodeId id);
329+
326330
void insert_module_child (NodeId module, NodeId child);
327331
tl::optional<std::vector<NodeId> &> lookup_module_children (NodeId module);
328332

@@ -438,10 +442,13 @@ class Mappings
438442
// Module tree maps
439443

440444
// Maps each module's node id to a list of its children
445+
// TODO: I think these are only used by the old resolved and can be removed
441446
std::map<NodeId, std::vector<NodeId>> module_child_map;
442447
std::map<NodeId, std::vector<Resolver::CanonicalPath>> module_child_items;
443448
std::map<NodeId, NodeId> child_to_parent_module_map;
449+
444450
std::map<NodeId, AST::GlobContainer *> glob_containers;
451+
std::set<NodeId> module_ids;
445452

446453
// AST mappings
447454
std::map<NodeId, AST::Item *> ast_item_mappings;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod foo {
2+
mod bar {}
3+
4+
struct bar; // { dg-error "defined multiple times" }
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod foo {
2+
mod bar {}
3+
4+
fn baz() -> bar {} // { dg-error "expected type, found module" }
5+
}

0 commit comments

Comments
 (0)