Skip to content

Commit 81d5d02

Browse files
committed
Rename Module field anonymous_children to module_children, expand it to include both named an anonymous modules, and fix #31644
1 parent 4af8564 commit 81d5d02

File tree

4 files changed

+19
-68
lines changed

4 files changed

+19
-68
lines changed

src/librustc_resolve/build_reduced_graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
306306
let def = Def::Mod(self.ast_map.local_def_id(item.id));
307307
let module = self.new_module(parent_link, Some(def), false, is_public);
308308
self.define(parent, name, TypeNS, (module, sp));
309+
parent.module_children.borrow_mut().insert(item.id, module);
309310
module
310311
}
311312

@@ -474,7 +475,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
474475

475476
let parent_link = BlockParentLink(parent, block_id);
476477
let new_module = self.new_module(parent_link, None, false, false);
477-
parent.anonymous_children.borrow_mut().insert(block_id, new_module);
478+
parent.module_children.borrow_mut().insert(block_id, new_module);
478479
new_module
479480
} else {
480481
parent

src/librustc_resolve/lib.rs

+11-50
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,9 @@ pub struct ModuleS<'a> {
801801
resolutions: RefCell<HashMap<(Name, Namespace), NameResolution<'a>>>,
802802
imports: RefCell<Vec<ImportDirective>>,
803803

804-
// The anonymous children of this node. Anonymous children are pseudo-
805-
// modules that are implicitly created around items contained within
806-
// blocks.
804+
// The module children of this node, including normal modules and anonymous modules.
805+
// Anonymous children are pseudo-modules that are implicitly created around items
806+
// contained within blocks.
807807
//
808808
// For example, if we have this:
809809
//
@@ -815,7 +815,7 @@ pub struct ModuleS<'a> {
815815
//
816816
// There will be an anonymous module created around `g` with the ID of the
817817
// entry block for `f`.
818-
anonymous_children: RefCell<NodeMap<Module<'a>>>,
818+
module_children: RefCell<NodeMap<Module<'a>>>,
819819

820820
shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
821821

@@ -848,7 +848,7 @@ impl<'a> ModuleS<'a> {
848848
is_extern_crate: false,
849849
resolutions: RefCell::new(HashMap::new()),
850850
imports: RefCell::new(Vec::new()),
851-
anonymous_children: RefCell::new(NodeMap()),
851+
module_children: RefCell::new(NodeMap()),
852852
shadowed_traits: RefCell::new(Vec::new()),
853853
glob_count: Cell::new(0),
854854
pub_count: Cell::new(0),
@@ -906,14 +906,6 @@ impl<'a> ModuleS<'a> {
906906
}
907907
}
908908

909-
fn for_each_local_child<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&self, mut f: F) {
910-
self.for_each_child(|name, ns, name_binding| {
911-
if !name_binding.is_import() && !name_binding.is_extern_crate() {
912-
f(name, ns, name_binding)
913-
}
914-
})
915-
}
916-
917909
fn def_id(&self) -> Option<DefId> {
918910
self.def.as_ref().map(Def::def_id)
919911
}
@@ -1640,20 +1632,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16401632
}
16411633

16421634
// Descend into children and anonymous children.
1643-
build_reduced_graph::populate_module_if_necessary(self, module_);
1644-
1645-
module_.for_each_local_child(|_, _, child_node| {
1646-
match child_node.module() {
1647-
None => {
1648-
// Continue.
1649-
}
1650-
Some(child_module) => {
1651-
self.report_unresolved_imports(child_module);
1652-
}
1653-
}
1654-
});
1655-
1656-
for (_, module_) in module_.anonymous_children.borrow().iter() {
1635+
for (_, module_) in module_.module_children.borrow().iter() {
16571636
self.report_unresolved_imports(module_);
16581637
}
16591638
}
@@ -1676,32 +1655,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16761655
// generate a fake "implementation scope" containing all the
16771656
// implementations thus found, for compatibility with old resolve pass.
16781657

1679-
fn with_scope<F>(&mut self, name: Option<Name>, f: F)
1658+
fn with_scope<F>(&mut self, id: NodeId, f: F)
16801659
where F: FnOnce(&mut Resolver)
16811660
{
16821661
let orig_module = self.current_module;
16831662

16841663
// Move down in the graph.
1685-
match name {
1686-
None => {
1687-
// Nothing to do.
1688-
}
1689-
Some(name) => {
1690-
build_reduced_graph::populate_module_if_necessary(self, &orig_module);
1691-
1692-
if let Success(name_binding) = orig_module.resolve_name(name, TypeNS, false) {
1693-
match name_binding.module() {
1694-
None => {
1695-
debug!("!!! (with scope) didn't find module for `{}` in `{}`",
1696-
name,
1697-
module_to_string(orig_module));
1698-
}
1699-
Some(module) => {
1700-
self.current_module = module;
1701-
}
1702-
}
1703-
}
1704-
}
1664+
if let Some(module) = orig_module.module_children.borrow().get(&id) {
1665+
self.current_module = module;
17051666
}
17061667

17071668
f(self);
@@ -1825,7 +1786,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18251786
}
18261787

18271788
ItemMod(_) | ItemForeignMod(_) => {
1828-
self.with_scope(Some(name), |this| {
1789+
self.with_scope(item.id, |this| {
18291790
intravisit::walk_item(this, item);
18301791
});
18311792
}
@@ -2261,7 +2222,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
22612222
// Move down in the graph, if there's an anonymous module rooted here.
22622223
let orig_module = self.current_module;
22632224
let anonymous_module =
2264-
orig_module.anonymous_children.borrow().get(&block.id).map(|module| *module);
2225+
orig_module.module_children.borrow().get(&block.id).map(|module| *module);
22652226

22662227
if let Some(anonymous_module) = anonymous_module {
22672228
debug!("(resolving block) found anonymous module, moving down");

src/librustc_resolve/resolve_imports.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
243243
errors.extend(self.resolve_imports_for_module(module_));
244244
self.resolver.current_module = orig_module;
245245

246-
build_reduced_graph::populate_module_if_necessary(self.resolver, module_);
247-
module_.for_each_local_child(|_, _, child_node| {
248-
match child_node.module() {
249-
None => {
250-
// Nothing to do.
251-
}
252-
Some(child_module) => {
253-
errors.extend(self.resolve_imports_for_module_subtree(child_module));
254-
}
255-
}
256-
});
257-
258-
for (_, child_module) in module_.anonymous_children.borrow().iter() {
246+
for (_, child_module) in module_.module_children.borrow().iter() {
259247
errors.extend(self.resolve_imports_for_module_subtree(child_module));
260248
}
261249

src/test/compile-fail/enum-and-module-in-same-scope.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
mod Foo {
12-
pub static X: isize = 42;
11+
enum Foo {
12+
X
1313
}
1414

15-
enum Foo { //~ ERROR duplicate definition of type or module `Foo`
16-
X
15+
mod Foo { //~ ERROR duplicate definition of type or module `Foo`
16+
pub static X: isize = 42;
17+
fn f() { f() } // Check that this does not result in a resolution error
1718
}
1819

1920
fn main() {}

0 commit comments

Comments
 (0)