]> git.lizzy.rs Git - rust.git/commitdiff
Change `self` in an import list `use foo::{self, ...}` to only import a module or...
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Mon, 12 Dec 2016 03:35:48 +0000 (03:35 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Tue, 10 Jan 2017 05:54:22 +0000 (05:54 +0000)
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/resolve_imports.rs
src/test/compile-fail/issue-28075.rs
src/test/compile-fail/issue-38293.rs [new file with mode: 0644]

index 1b3791355d2c35a460f79d58c9e542e2babf4a70..5be21bc62c56cefbacdecbf2320947f0b3559695 100644 (file)
@@ -168,6 +168,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
                             target: binding,
                             source: source,
                             result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
+                            type_ns_only: false,
                         };
                         self.add_import_directive(
                             module_path, subclass, view_path.span, item.id, vis, expansion,
@@ -195,10 +196,10 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
 
                         for source_item in source_items {
                             let node = source_item.node;
-                            let (module_path, ident, rename) = {
+                            let (module_path, ident, rename, type_ns_only) = {
                                 if node.name.name != keywords::SelfValue.name() {
                                     let rename = node.rename.unwrap_or(node.name);
-                                    (module_path.clone(), node.name, rename)
+                                    (module_path.clone(), node.name, rename, false)
                                 } else {
                                     let ident = *module_path.last().unwrap();
                                     if ident.name == keywords::CrateRoot.name() {
@@ -212,13 +213,14 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
                                     }
                                     let module_path = module_path.split_last().unwrap().1;
                                     let rename = node.rename.unwrap_or(ident);
-                                    (module_path.to_vec(), ident, rename)
+                                    (module_path.to_vec(), ident, rename, true)
                                 }
                             };
                             let subclass = SingleImport {
                                 target: rename,
                                 source: ident,
                                 result: self.per_ns(|_, _| Cell::new(Err(Undetermined))),
+                                type_ns_only: type_ns_only,
                             };
                             let id = source_item.node.id;
                             self.add_import_directive(
index 91197e53d3c82523ab2559dc3b63bb556098be93..1702a1441bf8d4c9031077e9e851bc4ebd30876f 100644 (file)
@@ -41,6 +41,7 @@ pub enum ImportDirectiveSubclass<'a> {
         target: Ident,
         source: Ident,
         result: PerNS<Cell<Result<&'a NameBinding<'a>, Determinacy>>>,
+        type_ns_only: bool,
     },
     GlobImport {
         is_prelude: bool,
@@ -503,8 +504,9 @@ fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> bool {
         };
 
         directive.imported_module.set(Some(module));
-        let (source, target, result) = match directive.subclass {
-            SingleImport { source, target, ref result } => (source, target, result),
+        let (source, target, result, type_ns_only) = match directive.subclass {
+            SingleImport { source, target, ref result, type_ns_only } =>
+                (source, target, result, type_ns_only),
             GlobImport { .. } => {
                 self.resolve_glob_import(directive);
                 return true;
@@ -513,7 +515,7 @@ fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> bool {
         };
 
         let mut indeterminate = false;
-        self.per_ns(|this, ns| {
+        self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
             if let Err(Undetermined) = result[ns].get() {
                 result[ns].set(this.resolve_ident_in_module(module, source, ns, false, None));
             } else {
@@ -573,8 +575,8 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<Stri
             _ => return None,
         };
 
-        let (ident, result) = match directive.subclass {
-            SingleImport { source, ref result, .. } => (source, result),
+        let (ident, result, type_ns_only) = match directive.subclass {
+            SingleImport { source, ref result, type_ns_only, .. } => (source, result, type_ns_only),
             GlobImport { .. } if module.def_id() == directive.parent.def_id() => {
                 // Importing a module into itself is not allowed.
                 return Some("Cannot glob-import a module into itself.".to_string());
@@ -592,7 +594,7 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<Stri
         };
 
         let mut all_ns_err = true;
-        self.per_ns(|this, ns| {
+        self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
             if let Ok(binding) = result[ns].get() {
                 all_ns_err = false;
                 if this.record_use(ident, ns, binding, directive.span) {
@@ -604,7 +606,7 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<Stri
 
         if all_ns_err {
             let mut all_ns_failed = true;
-            self.per_ns(|this, ns| {
+            self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
                 match this.resolve_ident_in_module(module, ident, ns, false, Some(span)) {
                     Ok(_) => all_ns_failed = false,
                     _ => {}
index 3e3d898e3683da7b7811e0913cb0ac7e36b32b7d..cd73a45641111c9e20260536ee83af89d873e459 100644 (file)
@@ -18,7 +18,5 @@
 
 use lint_stability::{unstable, deprecated}; //~ ERROR use of unstable library feature 'test_feature'
 
-use lint_stability::unstable::{self as u}; //~ ERROR use of unstable library feature 'test_feature'
-
 fn main() {
 }
diff --git a/src/test/compile-fail/issue-38293.rs b/src/test/compile-fail/issue-38293.rs
new file mode 100644 (file)
index 0000000..f97fad7
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that `fn foo::bar::{self}` only imports `bar` in the type namespace.
+
+mod foo {
+    pub fn f() { }
+}
+use foo::f::{self};
+//~^ ERROR unresolved import
+//~| NOTE no `f` in `foo`
+
+mod bar {
+    pub fn baz() {}
+    pub mod baz {}
+}
+use bar::baz::{self};
+
+fn main() {
+    baz();
+    //~^ ERROR unresolved name `baz`
+    //~| NOTE unresolved name
+    //~| HELP module `baz` cannot be used as an expression
+}