]> git.lizzy.rs Git - rust.git/commitdiff
Track the source of the type_target and value_target separately for ImportResolutions
authorAlex Crichton <alex@alexcrichton.com>
Mon, 10 Jun 2013 04:39:15 +0000 (21:39 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 10 Jun 2013 04:42:57 +0000 (21:42 -0700)
src/librustc/middle/resolve.rs
src/test/compile-fail/lint-unused-import-tricky-names.rs

index ec821ab2c7192f4a35b8979dff3cc827b0db9806..2d2eeff199fc50a1c31a43426e7ada23cd2cb656 100644 (file)
@@ -366,25 +366,31 @@ pub struct ImportResolution {
     /// The privacy of this `use` directive (whether it's `use` or
     /// `pub use`.
     privacy: Privacy,
-    id: node_id,
 
     // The number of outstanding references to this name. When this reaches
     // zero, outside modules can count on the targets being correct. Before
     // then, all bets are off; future imports could override this name.
-
     outstanding_references: uint,
 
     /// The value that this `use` directive names, if there is one.
     value_target: Option<Target>,
+    /// The source node of the `use` directive leading to the value target
+    /// being non-none
+    value_id: node_id,
+
     /// The type that this `use` directive names, if there is one.
     type_target: Option<Target>,
+    /// The source node of the `use` directive leading to the type target
+    /// being non-none
+    type_id: node_id,
 }
 
 pub fn ImportResolution(privacy: Privacy,
                         id: node_id) -> ImportResolution {
     ImportResolution {
         privacy: privacy,
-        id: id,
+        type_id: id,
+        value_id: id,
         outstanding_references: 0,
         value_target: None,
         type_target: None,
@@ -399,6 +405,13 @@ pub fn target_for_namespace(&self, namespace: Namespace)
             ValueNS     => return copy self.value_target
         }
     }
+
+    fn id(&self, namespace: Namespace) -> node_id {
+        match namespace {
+            TypeNS  => self.type_id,
+            ValueNS => self.value_id,
+        }
+    }
 }
 
 /// The link from a module up to its nearest parent node.
@@ -1920,7 +1933,8 @@ pub fn build_import_directive(@mut self,
 
                         // the source of this name is different now
                         resolution.privacy = privacy;
-                        resolution.id = id;
+                        resolution.type_id = id;
+                        resolution.value_id = id;
                     }
                     None => {
                         debug!("(building import directive) creating new");
@@ -2118,7 +2132,7 @@ pub fn resolve_import_for_module(@mut self,
                                                        containing_module,
                                                        target,
                                                        source,
-                                                       import_directive.span);
+                                                       import_directive);
                     }
                     GlobImport => {
                         let privacy = import_directive.privacy;
@@ -2181,7 +2195,7 @@ pub fn resolve_single_import(@mut self,
                                  containing_module: @mut Module,
                                  target: ident,
                                  source: ident,
-                                 span: span)
+                                 directive: &ImportDirective)
                                  -> ResolveResult<()> {
         debug!("(resolving single import) resolving `%s` = `%s::%s` from \
                 `%s`",
@@ -2270,9 +2284,10 @@ fn get_binding(this: @mut Resolver,
                                     return UnboundResult;
                                 }
                                 Some(target) => {
-                                    this.used_imports.insert(import_resolution.id);
+                                    let id = import_resolution.id(namespace);
+                                    this.used_imports.insert(id);
                                     return BoundResult(target.target_module,
-                                                    target.bindings);
+                                                       target.bindings);
                                 }
                             }
                         }
@@ -2323,8 +2338,10 @@ fn get_binding(this: @mut Resolver,
 
         match value_result {
             BoundResult(target_module, name_bindings) => {
+                debug!("(resolving single import) found value target");
                 import_resolution.value_target =
                     Some(Target(target_module, name_bindings));
+                import_resolution.value_id = directive.id;
             }
             UnboundResult => { /* Continue. */ }
             UnknownResult => {
@@ -2333,8 +2350,10 @@ fn get_binding(this: @mut Resolver,
         }
         match type_result {
             BoundResult(target_module, name_bindings) => {
+                debug!("(resolving single import) found type target");
                 import_resolution.type_target =
                     Some(Target(target_module, name_bindings));
+                import_resolution.type_id = directive.id;
             }
             UnboundResult => { /* Continue. */ }
             UnknownResult => {
@@ -2383,6 +2402,7 @@ fn get_binding(this: @mut Resolver,
             }
         }
 
+        let span = directive.span;
         if resolve_fail {
             self.session.span_err(span, fmt!("unresolved import: there is no `%s` in `%s`",
                                              *self.session.str_of(source),
@@ -2774,7 +2794,7 @@ pub fn resolve_item_in_lexical_scope(@mut self,
                     Some(target) => {
                         debug!("(resolving item in lexical scope) using \
                                 import resolution");
-                        self.used_imports.insert(import_resolution.id);
+                        self.used_imports.insert(import_resolution.id(namespace));
                         return Success(copy target);
                     }
                 }
@@ -3043,7 +3063,7 @@ pub fn resolve_name_in_module(@mut self,
                             import_resolution.privacy == Public => {
                         debug!("(resolving name in module) resolved to \
                                 import");
-                        self.used_imports.insert(import_resolution.id);
+                        self.used_imports.insert(import_resolution.id(namespace));
                         return Success(copy target);
                     }
                     Some(_) => {
@@ -4525,7 +4545,8 @@ pub fn resolve_definition_of_name_in_module(@mut self,
                                     namespace)) {
                             (Some(def), Some(Public)) => {
                                 // Found it.
-                                self.used_imports.insert(import_resolution.id);
+                                let id = import_resolution.id(namespace);
+                                self.used_imports.insert(id);
                                 return ImportNameDefinition(def);
                             }
                             (Some(_), _) | (None, _) => {
@@ -5140,7 +5161,7 @@ pub fn search_for_traits_containing_method(@mut self, name: ident)
                                                     &mut found_traits,
                                                     trait_def_id, name);
                                                 self.used_imports.insert(
-                                                    import_resolution.id);
+                                                    import_resolution.type_id);
                                             }
                                         }
                                         _ => {
index e36b5572909d857575ab80599e8ff627acd117c5..4b80c52471505ff5957837ea79ad9c4b76c7fe73 100644 (file)
 #[deny(unused_imports)];
 
 // Regression test for issue #6633
+mod issue6633 {
+    use self::foo::name::name; //~ ERROR: unused import
+    use self::foo::name;
 
-use foo::name::name; //~ ERROR: unused import
-use foo::name;
-
-pub mod foo {
-    pub mod name {
-        pub type a = int;
+    pub mod foo {
         pub mod name {
-            pub type a = float;
+            pub type a = int;
+            pub mod name {
+                pub type a = float;
+            }
         }
     }
+
+    fn bar() -> name::a { 1 }
 }
 
-fn bar() -> name::a { 1 }
+// Regression test for issue #6935
+mod issue6935 {
+    use self::a::foo::a::foo;
+    use self::a::foo; //~ ERROR: unused import
+
+    pub mod a {
+        pub mod foo {
+            pub mod a {
+                pub fn foo() {}
+            }
+        }
+    }
+
+    fn bar() { foo(); }
+}
 
 fn main(){}
+