]> git.lizzy.rs Git - rust.git/commitdiff
rustc_resolve: don't treat uniform_paths canaries as ambiguities unless they resolve...
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Thu, 13 Sep 2018 20:18:39 +0000 (23:18 +0300)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Thu, 13 Sep 2018 20:28:55 +0000 (23:28 +0300)
src/librustc_resolve/resolve_imports.rs
src/test/ui/run-pass/uniform-paths/basic-nested.rs
src/test/ui/run-pass/uniform-paths/basic.rs

index a8e3454b4cbd1956061e012138df7bfe3f653da1..e7d3a8ef661673f4d98afddd55d8ea9930e4cbcd 100644 (file)
@@ -730,7 +730,7 @@ struct UniformPathsCanaryResults<'a> {
             let external_crate = if ns == TypeNS && self.extern_prelude.contains(&name) {
                 let crate_id =
                     self.crate_loader.process_path_extern(name, span);
-                Some(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
+                Some(Def::Mod(DefId { krate: crate_id, index: CRATE_DEF_INDEX }))
             } else {
                 None
             };
@@ -741,30 +741,20 @@ struct UniformPathsCanaryResults<'a> {
                 return;
             }
 
-            let result_filter = |result: &&NameBinding| {
-                // Ignore canaries that resolve to an import of the same crate.
-                // That is, we allow `use crate_name; use crate_name::foo;`.
-                if let Some(def_id) = external_crate {
-                    if let Some(module) = result.module() {
-                        if module.normal_ancestor_id == def_id {
-                            return false;
-                        }
-                    }
+            {
+                let mut all_results = external_crate.into_iter().chain(
+                    results.module_scope.iter()
+                        .chain(&results.block_scopes)
+                        .map(|binding| binding.def())
+                );
+                let first = all_results.next().unwrap();
+
+                // An ambiguity requires more than one *distinct* possible resolution.
+                let possible_resultions =
+                    1 + all_results.filter(|&def| def != first).count();
+                if possible_resultions <= 1 {
+                    return;
                 }
-
-                true
-            };
-            let module_scope = results.module_scope.filter(result_filter);
-            let block_scopes = || {
-                results.block_scopes.iter().cloned().filter(result_filter)
-            };
-
-            // An ambiguity requires more than one possible resolution.
-            let possible_resultions =
-                (external_crate.is_some() as usize) +
-                module_scope.into_iter().chain(block_scopes()).count();
-            if possible_resultions <= 1 {
-                return;
             }
 
             errors = true;
@@ -777,7 +767,7 @@ struct UniformPathsCanaryResults<'a> {
                 err.span_label(span,
                     format!("can refer to external crate `::{}`", name));
             }
-            if let Some(result) = module_scope {
+            if let Some(result) = results.module_scope {
                 if !suggestion_choices.is_empty() {
                     suggestion_choices.push_str(" or ");
                 }
@@ -790,7 +780,7 @@ struct UniformPathsCanaryResults<'a> {
                         format!("may refer to `self::{}` in the future", name));
                 }
             }
-            for result in block_scopes() {
+            for result in results.block_scopes {
                 err.span_label(result.span,
                     format!("shadowed by block-scoped `{}`", name));
             }
index 1fe5d8abbe2e78966f25b2921dbdc6fe3b6392c4..1aaa1e70726c88295a352c3959f50b23bbd4fe37 100644 (file)
@@ -59,4 +59,12 @@ fn main() {
     bar::io::stdout();
     bar::std();
     bar::std!();
+
+    {
+        // Test that having `io` in a module scope and a non-module
+        // scope is allowed, when both resolve to the same definition.
+        use std::io;
+        use io::stdout;
+        stdout();
+    }
 }
index d8e68e9be972341305b611455a30e5d39f6d68a4..fbdac98d2582e9f3e87860e0c59617356c95ea49 100644 (file)
@@ -33,4 +33,12 @@ fn main() {
     Foo(());
     std_io::stdout();
     local_io(());
+
+    {
+        // Test that having `std_io` in a module scope and a non-module
+        // scope is allowed, when both resolve to the same definition.
+        use std::io as std_io;
+        use std_io::stdout;
+        stdout();
+    }
 }