]> git.lizzy.rs Git - rust.git/commitdiff
rustc_resolve: allow `use crate_name;` under `uniform_paths`.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Thu, 6 Sep 2018 19:07:56 +0000 (22:07 +0300)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Thu, 6 Sep 2018 19:07:56 +0000 (22:07 +0300)
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/resolve_imports.rs
src/test/run-pass/redundant.rs [new file with mode: 0644]
src/test/ui/rust-2018/uniform-paths/redundant.rs [deleted file]
src/test/ui/rust-2018/uniform-paths/redundant.stderr [deleted file]

index 80f6c263e199969648d80321edbe79fb49ad4612..37868a83e342168727f1623c8f2b0e7c5724cca4 100644 (file)
@@ -181,6 +181,23 @@ fn build_reduced_graph_for_use_tree(
                     self.session.features_untracked().uniform_paths);
 
             let source = module_path[0];
+
+            // HACK(eddyb) For `use x::{self, ...};`, use the ID of the
+            // `self` nested import for the canary. This allows the
+            // ambiguity reporting scope to ignore false positives
+            // in the same way it does for `use x;` (by comparing IDs).
+            let mut canary_id = id;
+            if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
+                for &(ref use_tree, id) in items {
+                    if let ast::UseTreeKind::Simple(..) = use_tree.kind {
+                        if use_tree.ident().name == keywords::SelfValue.name() {
+                            canary_id = id;
+                            break;
+                        }
+                    }
+                }
+            }
+
             // Helper closure to emit a canary with the given base path.
             let emit = |this: &mut Self, base: Option<Ident>| {
                 let subclass = SingleImport {
@@ -200,7 +217,7 @@ fn build_reduced_graph_for_use_tree(
                     base.into_iter().collect(),
                     subclass.clone(),
                     source.span,
-                    id,
+                    canary_id,
                     root_use_tree.span,
                     root_id,
                     ty::Visibility::Invisible,
index c60f9293d58e71428481cec776465b558a81d418..73c9af0d11c47f155046f52d018720fd4b2a6aee 100644 (file)
@@ -664,6 +664,14 @@ struct UniformPathsCanaryResult {
 
                 self.per_ns(|_, ns| {
                     if let Some(result) = result[ns].get().ok() {
+                        if let NameBindingKind::Import { directive, .. } = result.kind {
+                            // Skip canaries that resolve to the import itself.
+                            // These come from `use crate_name;`, which isn't really
+                            // ambiguous, as the import can't actually shadow itself.
+                            if directive.id == import.id {
+                                return;
+                            }
+                        }
                         if has_explicit_self {
                             // There should only be one `self::x` (module-scoped) canary.
                             assert_eq!(canary_results[ns].module_scope, None);
@@ -718,22 +726,6 @@ struct UniformPathsCanaryResult {
 
                 errors = true;
 
-                // Special-case the error when `self::x` finds its own `use x;`.
-                if has_external_crate &&
-                   results.module_scope == Some(span) &&
-                   results.block_scopes.is_empty() {
-                    let msg = format!("`{}` import is redundant", name);
-                    this.session.struct_span_err(span, &msg)
-                        .span_label(span,
-                            format!("refers to external crate `::{}`", name))
-                        .span_label(span,
-                            format!("defines `self::{}`, shadowing itself", name))
-                        .help(&format!("remove or write `::{}` explicitly instead", name))
-                        .note("relative `use` paths enabled by `#![feature(uniform_paths)]`")
-                        .emit();
-                    return;
-                }
-
                 let msg = format!("`{}` import is ambiguous", name);
                 let mut err = this.session.struct_span_err(span, &msg);
                 let mut suggestion_choices = String::new();
diff --git a/src/test/run-pass/redundant.rs b/src/test/run-pass/redundant.rs
new file mode 100644 (file)
index 0000000..39bd316
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2018 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.
+
+// edition:2018
+
+#![feature(uniform_paths)]
+
+use std;
+
+mod foo {
+    pub use std as my_std;
+}
+
+mod bar {
+    pub use std::{self};
+}
+
+fn main() {
+    self::std::io::stdout();
+    foo::my_std::io::stdout();
+    bar::std::io::stdout();
+}
diff --git a/src/test/ui/rust-2018/uniform-paths/redundant.rs b/src/test/ui/rust-2018/uniform-paths/redundant.rs
deleted file mode 100644 (file)
index 8f384b7..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright 2018 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.
-
-// edition:2018
-
-#![feature(uniform_paths)]
-
-use std;
-
-fn main() {}
diff --git a/src/test/ui/rust-2018/uniform-paths/redundant.stderr b/src/test/ui/rust-2018/uniform-paths/redundant.stderr
deleted file mode 100644 (file)
index dd38407..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-error: `std` import is redundant
-  --> $DIR/redundant.rs:15:5
-   |
-LL | use std;
-   |     ^^^
-   |     |
-   |     refers to external crate `::std`
-   |     defines `self::std`, shadowing itself
-   |
-   = help: remove or write `::std` explicitly instead
-   = note: relative `use` paths enabled by `#![feature(uniform_paths)]`
-
-error: aborting due to previous error
-