Skip to content

Commit c2aaad4

Browse files
committed
Auto merge of #33060 - jseyfried:cleanup_resolve, r=nrc
resolve: miscellaneous clean-ups This PR consists of some small, miscellaneous clean-ups in `resolve`. r? @eddyb
2 parents 478a33d + 1e134a4 commit c2aaad4

File tree

3 files changed

+62
-80
lines changed

3 files changed

+62
-80
lines changed

src/librustc_resolve/build_reduced_graph.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,9 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
177177
}
178178

179179
let subclass = ImportDirectiveSubclass::single(binding, source_name);
180+
let span = view_path.span;
181+
parent.add_import_directive(module_path, subclass, span, item.id, vis);
180182
self.unresolved_imports += 1;
181-
parent.add_import_directive(module_path,
182-
subclass,
183-
view_path.span,
184-
item.id,
185-
vis,
186-
is_prelude);
187183
}
188184
ViewPathList(_, ref source_items) => {
189185
// Make sure there's at most one `mod` import in the list.
@@ -228,23 +224,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
228224
}
229225
};
230226
let subclass = ImportDirectiveSubclass::single(rename, name);
227+
let (span, id) = (source_item.span, source_item.node.id());
228+
parent.add_import_directive(module_path, subclass, span, id, vis);
231229
self.unresolved_imports += 1;
232-
parent.add_import_directive(module_path,
233-
subclass,
234-
source_item.span,
235-
source_item.node.id(),
236-
vis,
237-
is_prelude);
238230
}
239231
}
240232
ViewPathGlob(_) => {
233+
let subclass = GlobImport { is_prelude: is_prelude };
234+
let span = view_path.span;
235+
parent.add_import_directive(module_path, subclass, span, item.id, vis);
241236
self.unresolved_imports += 1;
242-
parent.add_import_directive(module_path,
243-
GlobImport,
244-
view_path.span,
245-
item.id,
246-
vis,
247-
is_prelude);
248237
}
249238
}
250239
}
@@ -271,7 +260,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
271260
let def = Def::Mod(self.ast_map.local_def_id(item.id));
272261
let module = self.new_module(parent_link, Some(def), false, vis);
273262
self.define(parent, name, TypeNS, (module, sp));
274-
parent.module_children.borrow_mut().insert(item.id, module);
263+
self.module_map.insert(item.id, module);
275264
*parent_ref = module;
276265
}
277266

@@ -409,7 +398,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
409398

410399
let parent_link = BlockParentLink(parent, block_id);
411400
let new_module = self.new_module(parent_link, None, false, parent.vis);
412-
parent.module_children.borrow_mut().insert(block_id, new_module);
401+
self.module_map.insert(block_id, new_module);
413402
*parent = new_module;
414403
}
415404
}

src/librustc_resolve/lib.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -827,22 +827,6 @@ pub struct ModuleS<'a> {
827827
resolutions: RefCell<HashMap<(Name, Namespace), &'a RefCell<NameResolution<'a>>>>,
828828
unresolved_imports: RefCell<Vec<&'a ImportDirective<'a>>>,
829829

830-
// The module children of this node, including normal modules and anonymous modules.
831-
// Anonymous children are pseudo-modules that are implicitly created around items
832-
// contained within blocks.
833-
//
834-
// For example, if we have this:
835-
//
836-
// fn f() {
837-
// fn g() {
838-
// ...
839-
// }
840-
// }
841-
//
842-
// There will be an anonymous module created around `g` with the ID of the
843-
// entry block for `f`.
844-
module_children: RefCell<NodeMap<Module<'a>>>,
845-
846830
prelude: RefCell<Option<Module<'a>>>,
847831

848832
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective<'a>)>>,
@@ -874,7 +858,6 @@ impl<'a> ModuleS<'a> {
874858
extern_crate_id: None,
875859
resolutions: RefCell::new(HashMap::new()),
876860
unresolved_imports: RefCell::new(Vec::new()),
877-
module_children: RefCell::new(NodeMap()),
878861
prelude: RefCell::new(None),
879862
glob_importers: RefCell::new(Vec::new()),
880863
globs: RefCell::new((Vec::new())),
@@ -1082,6 +1065,22 @@ pub struct Resolver<'a, 'tcx: 'a> {
10821065
export_map: ExportMap,
10831066
trait_map: TraitMap,
10841067

1068+
// A map from nodes to modules, both normal (`mod`) modules and anonymous modules.
1069+
// Anonymous modules are pseudo-modules that are implicitly created around items
1070+
// contained within blocks.
1071+
//
1072+
// For example, if we have this:
1073+
//
1074+
// fn f() {
1075+
// fn g() {
1076+
// ...
1077+
// }
1078+
// }
1079+
//
1080+
// There will be an anonymous module created around `g` with the ID of the
1081+
// entry block for `f`.
1082+
module_map: NodeMap<Module<'a>>,
1083+
10851084
// Whether or not to print error messages. Can be set to true
10861085
// when getting additional info for error message suggestions,
10871086
// so as to avoid printing duplicate errors
@@ -1107,14 +1106,22 @@ pub struct Resolver<'a, 'tcx: 'a> {
11071106

11081107
struct ResolverArenas<'a> {
11091108
modules: arena::TypedArena<ModuleS<'a>>,
1109+
local_modules: RefCell<Vec<Module<'a>>>,
11101110
name_bindings: arena::TypedArena<NameBinding<'a>>,
11111111
import_directives: arena::TypedArena<ImportDirective<'a>>,
11121112
name_resolutions: arena::TypedArena<RefCell<NameResolution<'a>>>,
11131113
}
11141114

11151115
impl<'a> ResolverArenas<'a> {
11161116
fn alloc_module(&'a self, module: ModuleS<'a>) -> Module<'a> {
1117-
self.modules.alloc(module)
1117+
let module = self.modules.alloc(module);
1118+
if module.def_id().map(|def_id| def_id.is_local()).unwrap_or(true) {
1119+
self.local_modules.borrow_mut().push(module);
1120+
}
1121+
module
1122+
}
1123+
fn local_modules(&'a self) -> ::std::cell::Ref<'a, Vec<Module<'a>>> {
1124+
self.local_modules.borrow()
11181125
}
11191126
fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
11201127
self.name_bindings.alloc(name_binding)
@@ -1175,6 +1182,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11751182
freevars_seen: NodeMap(),
11761183
export_map: NodeMap(),
11771184
trait_map: NodeMap(),
1185+
module_map: NodeMap(),
11781186
used_imports: HashSet::new(),
11791187
used_crates: HashSet::new(),
11801188

@@ -1193,6 +1201,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11931201
fn arenas() -> ResolverArenas<'a> {
11941202
ResolverArenas {
11951203
modules: arena::TypedArena::new(),
1204+
local_modules: RefCell::new(Vec::new()),
11961205
name_bindings: arena::TypedArena::new(),
11971206
import_directives: arena::TypedArena::new(),
11981207
name_resolutions: arena::TypedArena::new(),
@@ -1573,7 +1582,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15731582
fn with_scope<F>(&mut self, id: NodeId, f: F)
15741583
where F: FnOnce(&mut Resolver)
15751584
{
1576-
if let Some(module) = self.current_module.module_children.borrow().get(&id) {
1585+
let module = self.module_map.get(&id).cloned(); // clones a reference
1586+
if let Some(module) = module {
15771587
// Move down in the graph.
15781588
let orig_module = ::std::mem::replace(&mut self.current_module, module);
15791589
self.value_ribs.push(Rib::new(ModuleRibKind(module)));
@@ -2124,8 +2134,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21242134
debug!("(resolving block) entering block");
21252135
// Move down in the graph, if there's an anonymous module rooted here.
21262136
let orig_module = self.current_module;
2127-
let anonymous_module =
2128-
orig_module.module_children.borrow().get(&block.id).map(|module| *module);
2137+
let anonymous_module = self.module_map.get(&block.id).cloned(); // clones a reference
21292138

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

src/librustc_resolve/resolve_imports.rs

+23-39
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use syntax::attr::AttrMetaMethods;
2929
use syntax::codemap::Span;
3030
use syntax::util::lev_distance::find_best_match_for_name;
3131

32-
use std::mem::replace;
3332
use std::cell::{Cell, RefCell};
3433

3534
/// Contains data for specific types of import directives.
@@ -41,7 +40,7 @@ pub enum ImportDirectiveSubclass {
4140
type_determined: Cell<bool>,
4241
value_determined: Cell<bool>,
4342
},
44-
GlobImport,
43+
GlobImport { is_prelude: bool },
4544
}
4645

4746
impl ImportDirectiveSubclass {
@@ -64,7 +63,6 @@ pub struct ImportDirective<'a> {
6463
subclass: ImportDirectiveSubclass,
6564
span: Span,
6665
vis: ty::Visibility, // see note in ImportResolutionPerNamespace about how to use this
67-
is_prelude: bool,
6866
}
6967

7068
impl<'a> ImportDirective<'a> {
@@ -84,7 +82,7 @@ impl<'a> ImportDirective<'a> {
8482
}
8583

8684
pub fn is_glob(&self) -> bool {
87-
match self.subclass { ImportDirectiveSubclass::GlobImport => true, _ => false }
85+
match self.subclass { ImportDirectiveSubclass::GlobImport { .. } => true, _ => false }
8886
}
8987
}
9088

@@ -191,7 +189,7 @@ impl<'a> NameResolution<'a> {
191189
};
192190
let name = match directive.subclass {
193191
SingleImport { source, .. } => source,
194-
GlobImport => unreachable!(),
192+
GlobImport { .. } => unreachable!(),
195193
};
196194
match target_module.resolve_name(name, ns, false) {
197195
Failed(_) => {}
@@ -282,16 +280,14 @@ impl<'a> ::ModuleS<'a> {
282280
subclass: ImportDirectiveSubclass,
283281
span: Span,
284282
id: NodeId,
285-
vis: ty::Visibility,
286-
is_prelude: bool) {
283+
vis: ty::Visibility) {
287284
let directive = self.arenas.alloc_import_directive(ImportDirective {
288285
module_path: module_path,
289286
target_module: Cell::new(None),
290287
subclass: subclass,
291288
span: span,
292289
id: id,
293290
vis: vis,
294-
is_prelude: is_prelude,
295291
});
296292

297293
self.unresolved_imports.borrow_mut().push(directive);
@@ -304,8 +300,8 @@ impl<'a> ::ModuleS<'a> {
304300
}
305301
// We don't add prelude imports to the globs since they only affect lexical scopes,
306302
// which are not relevant to import resolution.
307-
GlobImport if directive.is_prelude => {}
308-
GlobImport => self.globs.borrow_mut().push(directive),
303+
GlobImport { is_prelude: true } => {}
304+
GlobImport { .. } => self.globs.borrow_mut().push(directive),
309305
}
310306
}
311307

@@ -374,11 +370,17 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
374370
i,
375371
self.resolver.unresolved_imports);
376372

377-
self.resolve_imports_for_module_subtree(self.resolver.graph_root, &mut errors);
373+
// Attempt to resolve imports in all local modules.
374+
for module in self.resolver.arenas.local_modules().iter() {
375+
self.resolver.current_module = module;
376+
self.resolve_imports_in_current_module(&mut errors);
377+
}
378378

379379
if self.resolver.unresolved_imports == 0 {
380380
debug!("(resolving imports) success");
381-
self.finalize_resolutions(self.resolver.graph_root, false);
381+
for module in self.resolver.arenas.local_modules().iter() {
382+
self.finalize_resolutions_in(module, false);
383+
}
382384
break;
383385
}
384386

@@ -388,7 +390,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
388390
// to avoid generating multiple errors on the same import.
389391
// Imports that are still indeterminate at this point are actually blocked
390392
// by errored imports, so there is no point reporting them.
391-
self.finalize_resolutions(self.resolver.graph_root, errors.len() == 0);
393+
for module in self.resolver.arenas.local_modules().iter() {
394+
self.finalize_resolutions_in(module, errors.len() == 0);
395+
}
392396
for e in errors {
393397
self.import_resolving_error(e)
394398
}
@@ -425,22 +429,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
425429
ResolutionError::UnresolvedImport(Some((&path, &e.help))));
426430
}
427431

428-
/// Attempts to resolve imports for the given module and all of its
429-
/// submodules.
430-
fn resolve_imports_for_module_subtree(&mut self,
431-
module_: Module<'b>,
432-
errors: &mut Vec<ImportResolvingError<'b>>) {
433-
debug!("(resolving imports for module subtree) resolving {}",
434-
module_to_string(&module_));
435-
let orig_module = replace(&mut self.resolver.current_module, module_);
436-
self.resolve_imports_in_current_module(errors);
437-
self.resolver.current_module = orig_module;
438-
439-
for (_, child_module) in module_.module_children.borrow().iter() {
440-
self.resolve_imports_for_module_subtree(child_module, errors);
441-
}
442-
}
443-
444432
/// Attempts to resolve imports for the given module only.
445433
fn resolve_imports_in_current_module(&mut self, errors: &mut Vec<ImportResolvingError<'b>>) {
446434
let mut imports = Vec::new();
@@ -496,7 +484,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
496484
let (source, target, value_determined, type_determined) = match directive.subclass {
497485
SingleImport { source, target, ref value_determined, ref type_determined } =>
498486
(source, target, value_determined, type_determined),
499-
GlobImport => return self.resolve_glob_import(target_module, directive),
487+
GlobImport { .. } => return self.resolve_glob_import(target_module, directive),
500488
};
501489

502490
// We need to resolve both namespaces for this to succeed.
@@ -644,7 +632,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
644632
}
645633
self.resolver.populate_module_if_necessary(target_module);
646634

647-
if directive.is_prelude {
635+
if let GlobImport { is_prelude: true } = directive.subclass {
648636
*module_.prelude.borrow_mut() = Some(target_module);
649637
return Success(());
650638
}
@@ -676,9 +664,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
676664
return Success(());
677665
}
678666

679-
// Miscellaneous post-processing, including recording reexports, recording shadowed traits,
680-
// reporting conflicts, reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
681-
fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
667+
// Miscellaneous post-processing, including recording reexports, reporting conflicts,
668+
// reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
669+
fn finalize_resolutions_in(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
682670
// Since import resolution is finished, globs will not define any more names.
683671
*module.globs.borrow_mut() = Vec::new();
684672

@@ -726,10 +714,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
726714
break;
727715
}
728716
}
729-
730-
for (_, child) in module.module_children.borrow().iter() {
731-
self.finalize_resolutions(child, report_unresolved_imports);
732-
}
733717
}
734718
}
735719

@@ -747,7 +731,7 @@ fn import_path_to_string(names: &[Name], subclass: &ImportDirectiveSubclass) ->
747731
fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> String {
748732
match *subclass {
749733
SingleImport { source, .. } => source.to_string(),
750-
GlobImport => "*".to_string(),
734+
GlobImport { .. } => "*".to_string(),
751735
}
752736
}
753737

0 commit comments

Comments
 (0)