From 5f4791555486c856fdf4b1c782305a589b9cbf25 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 17 Apr 2016 19:23:25 +0000 Subject: [PATCH] Refactor `is_prelude` to only apply to glob imports --- src/librustc_resolve/build_reduced_graph.rs | 25 ++++++--------------- src/librustc_resolve/resolve_imports.rs | 21 ++++++++--------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 2547c756b9d6a..51d00266b7191 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -177,13 +177,9 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> { } let subclass = ImportDirectiveSubclass::single(binding, source_name); + let span = view_path.span; + parent.add_import_directive(module_path, subclass, span, item.id, vis); self.unresolved_imports += 1; - parent.add_import_directive(module_path, - subclass, - view_path.span, - item.id, - vis, - is_prelude); } ViewPathList(_, ref source_items) => { // Make sure there's at most one `mod` import in the list. @@ -228,23 +224,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> { } }; let subclass = ImportDirectiveSubclass::single(rename, name); + let (span, id) = (source_item.span, source_item.node.id()); + parent.add_import_directive(module_path, subclass, span, id, vis); self.unresolved_imports += 1; - parent.add_import_directive(module_path, - subclass, - source_item.span, - source_item.node.id(), - vis, - is_prelude); } } ViewPathGlob(_) => { + let subclass = GlobImport { is_prelude: is_prelude }; + let span = view_path.span; + parent.add_import_directive(module_path, subclass, span, item.id, vis); self.unresolved_imports += 1; - parent.add_import_directive(module_path, - GlobImport, - view_path.span, - item.id, - vis, - is_prelude); } } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 03492043dd449..a337cb6a4c113 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -41,7 +41,7 @@ pub enum ImportDirectiveSubclass { type_determined: Cell, value_determined: Cell, }, - GlobImport, + GlobImport { is_prelude: bool }, } impl ImportDirectiveSubclass { @@ -64,7 +64,6 @@ pub struct ImportDirective<'a> { subclass: ImportDirectiveSubclass, span: Span, vis: ty::Visibility, // see note in ImportResolutionPerNamespace about how to use this - is_prelude: bool, } impl<'a> ImportDirective<'a> { @@ -84,7 +83,7 @@ impl<'a> ImportDirective<'a> { } pub fn is_glob(&self) -> bool { - match self.subclass { ImportDirectiveSubclass::GlobImport => true, _ => false } + match self.subclass { ImportDirectiveSubclass::GlobImport { .. } => true, _ => false } } } @@ -191,7 +190,7 @@ impl<'a> NameResolution<'a> { }; let name = match directive.subclass { SingleImport { source, .. } => source, - GlobImport => unreachable!(), + GlobImport { .. } => unreachable!(), }; match target_module.resolve_name(name, ns, false) { Failed(_) => {} @@ -282,8 +281,7 @@ impl<'a> ::ModuleS<'a> { subclass: ImportDirectiveSubclass, span: Span, id: NodeId, - vis: ty::Visibility, - is_prelude: bool) { + vis: ty::Visibility) { let directive = self.arenas.alloc_import_directive(ImportDirective { module_path: module_path, target_module: Cell::new(None), @@ -291,7 +289,6 @@ impl<'a> ::ModuleS<'a> { span: span, id: id, vis: vis, - is_prelude: is_prelude, }); self.unresolved_imports.borrow_mut().push(directive); @@ -304,8 +301,8 @@ impl<'a> ::ModuleS<'a> { } // We don't add prelude imports to the globs since they only affect lexical scopes, // which are not relevant to import resolution. - GlobImport if directive.is_prelude => {} - GlobImport => self.globs.borrow_mut().push(directive), + GlobImport { is_prelude: true } => {} + GlobImport { .. } => self.globs.borrow_mut().push(directive), } } @@ -496,7 +493,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { let (source, target, value_determined, type_determined) = match directive.subclass { SingleImport { source, target, ref value_determined, ref type_determined } => (source, target, value_determined, type_determined), - GlobImport => return self.resolve_glob_import(target_module, directive), + GlobImport { .. } => return self.resolve_glob_import(target_module, directive), }; // We need to resolve both namespaces for this to succeed. @@ -644,7 +641,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { } self.resolver.populate_module_if_necessary(target_module); - if directive.is_prelude { + if let GlobImport { is_prelude: true } = directive.subclass { *module_.prelude.borrow_mut() = Some(target_module); return Success(()); } @@ -747,7 +744,7 @@ fn import_path_to_string(names: &[Name], subclass: &ImportDirectiveSubclass) -> fn import_directive_subclass_to_string(subclass: &ImportDirectiveSubclass) -> String { match *subclass { SingleImport { source, .. } => source.to_string(), - GlobImport => "*".to_string(), + GlobImport { .. } => "*".to_string(), } }